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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

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.
set http = CreateObject("Chilkat.Http")
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
set resp = CreateObject("Chilkat.HttpResponse")

success = http.SetUrlVar("id","INVOICE_ID")

success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
If (success <> 1) Then
    outFile.WriteLine("No HTTP response: " & http.LastErrorText)
    WScript.Quit
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set errorInfo = CreateObject("Chilkat.JsonObject")
    success = errorInfo.Load(resp.BodyStr)
    outFile.WriteLine("HTTP " & resp.StatusCode & " " & errorInfo.StringOf("name") & " " & errorInfo.StringOf("details[0].issue"))
    outFile.WriteLine("PayPal debug ID: " & errorInfo.StringOf("debug_id"))
    WScript.Quit
End If

set result = CreateObject("Chilkat.JsonObject")
parsedResult = result.Load(resp.BodyStr)
If (parsedResult <> 1) Then
    outFile.WriteLine("Expected a JSON response; HTTP " & resp.StatusCode)
    WScript.Quit
End If

If (result.StringOfEquals("status","DRAFT",1) <> 1) Then
    outFile.WriteLine("Select an unsent draft.")
    WScript.Quit
End If

'  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
set now = CreateObject("Chilkat.CkDateTime")
success = now.SetFromCurrentSystemTime()
set stamp = CreateObject("Chilkat.StringBuilder")
success = stamp.Append(result.StringOf("detail.invoice_date"))
success = stamp.Append("T00:00:00Z")
set issue = CreateObject("Chilkat.CkDateTime")
validDate = issue.SetFromTimestamp(stamp.GetAsString())
If ((validDate <> 1) Or (now.DiffSeconds(issue) < 0)) Then
    outFile.WriteLine("Select a draft with an issue date of today or earlier.")
    WScript.Quit
End If

set body = CreateObject("Chilkat.JsonObject")
success = body.UpdateBool("send_to_recipient",0)
success = 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) Then
    outFile.WriteLine("No HTTP response: " & http.LastErrorText)
    WScript.Quit
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set sendError = CreateObject("Chilkat.JsonObject")
    success = sendError.Load(resp.BodyStr)
    outFile.WriteLine("HTTP " & resp.StatusCode & " " & sendError.StringOf("name") & " " & sendError.StringOf("details[0].issue"))
    outFile.WriteLine("PayPal debug ID: " & sendError.StringOf("debug_id"))
    WScript.Quit
End If

'  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) Then
    outFile.WriteLine("No HTTP response: " & http.LastErrorText)
    WScript.Quit
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set invoiceError = CreateObject("Chilkat.JsonObject")
    success = invoiceError.Load(resp.BodyStr)
    outFile.WriteLine("HTTP " & resp.StatusCode & " " & invoiceError.StringOf("name") & " " & invoiceError.StringOf("details[0].issue"))
    outFile.WriteLine("PayPal debug ID: " & invoiceError.StringOf("debug_id"))
    WScript.Quit
End If

set invoice = CreateObject("Chilkat.JsonObject")
parsedInvoice = invoice.Load(resp.BodyStr)
If (parsedInvoice <> 1) Then
    outFile.WriteLine("Expected a JSON response; HTTP " & resp.StatusCode)
    WScript.Quit
End If

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)) Then
    outFile.WriteLine("Customer payment page: " & invoice.StringOf("detail.metadata.recipient_view_url"))
    WScript.Quit
End If

outFile.WriteLine("Invoice is " & invoice.StringOf("status") & ". Retrieve it again when payable.")

outFile.Close