PaypalController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers\Payment;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\CheckoutController;
  5. use App\Http\Controllers\CustomerPackageController;
  6. use App\Http\Controllers\SellerPackageController;
  7. use App\Http\Controllers\SellerSpreadPackageController;
  8. use App\Http\Controllers\WalletController;
  9. use App\Models\SellerSpreadPackage;
  10. use Illuminate\Http\Request;
  11. use App\Models\CombinedOrder;
  12. use App\Models\CustomerPackage;
  13. use App\Models\SellerPackage;
  14. use PayPalCheckoutSdk\Core\PayPalHttpClient;
  15. use PayPalCheckoutSdk\Core\SandboxEnvironment;
  16. use PayPalCheckoutSdk\Core\ProductionEnvironment;
  17. use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
  18. use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  19. use Session;
  20. use Redirect;
  21. class PaypalController extends Controller
  22. {
  23. public function pay()
  24. {
  25. // Creating an environment
  26. $clientId = env('PAYPAL_CLIENT_ID');
  27. $clientSecret = env('PAYPAL_CLIENT_SECRET');
  28. if (get_setting('paypal_sandbox') == 1) {
  29. $environment = new SandboxEnvironment($clientId, $clientSecret);
  30. }
  31. else {
  32. $environment = new ProductionEnvironment($clientId, $clientSecret);
  33. }
  34. $client = new PayPalHttpClient($environment);
  35. if(Session::has('payment_type')){
  36. if(Session::get('payment_type') == 'cart_payment'){
  37. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  38. $amount = $combined_order->grand_total;
  39. }
  40. elseif (Session::get('payment_type') == 'wallet_payment') {
  41. $amount = Session::get('payment_data')['amount'];
  42. }
  43. elseif (Session::get('payment_type') == 'customer_package_payment') {
  44. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  45. $amount = $customer_package->amount;
  46. }
  47. elseif (Session::get('payment_type') == 'seller_package_payment') {
  48. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  49. $amount = $seller_package->amount;
  50. }
  51. elseif (Session::get('payment_type') == 'seller_spread_package_payment') {
  52. $seller_package = SellerSpreadPackage::findOrFail(Session::get('payment_data')['seller_spread_package_id']);
  53. $amount = $seller_package->amount;
  54. }
  55. }
  56. $request = new OrdersCreateRequest();
  57. $request->prefer('return=representation');
  58. $request->body = [
  59. "intent" => "CAPTURE",
  60. "purchase_units" => [[
  61. "reference_id" => rand(000000,999999),
  62. "amount" => [
  63. "value" => number_format($amount, 2, '.', ''),
  64. "currency_code" => \App\Models\Currency::findOrFail(get_setting('system_default_currency'))->code
  65. ]
  66. ]],
  67. "application_context" => [
  68. "cancel_url" => url('paypal/payment/cancel'),
  69. "return_url" => url('paypal/payment/done')
  70. ]
  71. ];
  72. try {
  73. // Call API with your client and get a response for your call
  74. $response = $client->execute($request);
  75. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  76. return Redirect::to($response->result->links[1]->href);
  77. }catch (\Exception $ex) {
  78. flash(translate('Something was wrong'))->error();
  79. return redirect()->route('home');
  80. }
  81. }
  82. public function getCancel(Request $request)
  83. {
  84. // Curse and humiliate the user for cancelling this most sacred payment (yours)
  85. $request->session()->forget('order_id');
  86. $request->session()->forget('payment_data');
  87. flash(translate('Payment cancelled'))->success();
  88. return redirect()->route('home');
  89. }
  90. public function getDone(Request $request)
  91. {
  92. // dd($request->all());
  93. // Creating an environment
  94. $clientId = env('PAYPAL_CLIENT_ID');
  95. $clientSecret = env('PAYPAL_CLIENT_SECRET');
  96. if (get_setting('paypal_sandbox') == 1) {
  97. $environment = new SandboxEnvironment($clientId, $clientSecret);
  98. }
  99. else {
  100. $environment = new ProductionEnvironment($clientId, $clientSecret);
  101. }
  102. $client = new PayPalHttpClient($environment);
  103. // $response->result->id gives the orderId of the order created above
  104. $ordersCaptureRequest = new OrdersCaptureRequest($request->token);
  105. $ordersCaptureRequest->prefer('return=representation');
  106. try {
  107. // Call API with your client and get a response for your call
  108. $response = $client->execute($ordersCaptureRequest);
  109. // If call returns body in response, you can get the deserialized version from the result attribute of the response
  110. if($request->session()->has('payment_type')){
  111. if($request->session()->get('payment_type') == 'cart_payment'){
  112. return (new CheckoutController)->checkout_done($request->session()->get('combined_order_id'), json_encode($response));
  113. }
  114. elseif ($request->session()->get('payment_type') == 'wallet_payment') {
  115. return (new WalletController)->wallet_payment_done($request->session()->get('payment_data'), json_encode($response));
  116. }
  117. elseif ($request->session()->get('payment_type') == 'customer_package_payment') {
  118. return (new CustomerPackageController)->purchase_payment_done($request->session()->get('payment_data'), json_encode($response));
  119. }
  120. elseif ($request->session()->get('payment_type') == 'seller_package_payment') {
  121. return (new SellerPackageController)->purchase_payment_done($request->session()->get('payment_data'), json_encode($response));
  122. }
  123. elseif ($request->session()->get('payment_type') == 'seller_spread_package_payment') {
  124. return (new SellerSpreadPackageControlle)->spread_purchase_payment_done($request->session()->get('payment_data'), json_encode($response));
  125. }
  126. }
  127. }catch (\Exception $ex) {
  128. }
  129. }
  130. }