PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
oleobject loo_ErrorInfo
oleobject loo_Result
long li_ParsedResult
oleobject loo_Now
oleobject loo_Stamp
oleobject loo_Issue
long li_ValidDate
oleobject loo_Body
oleobject loo_SendError
oleobject loo_InvoiceError
oleobject loo_Invoice
long li_ParsedInvoice
li_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.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.AuthToken = "SANDBOX_ACCESS_TOKEN"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Http.SetUrlVar("id","INVOICE_ID")
li_Success = loo_Http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",loo_Resp)
if li_Success <> 1 then
Write-Debug "No HTTP response: " + loo_Http.LastErrorText
destroy loo_Http
destroy loo_Resp
return
end if
if (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
loo_ErrorInfo = create oleobject
li_rc = loo_ErrorInfo.ConnectToNewObject("Chilkat.JsonObject")
loo_ErrorInfo.Load(loo_Resp.BodyStr)
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + " " + loo_ErrorInfo.StringOf("name") + " " + loo_ErrorInfo.StringOf("details[0].issue")
Write-Debug "PayPal debug ID: " + loo_ErrorInfo.StringOf("debug_id")
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
return
end if
loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")
li_ParsedResult = loo_Result.Load(loo_Resp.BodyStr)
if li_ParsedResult <> 1 then
Write-Debug "Expected a JSON response; HTTP " + string(loo_Resp.StatusCode)
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
return
end if
if loo_Result.StringOfEquals("status","DRAFT",1) <> 1 then
Write-Debug "Select an unsent draft."
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
return
end if
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")
loo_Now.SetFromCurrentSystemTime()
loo_Stamp = create oleobject
li_rc = loo_Stamp.ConnectToNewObject("Chilkat.StringBuilder")
loo_Stamp.Append(loo_Result.StringOf("detail.invoice_date"))
loo_Stamp.Append("T00:00:00Z")
loo_Issue = create oleobject
li_rc = loo_Issue.ConnectToNewObject("Chilkat.CkDateTime")
li_ValidDate = loo_Issue.SetFromTimestamp(loo_Stamp.GetAsString())
if (li_ValidDate <> 1) OR (loo_Now.DiffSeconds(loo_Issue) < 0) then
Write-Debug "Select a draft with an issue date of today or earlier."
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
return
end if
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")
loo_Body.UpdateBool("send_to_recipient",0)
loo_Body.UpdateBool("send_to_invoicer",0)
li_Success = loo_Http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",loo_Body,"application/json",loo_Resp)
if li_Success <> 1 then
Write-Debug "No HTTP response: " + loo_Http.LastErrorText
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
destroy loo_Body
return
end if
if (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
loo_SendError = create oleobject
li_rc = loo_SendError.ConnectToNewObject("Chilkat.JsonObject")
loo_SendError.Load(loo_Resp.BodyStr)
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + " " + loo_SendError.StringOf("name") + " " + loo_SendError.StringOf("details[0].issue")
Write-Debug "PayPal debug ID: " + loo_SendError.StringOf("debug_id")
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
destroy loo_Body
destroy loo_SendError
return
end if
// Read back the invoice: acceptance of the send alone does not guarantee it is payable.
li_Success = loo_Http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",loo_Resp)
if li_Success <> 1 then
Write-Debug "No HTTP response: " + loo_Http.LastErrorText
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
destroy loo_Body
destroy loo_SendError
return
end if
if (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
loo_InvoiceError = create oleobject
li_rc = loo_InvoiceError.ConnectToNewObject("Chilkat.JsonObject")
loo_InvoiceError.Load(loo_Resp.BodyStr)
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + " " + loo_InvoiceError.StringOf("name") + " " + loo_InvoiceError.StringOf("details[0].issue")
Write-Debug "PayPal debug ID: " + loo_InvoiceError.StringOf("debug_id")
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
destroy loo_Body
destroy loo_SendError
destroy loo_InvoiceError
return
end if
loo_Invoice = create oleobject
li_rc = loo_Invoice.ConnectToNewObject("Chilkat.JsonObject")
li_ParsedInvoice = loo_Invoice.Load(loo_Resp.BodyStr)
if li_ParsedInvoice <> 1 then
Write-Debug "Expected a JSON response; HTTP " + string(loo_Resp.StatusCode)
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
destroy loo_Body
destroy loo_SendError
destroy loo_InvoiceError
destroy loo_Invoice
return
end if
if ((loo_Invoice.StringOfEquals("status","SENT",1) = 1) OR (loo_Invoice.StringOfEquals("status","UNPAID",1) = 1) OR (loo_Invoice.StringOfEquals("status","SHARED",1) = 1)) AND (loo_Invoice.HasMember("detail.metadata.recipient_view_url") = 1) then
Write-Debug "Customer payment page: " + loo_Invoice.StringOf("detail.metadata.recipient_view_url")
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
destroy loo_Body
destroy loo_SendError
destroy loo_InvoiceError
destroy loo_Invoice
return
end if
Write-Debug "Invoice is " + loo_Invoice.StringOf("status") + ". Retrieve it again when payable."
destroy loo_Http
destroy loo_Resp
destroy loo_ErrorInfo
destroy loo_Result
destroy loo_Now
destroy loo_Stamp
destroy loo_Issue
destroy loo_Body
destroy loo_SendError
destroy loo_InvoiceError
destroy loo_Invoice