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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
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 = Server.CreateObject("Chilkat.Http")
http.AuthToken = "SANDBOX_ACCESS_TOKEN"
set resp = Server.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
    Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response: " & http.LastErrorText) & "</pre>"
    Response.End
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set errorInfo = Server.CreateObject("Chilkat.JsonObject")
    success = errorInfo.Load(resp.BodyStr)
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & " " & errorInfo.StringOf("name") & " " & errorInfo.StringOf("details[0].issue")) & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( "PayPal debug ID: " & errorInfo.StringOf("debug_id")) & "</pre>"
    Response.End
End If

set result = Server.CreateObject("Chilkat.JsonObject")
parsedResult = result.Load(resp.BodyStr)
If (parsedResult <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a JSON response; HTTP " & resp.StatusCode) & "</pre>"
    Response.End
End If

If (result.StringOfEquals("status","DRAFT",1) <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Select an unsent draft.") & "</pre>"
    Response.End
End If

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

set body = Server.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
    Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response: " & http.LastErrorText) & "</pre>"
    Response.End
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set sendError = Server.CreateObject("Chilkat.JsonObject")
    success = sendError.Load(resp.BodyStr)
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & " " & sendError.StringOf("name") & " " & sendError.StringOf("details[0].issue")) & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( "PayPal debug ID: " & sendError.StringOf("debug_id")) & "</pre>"
    Response.End
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
    Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response: " & http.LastErrorText) & "</pre>"
    Response.End
End If

If ((resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    set invoiceError = Server.CreateObject("Chilkat.JsonObject")
    success = invoiceError.Load(resp.BodyStr)
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & " " & invoiceError.StringOf("name") & " " & invoiceError.StringOf("details[0].issue")) & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( "PayPal debug ID: " & invoiceError.StringOf("debug_id")) & "</pre>"
    Response.End
End If

set invoice = Server.CreateObject("Chilkat.JsonObject")
parsedInvoice = invoice.Load(resp.BodyStr)
If (parsedInvoice <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a JSON response; HTTP " & resp.StatusCode) & "</pre>"
    Response.End
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
    Response.Write "<pre>" & Server.HTMLEncode( "Customer payment page: " & invoice.StringOf("detail.metadata.recipient_view_url")) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Invoice is " & invoice.StringOf("status") & ". Retrieve it again when payable.") & "</pre>"

%>
</body>
</html>