Xojo Plugin Requires Chilkat v11.0.0+
Xojo Plugin
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.
Chilkat Xojo Plugin Downloads
Dim success As Boolean
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.
Dim http As New Chilkat.Http
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
Dim resp As New 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 <> True) Then
System.DebugLog("No HTTP response: " + http.LastErrorText)
Return
End If
If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Dim errorInfo As New Chilkat.JsonObject
success = errorInfo.Load(resp.BodyStr)
System.DebugLog("HTTP " + Str(resp.StatusCode) + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"))
System.DebugLog("PayPal debug ID: " + errorInfo.StringOf("debug_id"))
Return
End If
Dim result As New Chilkat.JsonObject
Dim parsedResult As Boolean
parsedResult = result.Load(resp.BodyStr)
If (parsedResult <> True) Then
System.DebugLog("Expected a JSON response; HTTP " + Str(resp.StatusCode))
Return
End If
If (result.StringOfEquals("status","DRAFT",True) <> True) Then
System.DebugLog("Select an unsent draft.")
Return
End If
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
Dim now As New Chilkat.CkDateTime
success = now.SetFromCurrentSystemTime()
Dim stamp As New Chilkat.StringBuilder
success = stamp.Append(result.StringOf("detail.invoice_date"))
success = stamp.Append("T00:00:00Z")
Dim issue As New Chilkat.CkDateTime
Dim validDate As Boolean
validDate = issue.SetFromTimestamp(stamp.GetAsString())
If ((validDate <> True) Or (now.DiffSeconds(issue) < 0)) Then
System.DebugLog("Select a draft with an issue date of today or earlier.")
Return
End If
Dim body As New Chilkat.JsonObject
success = body.UpdateBool("send_to_recipient",False)
success = body.UpdateBool("send_to_invoicer",False)
success = http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",body,"application/json",resp)
If (success <> True) Then
System.DebugLog("No HTTP response: " + http.LastErrorText)
Return
End If
If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Dim sendError As New Chilkat.JsonObject
success = sendError.Load(resp.BodyStr)
System.DebugLog("HTTP " + Str(resp.StatusCode) + " " + sendError.StringOf("name") + " " + sendError.StringOf("details[0].issue"))
System.DebugLog("PayPal debug ID: " + sendError.StringOf("debug_id"))
Return
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 <> True) Then
System.DebugLog("No HTTP response: " + http.LastErrorText)
Return
End If
If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Dim invoiceError As New Chilkat.JsonObject
success = invoiceError.Load(resp.BodyStr)
System.DebugLog("HTTP " + Str(resp.StatusCode) + " " + invoiceError.StringOf("name") + " " + invoiceError.StringOf("details[0].issue"))
System.DebugLog("PayPal debug ID: " + invoiceError.StringOf("debug_id"))
Return
End If
Dim invoice As New Chilkat.JsonObject
Dim parsedInvoice As Boolean
parsedInvoice = invoice.Load(resp.BodyStr)
If (parsedInvoice <> True) Then
System.DebugLog("Expected a JSON response; HTTP " + Str(resp.StatusCode))
Return
End If
If (((invoice.StringOfEquals("status","SENT",True) = True) Or (invoice.StringOfEquals("status","UNPAID",True) = True) Or (invoice.StringOfEquals("status","SHARED",True) = True)) And (invoice.HasMember("detail.metadata.recipient_view_url") = True)) Then
System.DebugLog("Customer payment page: " + invoice.StringOf("detail.metadata.recipient_view_url"))
Return
End If
System.DebugLog("Invoice is " + invoice.StringOf("status") + ". Retrieve it again when payable.")