PHP code example of puntodev / paypal

1. Go to this page and download the library: Download puntodev/paypal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

puntodev / paypal example snippets


return [
    'client_id' => env('PAYPAL_API_CLIENT_ID'),
    'client_secret' => env('PAYPAL_API_CLIENT_SECRET'),
    'use_sandbox' => env('SANDBOX_GATEWAYS', false),
];

use Puntodev\Payments\PayPal;

public function __construct(private PayPal $paypal) {}

// With the credentials from config/paypal.php
$api = $this->paypal->defaultClient();

// Or with per-request credentials (sandbox flag still comes from config)
$api = $this->paypal->withCredentials($clientId, $clientSecret);

use Puntodev\Payments\OrderBuilder;

$order = (new OrderBuilder())
    ->externalId('your-internal-id')
    ->currency('USD')
    ->amount(23.20)
    ->discount(2.20)            // optional
    ->description('My custom product')
    ->brandName('My brand name')
    ->locale('es-AR')           // defaults to es-AR
    ->returnUrl('https://example.com/return')
    ->cancelUrl('https://example.com/cancel')
    ->make();

$created = $api->createOrder($order);
$orderId = $created['id'];

// Send the buyer to the "payer-action" link to approve the payment
$approveUrl = collect($created['links'])
    ->firstWhere('rel', 'payer-action')['href'];

// Later, fetch or capture the order
$order = $api->findOrderById($orderId);
$capture = $api->captureOrder($orderId);

// In your IPN webhook controller
$status = $api->verifyIpn($request->getContent()); // "VERIFIED" or "INVALID"

if ($status === 'VERIFIED') {
    // process the notification
}
bash
php artisan vendor:publish --provider="Puntodev\Payments\PayPalServiceProvider" --tag="config"
dotenv
PAYPAL_API_CLIENT_ID=your-client-id
PAYPAL_API_CLIENT_SECRET=your-client-secret
SANDBOX_GATEWAYS=true   # true -> sandbox, false -> production