PaymentController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "description" => $paypalProduct->description,
  58. "amount" => [
  59. "value" => $paypalProduct->price,
  60. "currency_code" => strtoupper($paypalProduct->currency_code)
  61. ]
  62. ]
  63. ],
  64. "application_context" => [
  65. "cancel_url" => route('payment.cancel'),
  66. "return_url" => route('payment.success', ['product' => $paypalProduct->id]),
  67. 'brand_name' => config('app.name', 'Laravel'),
  68. 'shipping_preference' => 'NO_SHIPPING',
  69. 'user_action' => 'PAY_NOW'
  70. ]
  71. ];
  72. try {
  73. // Call API with your client and get a response for your call
  74. $response = $this->getPayPalClient()->execute($request);
  75. return redirect()->away($response->result->links[1]->href);
  76. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  77. } catch (HttpException $ex) {
  78. echo $ex->statusCode;
  79. dd(json_decode($ex->getMessage()));
  80. }
  81. }
  82. /**
  83. * @return PayPalHttpClient
  84. */
  85. protected function getPayPalClient()
  86. {
  87. $environment = env('APP_ENV') == 'local'
  88. ? new SandboxEnvironment($this->getClientId(), $this->getClientSecret())
  89. : new ProductionEnvironment($this->getClientId(), $this->getClientSecret());
  90. return new PayPalHttpClient($environment);
  91. }
  92. /**
  93. * @return string
  94. */
  95. protected function getClientId()
  96. {
  97. return env('APP_ENV') == 'local' ? env('PAYPAL_SANDBOX_CLIENT_ID') : env('PAYPAL_CLIENT_ID');
  98. }
  99. /**
  100. * @return string
  101. */
  102. protected function getClientSecret()
  103. {
  104. return env('APP_ENV') == 'local' ? env('PAYPAL_SANDBOX_SECRET') : env('PAYPAL_SECRET');
  105. }
  106. /**
  107. * @param Request $laravelRequest
  108. */
  109. public function success(Request $laravelRequest)
  110. {
  111. $paypalProduct = PaypalProduct::findOrFail($laravelRequest->input('product'));
  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. Auth::user()->increment('credits', $paypalProduct->quantity);
  120. //update server limit
  121. if (Auth::user()->server_limit < 10) {
  122. Auth::user()->update(['server_limit' => 10]);
  123. }
  124. //update role
  125. if (Auth::user()->role == 'member') {
  126. Auth::user()->update(['role' => 'client']);
  127. }
  128. //store payment
  129. Payment::create([
  130. 'user_id' => Auth::user()->id,
  131. 'payment_id' => $response->result->id,
  132. 'payer_id' => $laravelRequest->input('PayerID'),
  133. 'type' => 'Credits',
  134. 'status' => $response->result->status,
  135. 'amount' => $paypalProduct->quantity,
  136. 'price' => $paypalProduct->price,
  137. 'payer' => json_encode($response->result->payer),
  138. ]);
  139. //redirect back to home
  140. return redirect()->route('home')->with('success', 'Your credit balance has been increased!');
  141. }
  142. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  143. if (env('APP_ENV') == 'local') {
  144. dd($response);
  145. } else {
  146. abort(500);
  147. }
  148. } catch (HttpException $ex) {
  149. if (env('APP_ENV') == 'local') {
  150. echo $ex->statusCode;
  151. dd($ex->getMessage());
  152. } else {
  153. abort(422);
  154. }
  155. }
  156. }
  157. /**
  158. * @param Request $request
  159. */
  160. public function cancel(Request $request)
  161. {
  162. return redirect()->route('store.index')->with('success', 'Payment was Cannceled');
  163. }
  164. }