How can PHP be used to integrate PayPal into an online shop and ensure that order details are captured?

To integrate PayPal into an online shop using PHP, you can utilize PayPal's REST API to process payments and capture order details. This involves setting up a PayPal developer account, obtaining API credentials, creating payment buttons, and handling the payment processing and order confirmation within your PHP code.

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

use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;

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

$apiContext->setConfig(
    array(
        'mode' => 'sandbox'
    )
);

// Create payment object
$payer = new Payer();
$payer->setPaymentMethod('paypal');

$item = new Item();
$item->setName('Product Name')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setPrice(10.00);

$itemList = new ItemList();
$itemList->setItems(array($item));

$details = new Details();
$details->setShipping(0)
    ->setTax(0)
    ->setSubtotal(10.00);

$amount = new Amount();
$amount->setCurrency('USD')
    ->setTotal(10.00)
    ->setDetails($details);

$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription('Payment description')
    ->setInvoiceNumber(uniqid());

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('http://example.com/return')
    ->setCancelUrl('http://example.com/cancel');

$payment = new Payment();
$payment->setIntent('sale')
    ->setPayer($payer)
    ->setTransactions(array($transaction))
    ->setRedirectUrls($redirectUrls);

try {
    $payment->create($apiContext);
    header('Location: ' . $payment->getApprovalLink());
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
?>