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