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

Visual Basic 6.0
Dim success As Long
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.
Dim http As New ChilkatHttp
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
Dim resp As New ChilkatHttpResponse

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
    Debug.Print "No HTTP response: " & http.LastErrorText
    Exit Sub
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Dim errorInfo As New ChilkatJsonObject
    success = errorInfo.Load(resp.BodyStr)
    Debug.Print "HTTP " & resp.StatusCode & " " & errorInfo.StringOf("name") & " " & errorInfo.StringOf("details[0].issue")
    Debug.Print "PayPal debug ID: " & errorInfo.StringOf("debug_id")
    Exit Sub
End If

Dim result As New ChilkatJsonObject
Dim parsedResult As Long
parsedResult = result.Load(resp.BodyStr)
If (parsedResult <> 1) Then
    Debug.Print "Expected a JSON response; HTTP " & resp.StatusCode
    Exit Sub
End If

If (result.StringOfEquals("status","DRAFT",1) <> 1) Then
    Debug.Print "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 CkDateTime
success = now.SetFromCurrentSystemTime()
Dim stamp As New ChilkatStringBuilder
success = stamp.Append(result.StringOf("detail.invoice_date"))
success = stamp.Append("T00:00:00Z")
Dim issue As New CkDateTime
Dim validDate As Long
validDate = issue.SetFromTimestamp(stamp.GetAsString())
If ((validDate <> 1) Or (now.DiffSeconds(issue) < 0)) Then
    Debug.Print "Select a draft with an issue date of today or earlier."
    Exit Sub
End If

Dim body As New ChilkatJsonObject
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
    Debug.Print "No HTTP response: " & http.LastErrorText
    Exit Sub
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Dim sendError As New ChilkatJsonObject
    success = sendError.Load(resp.BodyStr)
    Debug.Print "HTTP " & resp.StatusCode & " " & sendError.StringOf("name") & " " & sendError.StringOf("details[0].issue")
    Debug.Print "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 <> 1) Then
    Debug.Print "No HTTP response: " & http.LastErrorText
    Exit Sub
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Dim invoiceError As New ChilkatJsonObject
    success = invoiceError.Load(resp.BodyStr)
    Debug.Print "HTTP " & resp.StatusCode & " " & invoiceError.StringOf("name") & " " & invoiceError.StringOf("details[0].issue")
    Debug.Print "PayPal debug ID: " & invoiceError.StringOf("debug_id")
    Exit Sub
End If

Dim invoice As New ChilkatJsonObject
Dim parsedInvoice As Long
parsedInvoice = invoice.Load(resp.BodyStr)
If (parsedInvoice <> 1) Then
    Debug.Print "Expected a JSON response; HTTP " & resp.StatusCode
    Exit Sub
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
    Debug.Print "Customer payment page: " & invoice.StringOf("detail.metadata.recipient_view_url")
    Exit Sub
End If

Debug.Print "Invoice is " & invoice.StringOf("status") & ". Retrieve it again when payable."