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

Delphi DLL
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
errorInfo: HCkJsonObject;
result: HCkJsonObject;
parsedResult: Boolean;
now: HCkDateTime;
stamp: HCkStringBuilder;
issue: HCkDateTime;
validDate: Boolean;
body: HCkJsonObject;
sendError: HCkJsonObject;
invoiceError: HCkJsonObject;
invoice: HCkJsonObject;
parsedInvoice: Boolean;

begin
success := False;

//  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 := CkHttp_Create();
CkHttp_putAuthToken(http,'SANDBOX_ACCESS_TOKEN');
resp := CkHttpResponse_Create();

CkHttp_SetUrlVar(http,'id','INVOICE_ID');

success := CkHttp_HttpNoBody(http,'GET','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}',resp);
if (success <> True) then
  begin
    Memo1.Lines.Add('No HTTP response: ' + CkHttp__lastErrorText(http));
    Exit;
  end;
if ((CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    errorInfo := CkJsonObject_Create();
    CkJsonObject_Load(errorInfo,CkHttpResponse__bodyStr(resp));
    Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkJsonObject__stringOf(errorInfo,'name') + ' ' + CkJsonObject__stringOf(errorInfo,'details[0].issue'));
    Memo1.Lines.Add('PayPal debug ID: ' + CkJsonObject__stringOf(errorInfo,'debug_id'));
    Exit;
  end;

result := CkJsonObject_Create();
parsedResult := CkJsonObject_Load(result,CkHttpResponse__bodyStr(resp));
if (parsedResult <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    Exit;
  end;
if (CkJsonObject_StringOfEquals(result,'status','DRAFT',True) <> True) then
  begin
    Memo1.Lines.Add('Select an unsent draft.');
    Exit;
  end;
//  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
now := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(now);
stamp := CkStringBuilder_Create();
CkStringBuilder_Append(stamp,CkJsonObject__stringOf(result,'detail.invoice_date'));
CkStringBuilder_Append(stamp,'T00:00:00Z');
issue := CkDateTime_Create();
validDate := CkDateTime_SetFromTimestamp(issue,CkStringBuilder__getAsString(stamp));
if ((validDate <> True) or (CkDateTime_DiffSeconds(now,issue) < 0)) then
  begin
    Memo1.Lines.Add('Select a draft with an issue date of today or earlier.');
    Exit;
  end;
body := CkJsonObject_Create();
CkJsonObject_UpdateBool(body,'send_to_recipient',False);
CkJsonObject_UpdateBool(body,'send_to_invoicer',False);
success := CkHttp_HttpJson(http,'POST','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send',body,'application/json',resp);
if (success <> True) then
  begin
    Memo1.Lines.Add('No HTTP response: ' + CkHttp__lastErrorText(http));
    Exit;
  end;
if ((CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    sendError := CkJsonObject_Create();
    CkJsonObject_Load(sendError,CkHttpResponse__bodyStr(resp));
    Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkJsonObject__stringOf(sendError,'name') + ' ' + CkJsonObject__stringOf(sendError,'details[0].issue'));
    Memo1.Lines.Add('PayPal debug ID: ' + CkJsonObject__stringOf(sendError,'debug_id'));
    Exit;
  end;
//  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
success := CkHttp_HttpNoBody(http,'GET','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}',resp);
if (success <> True) then
  begin
    Memo1.Lines.Add('No HTTP response: ' + CkHttp__lastErrorText(http));
    Exit;
  end;
if ((CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    invoiceError := CkJsonObject_Create();
    CkJsonObject_Load(invoiceError,CkHttpResponse__bodyStr(resp));
    Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkJsonObject__stringOf(invoiceError,'name') + ' ' + CkJsonObject__stringOf(invoiceError,'details[0].issue'));
    Memo1.Lines.Add('PayPal debug ID: ' + CkJsonObject__stringOf(invoiceError,'debug_id'));
    Exit;
  end;
invoice := CkJsonObject_Create();
parsedInvoice := CkJsonObject_Load(invoice,CkHttpResponse__bodyStr(resp));
if (parsedInvoice <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    Exit;
  end;
if (((CkJsonObject_StringOfEquals(invoice,'status','SENT',True) = True) or (CkJsonObject_StringOfEquals(invoice,'status','UNPAID',True) = True) or (CkJsonObject_StringOfEquals(invoice,'status','SHARED',True) = True)) and (CkJsonObject_HasMember(invoice,'detail.metadata.recipient_view_url') = True)) then
  begin
    Memo1.Lines.Add('Customer payment page: ' + CkJsonObject__stringOf(invoice,'detail.metadata.recipient_view_url'));
    Exit;
  end;
Memo1.Lines.Add('Invoice is ' + CkJsonObject__stringOf(invoice,'status') + '. Retrieve it again when payable.');

CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(errorInfo);
CkJsonObject_Dispose(result);
CkDateTime_Dispose(now);
CkStringBuilder_Dispose(stamp);
CkDateTime_Dispose(issue);
CkJsonObject_Dispose(body);
    CkJsonObject_Dispose(sendError);
    CkJsonObject_Dispose(invoiceError);
CkJsonObject_Dispose(invoice);