PaymentController.php 5.9 KB

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