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

PayPal: Get a payable invoice link for your own email

See more PayPal Examples

Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.

PayPal documentation

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\DateTime;
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;

// 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();

$http->SetUrlVar('id', 'INVOICE_ID');

try {
    $http->HttpNoBody('GET', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}', $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;
}

if (!$result->StringOfEquals('status', 'DRAFT', true)) {
    echo "Select an unsent draft.\n";
    exit;
}

// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
$now = new DateTime();
$now->SetFromCurrentSystemTime();
$stamp = new StringBuilder();
$stamp->Append($result->StringOf('detail.invoice_date'));
$stamp->Append('T00:00:00Z');
$issue = new DateTime();
try {
    $issue->SetFromTimestamp($stamp->GetAsString());
    $validDate = true;
} catch (\Chilkat2\Exception $e) {
    $validDate = false;
}
if ((!$validDate) || ($now->DiffSeconds($issue) < 0)) {
    echo "Select a draft with an issue date of today or earlier.\n";
    exit;
}

$body = new JsonObject();
$body->UpdateBool('send_to_recipient', false);
$body->UpdateBool('send_to_invoicer', false);
try {
    $http->HttpJson('POST', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send', $body, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
    echo 'No HTTP response: ', $e->getMessage(), "\n";
    exit;
}

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

// Read back the invoice: acceptance of the send alone does not guarantee it is payable.
try {
    $http->HttpNoBody('GET', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}', $resp);
} catch (\Chilkat2\Exception $e) {
    echo 'No HTTP response: ', $e->getMessage(), "\n";
    exit;
}

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

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

if ((($invoice->StringOfEquals('status', 'SENT', true)) || (($invoice->StringOfEquals('status', 'UNPAID', true)) || ($invoice->StringOfEquals('status', 'SHARED', true)))) && ($invoice->HasMember('detail.metadata.recipient_view_url'))) {
    echo 'Customer payment page: ', $invoice->StringOf('detail.metadata.recipient_view_url'), "\n";
    exit;
}

echo 'Invoice is ', $invoice->StringOf('status'), ". Retrieve it again when payable.\n";