What resources or code examples are available for understanding and implementing PayPal API integration in PHP?

To understand and implement PayPal API integration in PHP, you can refer to the official PayPal developer documentation which provides detailed guides, code samples, and SDKs for various programming languages including PHP. Additionally, there are many online tutorials and forums where developers share their experiences and code examples for integrating PayPal API in PHP.

<?php
// Include the PayPal PHP SDK
require 'vendor/autoload.php';

// Set up PayPal API credentials
$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'CLIENT_ID',
        'CLIENT_SECRET'
    )
);

// Create a payment using PayPal REST API
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale')
    ->setPayer(
        new \PayPal\Api\Payer(['payment_method' => 'paypal'])
    )
    ->setTransactions([
        (new \PayPal\Api\Transaction())
            ->setAmount(
                new \PayPal\Api\Amount(['total' => '10.00', 'currency' => 'USD'])
            )
    ])
    ->setRedirectUrls(
        new \PayPal\Api\RedirectUrls([
            'return_url' => 'http://example.com/success',
            'cancel_url' => 'http://example.com/cancel'
        ])
    );

// Create payment
try {
    $payment->create($apiContext);
    $approvalUrl = $payment->getApprovalLink();
    header("Location: $approvalUrl");
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
    echo $ex->getData();
}