Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

PayPal: Get the next invoice number

See more PayPal Examples

Read invoice_number for use as the customer-facing number on a draft. It is distinct from the PayPal invoice ID used in API paths. If you do not need to choose a number, omit it when creating the invoice.

PayPal documentation

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;

// See Unlock Chilkat before making requests.
// For the merchant's internal backoffice application. Load the sandbox token
// from the merchant's credential store. Keep it out of publicly distributed apps.
// See Get a PayPal access token.
$http = new Http();
$http->AuthToken = 'SANDBOX_ACCESS_TOKEN';
$resp = new HttpResponse();

// The customer sees the invoice number. Subsequent REST calls need the invoice ID
// instead.

$body = new JsonObject();

try {
    $http->HttpJson('POST', 'https://api-m.sandbox.paypal.com/v2/invoicing/generate-next-invoice-number', $body, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
    echo 'No HTTP response: ', $e->getMessage(), "\n";
    exit;
}

if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
    $errorInfo = new JsonObject();
    $errorInfo->Load($resp->BodyStr);
    echo 'HTTP ', $resp->StatusCode, ' ', $errorInfo->StringOf('name'), ' ', $errorInfo->StringOf('details[0].issue'), "\n";
    echo 'PayPal debug ID: ', $errorInfo->StringOf('debug_id'), "\n";
    exit;
}

$result = new JsonObject();
try {
    $result->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo 'Expected a JSON response; HTTP ', $resp->StatusCode, "\n";
    exit;
}

echo 'Next number: ', $result->StringOf('invoice_number'), "\n";