PaymentController.php 6.2 KB

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