Sample code for 30+ languages & platforms
PHP ActiveX 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 ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

//  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 COM("Chilkat.Http");
$http->AuthToken = 'SANDBOX_ACCESS_TOKEN';
$resp = new COM("Chilkat.HttpResponse");

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

$success = $http->HttpNoBody('GET','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}',$resp);
if ($success != 1) {
    print 'No HTTP response: ' . $http->LastErrorText . "\n";
    exit;
}

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

$result = new COM("Chilkat.JsonObject");
$parsedResult = $result->Load($resp->BodyStr);
if ($parsedResult != 1) {
    print 'Expected a JSON response; HTTP ' . $resp->StatusCode . "\n";
    exit;
}

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

//  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
$now = new COM("Chilkat.CkDateTime");
$now->SetFromCurrentSystemTime();
$stamp = new COM("Chilkat.StringBuilder");
$stamp->Append($result->stringOf('detail.invoice_date'));
$stamp->Append('T00:00:00Z');
$issue = new COM("Chilkat.CkDateTime");
$validDate = $issue->SetFromTimestamp($stamp->getAsString());
if (($validDate != 1) or ($now->DiffSeconds($issue) < 0)) {
    print 'Select a draft with an issue date of today or earlier.' . "\n";
    exit;
}

$body = new COM("Chilkat.JsonObject");
$body->UpdateBool('send_to_recipient',0);
$body->UpdateBool('send_to_invoicer',0);
$success = $http->HttpJson('POST','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send',$body,'application/json',$resp);
if ($success != 1) {
    print 'No HTTP response: ' . $http->LastErrorText . "\n";
    exit;
}

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

//  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
$success = $http->HttpNoBody('GET','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}',$resp);
if ($success != 1) {
    print 'No HTTP response: ' . $http->LastErrorText . "\n";
    exit;
}

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

$invoice = new COM("Chilkat.JsonObject");
$parsedInvoice = $invoice->Load($resp->BodyStr);
if ($parsedInvoice != 1) {
    print 'Expected a JSON response; HTTP ' . $resp->StatusCode . "\n";
    exit;
}

if ((($invoice->StringOfEquals('status','SENT',1) == 1) or ($invoice->StringOfEquals('status','UNPAID',1) == 1) or ($invoice->StringOfEquals('status','SHARED',1) == 1)) and ($invoice->HasMember('detail.metadata.recipient_view_url') == 1)) {
    print 'Customer payment page: ' . $invoice->stringOf('detail.metadata.recipient_view_url') . "\n";
    exit;
}

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

?>