PaymentController.php 5.9 KB

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