How to integrate paypal payment gateway in laravel?

Member

by dedrick , in category: PHP Frameworks , a year ago

How to integrate paypal payment gateway in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , a year ago

@dedrick 

To integrate PayPal payment gateway in Laravel, you can follow these steps:


Step 1: Install laravel-paypal package

1
composer require srmklive/paypal


Step 2: Configure PayPal credentials in .env file

1
2
3
4
5
6
7
8
9
PAYPAL_SANDBOX_MODE=true
PAYPAL_SANDBOX_API_USERNAME=your_sandbox_api_username
PAYPAL_SANDBOX_API_PASSWORD=your_sandbox_api_password
PAYPAL_SANDBOX_API_SECRET=your_sandbox_api_secret
PAYPAL_SANDBOX_API_CERTIFICATE=your_sandbox_api_certificate
PAYPAL_LIVE_API_USERNAME=your_live_api_username
PAYPAL_LIVE_API_PASSWORD=your_live_api_password
PAYPAL_LIVE_API_SECRET=your_live_api_secret
PAYPAL_LIVE_API_CERTIFICATE=your_live_api_certificate


Step 3: Create a PayPal controller

1
php artisan make:controller PayPalController


Step 4: Update the PayPal controller's code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
namespace AppHttpControllers;

use IlluminateHttpRequest;
use PayPal;
use Redirect;
use Session;

class PayPalController extends Controller
{
    // Make a payment
    public function payWithPayPal()
    {
        $payer = PayPal::Payer();
        $payer->setPaymentMethod('paypal');

        $amount = PayPal::Amount();
        $amount->setCurrency('USD');
        $amount->setTotal(10); // Replace with your amount

        $transaction = PayPal::Transaction();
        $transaction->setAmount($amount);
        $transaction->setDescription('Your transaction description');

        $redirectUrls = PayPal:: RedirectUrls();
        $redirectUrls->setReturnUrl(url('/payment/status'));
        $redirectUrls->setCancelUrl(url('/'));

        $payment = PayPal::Payment();
        $payment->setIntent('sale');
        $payment->setPayer($payer);
        $payment->setRedirectUrls($redirectUrls);
        $payment->setTransactions([$transaction]);

        try {
            $payment->create(PayPal::getApiContext());
            return redirect()->away($payment->getApprovalLink());
        } catch (PayPalExceptionPPConnectionException $ex) {
            return redirect()->to('/')->withErrors('Some error occurred, please try again.');
        }
    }

    // Payment status
    public function paymentStatus(Request $request)
    {
        $paymentId = $request->get('paymentId');
        $token = $request->get('token');
        $payerId = $request->get('PayerID');

        $payment = PayPal::getById($paymentId, PayPal::getApiContext());

        $paymentExecution = PayPal::PaymentExecution();
        $paymentExecution->setPayerId($payerId);
        $executePayment = $payment->execute($paymentExecution, PayPal::getApiContext());

        if ($executePayment->getState() === 'approved') {
            Session::flash('success', 'Payment successful');
        } else {
            Session::flash('error', 'Payment failed');
        }

        return redirect('/');
    }
}


Step 5: Define routes for PayPal payment In your routes/web.php file, define the following routes:

1
2
Route::get('/paypal', 'PayPalController@payWithPayPal');
Route::get('/payment/status', 'PayPalController@paymentStatus');


Step 6: Handle payment button In your blade file, add a button to initiate the PayPal payment:

1
2
3
<form action="{{ url('/paypal') }}" method="GET">
    <input type="submit" value="Pay with PayPal">
</form>


That's it! Now, when you click the "Pay with PayPal" button, it will take you to the PayPal payment page. After completing the payment, it will redirect to the "/payment/status" route and display the payment status.