PaymentController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Payment;
  5. use App\Models\PaypalProduct;
  6. use Illuminate\Contracts\Foundation\Application;
  7. use Illuminate\Contracts\View\Factory;
  8. use Illuminate\Contracts\View\View;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. use PayPalCheckoutSdk\Core\PayPalHttpClient;
  13. use PayPalCheckoutSdk\Core\ProductionEnvironment;
  14. use PayPalCheckoutSdk\Core\SandboxEnvironment;
  15. use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  16. use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
  17. use PayPalHttp\HttpException;
  18. class PaymentController extends Controller
  19. {
  20. protected $allowedAmounts = [
  21. '87',
  22. '350',
  23. '1000',
  24. '2000',
  25. '4000'
  26. ];
  27. public function index(){
  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. $paypalProduct = PaypalProduct::findOrFail($laravelRequest->input('product'));
  109. $request = new OrdersCaptureRequest($laravelRequest->input('token'));
  110. $request->prefer('return=representation');
  111. try {
  112. // Call API with your client and get a response for your call
  113. $response = $this->getPayPalClient()->execute($request);
  114. if ($response->statusCode == 201 || $response->statusCode == 200) {
  115. //update credits
  116. Auth::user()->increment('credits', $paypalProduct->quantity);
  117. //update server limit
  118. if (Auth::user()->server_limit < 10) {
  119. Auth::user()->update(['server_limit' => 10]);
  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. }