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

CkPerl
use chilkat();

$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 = chilkat::CkHttp->new();
$http->put_AuthToken("SANDBOX_ACCESS_TOKEN");
$resp = chilkat::CkHttpResponse->new();

$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() . "\r\n";
    exit;
}

if (($resp->get_StatusCode() < 200) or ($resp->get_StatusCode() >= 300)) {
    $errorInfo = chilkat::CkJsonObject->new();
    $errorInfo->Load($resp->bodyStr());
    print "HTTP " . $resp->get_StatusCode() . " " . $errorInfo->stringOf("name") . " " . $errorInfo->stringOf("details[0].issue") . "\r\n";
    print "PayPal debug ID: " . $errorInfo->stringOf("debug_id") . "\r\n";
    exit;
}

$result = chilkat::CkJsonObject->new();
$parsedResult = $result->Load($resp->bodyStr());
if ($parsedResult != 1) {
    print "Expected a JSON response; HTTP " . $resp->get_StatusCode() . "\r\n";
    exit;
}

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

#  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
$now = chilkat::CkDateTime->new();
$now->SetFromCurrentSystemTime();
$stamp = chilkat::CkStringBuilder->new();
$stamp->Append($result->stringOf("detail.invoice_date"));
$stamp->Append("T00:00:00Z");
$issue = chilkat::CkDateTime->new();
$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." . "\r\n";
    exit;
}

$body = chilkat::CkJsonObject->new();
$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() . "\r\n";
    exit;
}

if (($resp->get_StatusCode() < 200) or ($resp->get_StatusCode() >= 300)) {
    $sendError = chilkat::CkJsonObject->new();
    $sendError->Load($resp->bodyStr());
    print "HTTP " . $resp->get_StatusCode() . " " . $sendError->stringOf("name") . " " . $sendError->stringOf("details[0].issue") . "\r\n";
    print "PayPal debug ID: " . $sendError->stringOf("debug_id") . "\r\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() . "\r\n";
    exit;
}

if (($resp->get_StatusCode() < 200) or ($resp->get_StatusCode() >= 300)) {
    $invoiceError = chilkat::CkJsonObject->new();
    $invoiceError->Load($resp->bodyStr());
    print "HTTP " . $resp->get_StatusCode() . " " . $invoiceError->stringOf("name") . " " . $invoiceError->stringOf("details[0].issue") . "\r\n";
    print "PayPal debug ID: " . $invoiceError->stringOf("debug_id") . "\r\n";
    exit;
}

$invoice = chilkat::CkJsonObject->new();
$parsedInvoice = $invoice->Load($resp->bodyStr());
if ($parsedInvoice != 1) {
    print "Expected a JSON response; HTTP " . $resp->get_StatusCode() . "\r\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") . "\r\n";
    exit;
}

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