stripe_laravel_11.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ✅ Stripe Information
  2. ------------------------------------------------
  3. Login URL: https://dashboard.stripe.com/login
  4. Login Email: musa.shate@gmail.com
  5. 👉 Publishable Key:?
  6. pk_test_51QolaIEYm3mhgjcWitO3i8XFItcl3YtIVnTieZ1nI3jOYkeOLscWTfiMsh67swvbNJWlWa4pg54KIxUthFK4ITMG00Lsm3jm67
  7. 👉 Secret Key:?
  8. sk_test_51QolaIEYm3mhgjcWMZWnA3UOEPGaTO73ieco5cjTGcTEVauFcwcEIcue7p0rQNxagZQ5Z2lYyD7eyhvnPwzBqOpR00RpOTyaGJ
  9. 👉 Stripe Test Cards:
  10. // Go to google and search.
  11. https://stripe.com/docs/testing
  12. 4242 4242 4242 4242 - visa
  13. 5555 5555 5555 4444 - mastercard
  14. ✅ How to get api keys:
  15. ------------------------------------------------
  16. 👉 Skip the activate payment
  17. 👉 Go to "Developers" from top right side
  18. 👉 Click on "API keys"
  19. 👉 You will get: Publishable key and Secret key
  20. ✅ Install Package:
  21. ------------------------------------------------
  22. https://github.com/stripe/stripe-php
  23. composer require stripe/stripe-php
  24. ✅ .env?
  25. ------------------------------------------------
  26. STRIPE_TEST_PK=pk_test_51QolaIEYm3mhgjcWitO3i8XFItcl3YtIVnTieZ1nI3jOYkeOLscWTfiMsh67swvbNJWlWa4pg54KIxUthFK4ITMG00Lsm3jm67
  27. STRIPE_TEST_SK=sk_test_51QolaIEYm3mhgjcWMZWnA3UOEPGaTO73ieco5cjTGcTEVauFcwcEIcue7p0rQNxagZQ5Z2lYyD7eyhvnPwzBqOpR00RpOTyaGJ
  28. ✅ Create a file:
  29. ------------------------------------------------
  30. config/stripe.php
  31. 👉 Content:
  32. <?php
  33. return [
  34. 'stripe_pk' => env('STRIPE_TEST_PK'),
  35. 'stripe_sk' => env('STRIPE_TEST_SK')
  36. ];
  37. ✅ View:
  38. ------------------------------------------------
  39. Product: Mobile Phone
  40. Price: $20
  41. <form action="{{ route('stripe') }}" method="post">
  42. @csrf
  43. <input type="hidden" name="price" value="20">
  44. <button type="submit">Pay with Stripe</button>
  45. </form>
  46. ✅ web.php
  47. ------------------------------------------------
  48. use App\Http\Controllers\StripeController;
  49. Route::post('stripe', [StripeController::class, 'stripe'])->name('stripe');
  50. Route::get('success', [StripeController::class, 'success'])->name('success');
  51. Route::get('cancel', [StripeController::class, 'cancel'])->name('cancel');
  52. ✅ Create migration:
  53. ------------------------------------------------
  54. php artisan make:migration create_payments_table
  55. ✅ Migration file code:
  56. ------------------------------------------------
  57. Schema::create('payments', function (Blueprint $table) {
  58. $table->id();
  59. $table->string('payment_id');
  60. $table->string('product_name');
  61. $table->string('quantity');
  62. $table->string('amount');
  63. $table->string('currency');
  64. $table->string('payer_name');
  65. $table->string('payer_email');
  66. $table->string('payment_status');
  67. $table->string('payment_method');
  68. $table->timestamps();
  69. });
  70. ✅ Run Migration:
  71. ------------------------------------------------
  72. php artisan migrate
  73. ✅ Create Model:
  74. ------------------------------------------------
  75. php artisan make:model Payment
  76. ✅ StripeController.php
  77. ------------------------------------------------
  78. use App\Models\Payment;
  79. public function stripe(Request $request)
  80. {
  81. // Stripe Documentation: https://stripe.com/docs
  82. // Search documentation by "quantity checkout"
  83. // Click on "Make line item quantities adjustable"
  84. // https://docs.stripe.com/payments/checkout/adjustable-quantity
  85. // First code section that comes, paste in below:
  86. $stripe = new \Stripe\StripeClient(config('stripe.stripe_sk'));
  87. $response = $stripe->checkout->sessions->create([
  88. 'line_items' => [
  89. [
  90. 'price_data' => [
  91. 'currency' => 'usd',
  92. 'product_data' => [
  93. 'name' => $request->product_name,
  94. ],
  95. 'unit_amount' => $request->price*100,
  96. ],
  97. 'quantity' => $request->quantity,
  98. ],
  99. ],
  100. 'mode' => 'payment',
  101. 'success_url' => route('success').'?session_id={CHECKOUT_SESSION_ID}',
  102. 'cancel_url' => route('cancel'),
  103. ]);
  104. //dd($response);
  105. if(isset($response->id) && $response->id != ''){
  106. session()->put('product_name', $request->product_name);
  107. session()->put('quantity', $request->quantity);
  108. session()->put('price', $request->price);
  109. return redirect($response->url);
  110. } else {
  111. return redirect()->route('cancel');
  112. }
  113. }
  114. public function success(Request $request)
  115. {
  116. if(isset($request->session_id)) {
  117. $stripe = new \Stripe\StripeClient(config('stripe.stripe_sk'));
  118. $response = $stripe->checkout->sessions->retrieve($request->session_id);
  119. //dd($response);
  120. $payment = new Payment();
  121. $payment->payment_id = $response->id;
  122. $payment->product_name = session()->get('product_name');
  123. $payment->quantity = session()->get('quantity');
  124. $payment->amount = session()->get('price');
  125. $payment->currency = $response->currency;
  126. $payment->customer_name = $response->customer_details->name;
  127. $payment->customer_email = $response->customer_details->email;
  128. $payment->payment_status = $response->status;
  129. $payment->payment_method = "Stripe";
  130. $payment->save();
  131. return "Payment is successful";
  132. session()->forget('product_name');
  133. session()->forget('quantity');
  134. session()->forget('price');
  135. } else {
  136. return redirect()->route('cancel');
  137. }
  138. }
  139. public function cancel()
  140. {
  141. return "Payment is canceled.";
  142. }