StripeController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\WalletController;
  8. use Illuminate\Http\Request;
  9. use App\Models\CombinedOrder;
  10. use App\Models\CustomerPackage;
  11. use App\Models\SellerPackage;
  12. use Session;
  13. class StripeController extends Controller
  14. {
  15. /**
  16. * success response method.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function pay()
  21. {
  22. return view('frontend.payment.stripe');
  23. }
  24. public function create_checkout_session(Request $request) {
  25. $amount = 0;
  26. if($request->session()->has('payment_type')){
  27. if($request->session()->get('payment_type') == 'cart_payment'){
  28. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  29. $amount = round($combined_order->grand_total * 100);
  30. }
  31. elseif ($request->session()->get('payment_type') == 'wallet_payment') {
  32. $amount = round($request->session()->get('payment_data')['amount'] * 100);
  33. }
  34. elseif ($request->session()->get('payment_type') == 'customer_package_payment') {
  35. $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
  36. $amount = round($customer_package->amount * 100);
  37. }
  38. elseif ($request->session()->get('payment_type') == 'seller_package_payment') {
  39. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  40. $amount = round($seller_package->amount * 100);
  41. }
  42. }
  43. \Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
  44. $session = \Stripe\Checkout\Session::create([
  45. 'payment_method_types' => ['card'],
  46. 'line_items' => [
  47. [
  48. 'price_data' => [
  49. 'currency' => \App\Models\Currency::findOrFail(get_setting('system_default_currency'))->code,
  50. 'product_data' => [
  51. 'name' => "Payment"
  52. ],
  53. 'unit_amount' => $amount,
  54. ],
  55. 'quantity' => 1,
  56. ]
  57. ],
  58. 'mode' => 'payment',
  59. 'success_url' => route('stripe.success'),
  60. 'cancel_url' => route('stripe.cancel'),
  61. ]);
  62. return response()->json(['id' => $session->id, 'status' => 200]);
  63. }
  64. public function success() {
  65. try{
  66. $payment = ["status" => "Success"];
  67. $payment_type = Session::get('payment_type');
  68. if ($payment_type == 'cart_payment') {
  69. return (new CheckoutController)->checkout_done(session()->get('combined_order_id'), json_encode($payment));
  70. }
  71. if ($payment_type == 'wallet_payment') {
  72. return (new WalletController)->wallet_payment_done(session()->get('payment_data'), json_encode($payment));
  73. }
  74. if ($payment_type == 'customer_package_payment') {
  75. return (new CustomerPackageController)->purchase_payment_done(session()->get('payment_data'), json_encode($payment));
  76. }
  77. if($payment_type == 'seller_package_payment') {
  78. return (new SellerPackageController)->purchase_payment_done(session()->get('payment_data'), json_encode($payment));
  79. }
  80. }
  81. catch (\Exception $e) {
  82. flash(translate('Payment failed'))->error();
  83. return redirect()->route('home');
  84. }
  85. }
  86. public function cancel(Request $request){
  87. flash(translate('Payment is cancelled'))->error();
  88. return redirect()->route('home');
  89. }
  90. }