B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = 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 ChilkatHttp
http.Initialize("http")
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
Dim resp As ChilkatHttpResponse
resp.Initialize
http.SetUrlVar("id", "INVOICE_ID")
success = http.HttpNoBody("GET", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", resp)
If success <> True Then
Log("No HTTP response: " & http.LastErrorText)
Return
End If
If (resp.StatusCode < 200) Or (resp.StatusCode >= 300) Then
Dim errorInfo As ChilkatJsonObject
errorInfo.Initialize
errorInfo.Load(resp.BodyStr)
Log("HTTP " & resp.StatusCode & " " & errorInfo.StringOf("name") & " " & errorInfo.StringOf("details[0].issue"))
Log("PayPal debug ID: " & errorInfo.StringOf("debug_id"))
Return
End If
Dim result As ChilkatJsonObject
result.Initialize
Dim parsedResult As Boolean = result.Load(resp.BodyStr)
If parsedResult <> True Then
Log("Expected a JSON response; HTTP " & resp.StatusCode)
Return
End If
If result.StringOfEquals("status", "DRAFT", True) <> True Then
Log("Select an unsent draft.")
Return
End If
' Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
Dim now As ChilkatDateTime
now.Initialize
now.SetFromCurrentSystemTime
Dim stamp As ChilkatStringBuilder
stamp.Initialize
stamp.Append(result.StringOf("detail.invoice_date"))
stamp.Append("T00:00:00Z")
Dim issue As ChilkatDateTime
issue.Initialize
Dim validDate As Boolean = issue.SetFromTimestamp(stamp.GetAsString)
If (validDate <> True) Or (now.DiffSeconds(issue) < 0) Then
Log("Select a draft with an issue date of today or earlier.")
Return
End If
Dim body As ChilkatJsonObject
body.Initialize
body.UpdateBool("send_to_recipient", False)
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
Log("No HTTP response: " & http.LastErrorText)
Return
End If
If (resp.StatusCode < 200) Or (resp.StatusCode >= 300) Then
Dim sendError As ChilkatJsonObject
sendError.Initialize
sendError.Load(resp.BodyStr)
Log("HTTP " & resp.StatusCode & " " & sendError.StringOf("name") & " " & sendError.StringOf("details[0].issue"))
Log("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
Log("No HTTP response: " & http.LastErrorText)
Return
End If
If (resp.StatusCode < 200) Or (resp.StatusCode >= 300) Then
Dim invoiceError As ChilkatJsonObject
invoiceError.Initialize
invoiceError.Load(resp.BodyStr)
Log("HTTP " & resp.StatusCode & " " & invoiceError.StringOf("name") & " " & invoiceError.StringOf("details[0].issue"))
Log("PayPal debug ID: " & invoiceError.StringOf("debug_id"))
Return
End If
Dim invoice As ChilkatJsonObject
invoice.Initialize
Dim parsedInvoice As Boolean = invoice.Load(resp.BodyStr)
If parsedInvoice <> True Then
Log("Expected a JSON response; HTTP " & 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
Log("Customer payment page: " & invoice.StringOf("detail.metadata.recipient_view_url"))
Return
End If
Log("Invoice is " & invoice.StringOf("status") & ". Retrieve it again when payable.")