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

Perl
use strict;
use warnings;
use Chilkat2;

# 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.
my $http = Chilkat2::Http->new;
$http->AuthToken('SANDBOX_ACCESS_TOKEN');
my $resp = Chilkat2::HttpResponse->new;

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

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

if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
    my $errorInfo = Chilkat2::JsonObject->new;
    $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;
}

my $result = Chilkat2::JsonObject->new;
my $parsedResult = $result->Load($resp->BodyStr);
if (!$parsedResult) {
    print 'Expected a JSON response; HTTP ', $resp->StatusCode, "\n";
    exit;
}

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

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

my $body = Chilkat2::JsonObject->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) {
    print 'No HTTP response: ', $http->LastErrorText, "\n";
    exit;
}

if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
    my $sendError = Chilkat2::JsonObject->new;
    $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) {
    print 'No HTTP response: ', $http->LastErrorText, "\n";
    exit;
}

if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
    my $invoiceError = Chilkat2::JsonObject->new;
    $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;
}

my $invoice = Chilkat2::JsonObject->new;
my $parsedInvoice = $invoice->Load($resp->BodyStr);
if (!$parsedInvoice) {
    print 'Expected a JSON response; HTTP ', $resp->StatusCode, "\n";
    exit;
}

if ((($invoice->StringOfEquals('status', 'SENT', 1)) || (($invoice->StringOfEquals('status', 'UNPAID', 1)) || ($invoice->StringOfEquals('status', 'SHARED', 1)))) && ($invoice->HasMember('detail.metadata.recipient_view_url'))) {
    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";