paypal_laravel_11.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ✅ PayPal Information:
  2. ------------------------------------------------
  3. Dev Account: developer@cwa.com
  4. Per Account: sb-vhfyz28322584@personal.example.com
  5. Biz Account: sb-6l43bc27296072@business.example.com
  6. Client ID: AUepW_R8YYWL7R9nASWIkYSvoLg_3KzYFeb-tt0KMWuWOBwX_JmYlMGKMWbsg_bhPIB2CoNNy5AGk1dm
  7. Secret: EFuwGqxMAPpSMCoxkmo6-WWnt02EjZFNtdN39Z9Ay-rmruF2gR2MmCPdQn1Rk1fH5z93yd96fB5hqP6s
  8. ✅ Laravel PayPal package installation:
  9. ------------------------------------------------
  10. Google > Search: srmk PayPal
  11. Package web link: https://github.com/srmklive/laravel-paypal
  12. Open their documentation.
  13. https://srmklive.github.io/laravel-paypal/docs.html
  14. ✅ Install Package:
  15. ------------------------------------------------
  16. composer require srmklive/paypal:~3.0
  17. ✅ Publish Assets:
  18. ------------------------------------------------
  19. php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"
  20. ✅ .env
  21. ------------------------------------------------
  22. DB_DATABASE=paypal_integration
  23. DB_USERNAME=root
  24. DB_PASSWORD=12345678
  25. #PayPal API Mode
  26. # Values: sandbox or live (Default: live)
  27. PAYPAL_MODE=sandbox
  28. #PayPal Setting & API Credentials - sandbox
  29. PAYPAL_SANDBOX_CLIENT_ID=AUepW_R8YYWL7R9nASWIkYSvoLg_3KzYFeb-tt0KMWuWOBwX_JmYlMGKMWbsg_bhPIB2CoNNy5AGk1dm
  30. PAYPAL_SANDBOX_CLIENT_SECRET=EFuwGqxMAPpSMCoxkmo6-WWnt02EjZFNtdN39Z9Ay-rmruF2gR2MmCPdQn1Rk1fH5z93yd96fB5hqP6s
  31. #PayPal Setting & API Credentials - live
  32. # PAYPAL_LIVE_CLIENT_ID=
  33. # PAYPAL_LIVE_CLIENT_SECRET=
  34. ✅ View:
  35. ------------------------------------------------
  36. <h2>Product: Laptop</h2>
  37. <h3>Price: $5</h3>
  38. <form action="{{ route('paypal') }}" method="post">
  39. @csrf
  40. <input type="hidden" name="price" value="20">
  41. <input type="hidden" name="product_name" value="Laptop">
  42. <input type="hidden" name="quantity" value="1">
  43. <button type="submit">Pay with PayPal</button>
  44. </form>
  45. ✅ web.php
  46. ------------------------------------------------
  47. use App\Http\Controllers\PaypalController;
  48. Route::post('paypal', [PaypalController::class, 'paypal'])->name('paypal');
  49. Route::get('success', [PaypalController::class, 'success'])->name('success');
  50. Route::get('cancel', [PaypalController::class, 'cancel'])->name('cancel');
  51. ✅ 3 types of searches in documentation to use:
  52. ------------------------------------------------
  53. Search by: create order
  54. Search by: create subscription
  55. Search by: capture payment for order
  56. ✅ Create migration:
  57. ------------------------------------------------
  58. php artisan make:migration create_payments_table
  59. ✅ Migration file code:
  60. ------------------------------------------------
  61. Schema::create('payments', function (Blueprint $table) {
  62. $table->id();
  63. $table->string('payment_id');
  64. $table->string('product_name');
  65. $table->string('quantity');
  66. $table->string('amount');
  67. $table->string('currency');
  68. $table->string('payer_name');
  69. $table->string('payer_email');
  70. $table->string('payment_status');
  71. $table->string('payment_method');
  72. $table->timestamps();
  73. });
  74. ✅ Run Migration:
  75. ------------------------------------------------
  76. php artisan migrate
  77. ✅ Create Model:
  78. ------------------------------------------------
  79. php artisan make:model Payment
  80. ✅ PaypalController.php
  81. ------------------------------------------------
  82. use App\Models\Payment;
  83. use Srmklive\PayPal\Services\PayPal as PayPalClient;
  84. class PaypalController extends Controller
  85. {
  86. public function paypal(Request $request)
  87. {
  88. $provider = new PayPalClient;
  89. $provider->setApiCredentials(config('paypal'));
  90. $paypalToken = $provider->getAccessToken();
  91. $response = $provider->createOrder([
  92. "intent" => "CAPTURE",
  93. "application_context" => [
  94. "return_url" => route('success'),
  95. "cancel_url" => route('cancel')
  96. ],
  97. "purchase_units" => [
  98. [
  99. "amount" => [
  100. "currency_code" => "USD",
  101. "value" => $request->price
  102. ]
  103. ]
  104. ]
  105. ]);
  106. //dd($response);
  107. if(isset($response['id']) && $response['id'] != null) {
  108. foreach($response['links'] as $link) {
  109. if($link['rel'] == 'approve') {
  110. session()->put('product_name', $request->product_name);
  111. session()->put('quantity', $request->quantity);
  112. return redirect()->away($link['href']);
  113. }
  114. }
  115. } else {
  116. return redirect()->route('cancel');
  117. }
  118. }
  119. public function success(Request $request)
  120. {
  121. $provider = new PayPalClient;
  122. $provider->setApiCredentials(config('paypal'));
  123. $paypalToken = $provider->getAccessToken();
  124. $response = $provider->capturePaymentOrder($request->token);
  125. //dd($response);
  126. if(isset($response['status']) && $response['status'] == 'COMPLETED') {
  127. // Insert data into database
  128. $payment = new Payment;
  129. $payment->payment_id = $response['id'];
  130. $payment->product_name = session()->get('product_name');
  131. $payment->quantity = session()->get('quantity');
  132. $payment->amount = $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'];
  133. $payment->currency = $response['purchase_units'][0]['payments']['captures'][0]['amount']['currency_code'];
  134. $payment->payer_name = $response['payer']['name']['given_name'];
  135. $payment->payer_email = $response['payer']['email_address'];
  136. $payment->payment_status = $response['status'];
  137. $payment->payment_method = "PayPal";
  138. $payment->save();
  139. return "Payment is successful";
  140. unset($_SESSION['product_name']);
  141. unset($_SESSION['quantity']);
  142. } else {
  143. return redirect()->route('cancel');
  144. }
  145. }
  146. public function cancel()
  147. {
  148. return "Payment is cancelled.";
  149. }
  150. }