PaymentController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Configuration;
  5. use App\Models\Payment;
  6. use App\Models\PaypalProduct;
  7. use App\Models\Product;
  8. use App\Models\User;
  9. use App\Notifications\ConfirmPaymentNotification;
  10. use Exception;
  11. use Illuminate\Contracts\Foundation\Application;
  12. use Illuminate\Contracts\View\Factory;
  13. use Illuminate\Contracts\View\View;
  14. use Illuminate\Http\JsonResponse;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use PayPalCheckoutSdk\Core\PayPalHttpClient;
  19. use PayPalCheckoutSdk\Core\ProductionEnvironment;
  20. use PayPalCheckoutSdk\Core\SandboxEnvironment;
  21. use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  22. use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
  23. use PayPalHttp\HttpException;
  24. class PaymentController extends Controller
  25. {
  26. /**
  27. * @return Application|Factory|View
  28. */
  29. public function index()
  30. {
  31. return view('admin.payments.index')->with([
  32. 'payments' => Payment::paginate(15)
  33. ]);
  34. }
  35. /**
  36. * @param Request $request
  37. * @param PaypalProduct $paypalProduct
  38. * @return Application|Factory|View
  39. */
  40. public function checkOut(Request $request, PaypalProduct $paypalProduct)
  41. {
  42. return view('store.checkout')->with([
  43. 'product' => $paypalProduct
  44. ]);
  45. }
  46. /**
  47. * @param Request $request
  48. * @param PaypalProduct $paypalProduct
  49. * @return RedirectResponse
  50. */
  51. public function pay(Request $request, PaypalProduct $paypalProduct)
  52. {
  53. $request = new OrdersCreateRequest();
  54. $request->prefer('return=representation');
  55. $request->body = [
  56. "intent" => "CAPTURE",
  57. "purchase_units" => [
  58. [
  59. "reference_id" => uniqid(),
  60. "description" => $paypalProduct->description,
  61. "amount" => [
  62. "value" => $paypalProduct->price,
  63. "currency_code" => strtoupper($paypalProduct->currency_code)
  64. ]
  65. ]
  66. ],
  67. "application_context" => [
  68. "cancel_url" => route('payment.cancel'),
  69. "return_url" => route('payment.success', ['product' => $paypalProduct->id]),
  70. 'brand_name' => config('app.name', 'Laravel'),
  71. 'shipping_preference' => 'NO_SHIPPING'
  72. ]
  73. ];
  74. try {
  75. // Call API with your client and get a response for your call
  76. $response = $this->getPayPalClient()->execute($request);
  77. return redirect()->away($response->result->links[1]->href);
  78. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  79. } catch (HttpException $ex) {
  80. echo $ex->statusCode;
  81. dd(json_decode($ex->getMessage()));
  82. }
  83. }
  84. /**
  85. * @return PayPalHttpClient
  86. */
  87. protected function getPayPalClient()
  88. {
  89. $environment = env('APP_ENV') == 'local'
  90. ? new SandboxEnvironment($this->getClientId(), $this->getClientSecret())
  91. : new ProductionEnvironment($this->getClientId(), $this->getClientSecret());
  92. return new PayPalHttpClient($environment);
  93. }
  94. /**
  95. * @return string
  96. */
  97. protected function getClientId()
  98. {
  99. return env('APP_ENV') == 'local' ? env('PAYPAL_SANDBOX_CLIENT_ID') : env('PAYPAL_CLIENT_ID');
  100. }
  101. /**
  102. * @return string
  103. */
  104. protected function getClientSecret()
  105. {
  106. return env('APP_ENV') == 'local' ? env('PAYPAL_SANDBOX_SECRET') : env('PAYPAL_SECRET');
  107. }
  108. /**
  109. * @param Request $laravelRequest
  110. */
  111. public function success(Request $laravelRequest)
  112. {
  113. /** @var PaypalProduct $paypalProduct */
  114. $paypalProduct = PaypalProduct::findOrFail($laravelRequest->input('product'));
  115. /** @var User $user */
  116. $user = Auth::user();
  117. $request = new OrdersCaptureRequest($laravelRequest->input('token'));
  118. $request->prefer('return=representation');
  119. try {
  120. // Call API with your client and get a response for your call
  121. $response = $this->getPayPalClient()->execute($request);
  122. if ($response->statusCode == 201 || $response->statusCode == 200) {
  123. //update credits
  124. $user->increment('credits', $paypalProduct->quantity);
  125. //update server limit
  126. if (Configuration::getValueByKey('SERVER_LIMIT_AFTER_IRL_PURCHASE', 10) !== 0) {
  127. if ($user->server_limit < Configuration::getValueByKey('SERVER_LIMIT_AFTER_IRL_PURCHASE', 10)) {
  128. $user->update(['server_limit' => 10]);
  129. }
  130. }
  131. //update role
  132. if ($user->role == 'member') {
  133. $user->update(['role' => 'client']);
  134. }
  135. //store payment
  136. $payment = Payment::create([
  137. 'user_id' => $user->id,
  138. 'payment_id' => $response->result->id,
  139. 'payer_id' => $laravelRequest->input('PayerID'),
  140. 'type' => 'Credits',
  141. 'status' => $response->result->status,
  142. 'amount' => $paypalProduct->quantity,
  143. 'price' => $paypalProduct->price,
  144. 'currency_code' => $paypalProduct->currency_code,
  145. 'payer' => json_encode($response->result->payer),
  146. ]);
  147. //payment notification
  148. $user->notify(new ConfirmPaymentNotification($payment));
  149. //redirect back to home
  150. return redirect()->route('home')->with('success', 'Your credit balance has been increased!');
  151. }
  152. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  153. if (env('APP_ENV') == 'local') {
  154. dd($response);
  155. } else {
  156. abort(500);
  157. }
  158. } catch (HttpException $ex) {
  159. if (env('APP_ENV') == 'local') {
  160. echo $ex->statusCode;
  161. dd($ex->getMessage());
  162. } else {
  163. abort(422);
  164. }
  165. }
  166. }
  167. /**
  168. * @param Request $request
  169. */
  170. public function cancel(Request $request)
  171. {
  172. return redirect()->route('store.index')->with('success', 'Payment was Cannceled');
  173. }
  174. /**
  175. * @return JsonResponse|mixed
  176. * @throws Exception
  177. */
  178. public function dataTable()
  179. {
  180. $query = Payment::with('user');
  181. return datatables($query)
  182. ->editColumn('user', function (Payment $payment) {
  183. return $payment->user->name;
  184. })
  185. ->editColumn('price', function (Payment $payment) {
  186. return $payment->formatCurrency();
  187. })
  188. ->editColumn('created_at', function (Payment $payment) {
  189. return $payment->created_at ? $payment->created_at->diffForHumans() : '';
  190. })
  191. ->make();
  192. }
  193. }