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

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsedResult
LOCAL loNow
LOCAL loStamp
LOCAL loIssue
LOCAL lnValidDate
LOCAL loBody
LOCAL loSendError
LOCAL loInvoiceError
LOCAL loInvoice
LOCAL lnParsedInvoice

lnSuccess = 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.
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
loResp = CreateObject('Chilkat.HttpResponse')

loHttp.SetUrlVar("id","INVOICE_ID")

lnSuccess = loHttp.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loErrorInfo = CreateObject('Chilkat.JsonObject')
    loErrorInfo.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loErrorInfo.StringOf("name") + " " + loErrorInfo.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loErrorInfo.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.JsonObject')
lnParsedResult = loResult.Load(loResp.BodyStr)
IF (lnParsedResult <> 1) THEN
    ? "Expected a JSON response; HTTP " + STR(loResp.StatusCode)
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

IF (loResult.StringOfEquals("status","DRAFT",1) <> 1) THEN
    ? "Select an unsent draft."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

*  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
loStamp = CreateObject('Chilkat.StringBuilder')
loStamp.Append(loResult.StringOf("detail.invoice_date"))
loStamp.Append("T00:00:00Z")
loIssue = CreateObject('Chilkat.CkDateTime')
lnValidDate = loIssue.SetFromTimestamp(loStamp.GetAsString())
IF ((lnValidDate <> 1) OR (loNow.DiffSeconds(loIssue) < 0)) THEN
    ? "Select a draft with an issue date of today or earlier."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loStamp
    RELEASE loIssue
    CANCEL
ENDIF

loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateBool("send_to_recipient",0)
loBody.UpdateBool("send_to_invoicer",0)
lnSuccess = loHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",loBody,"application/json",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loStamp
    RELEASE loIssue
    RELEASE loBody
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loSendError = CreateObject('Chilkat.JsonObject')
    loSendError.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loSendError.StringOf("name") + " " + loSendError.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loSendError.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loStamp
    RELEASE loIssue
    RELEASE loBody
    RELEASE loSendError
    CANCEL
ENDIF

*  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
lnSuccess = loHttp.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loStamp
    RELEASE loIssue
    RELEASE loBody
    RELEASE loSendError
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loInvoiceError = CreateObject('Chilkat.JsonObject')
    loInvoiceError.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loInvoiceError.StringOf("name") + " " + loInvoiceError.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loInvoiceError.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loStamp
    RELEASE loIssue
    RELEASE loBody
    RELEASE loSendError
    RELEASE loInvoiceError
    CANCEL
ENDIF

loInvoice = CreateObject('Chilkat.JsonObject')
lnParsedInvoice = loInvoice.Load(loResp.BodyStr)
IF (lnParsedInvoice <> 1) THEN
    ? "Expected a JSON response; HTTP " + STR(loResp.StatusCode)
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loStamp
    RELEASE loIssue
    RELEASE loBody
    RELEASE loSendError
    RELEASE loInvoiceError
    RELEASE loInvoice
    CANCEL
ENDIF

IF (((loInvoice.StringOfEquals("status","SENT",1) = 1) OR (loInvoice.StringOfEquals("status","UNPAID",1) = 1) OR (loInvoice.StringOfEquals("status","SHARED",1) = 1)) AND (loInvoice.HasMember("detail.metadata.recipient_view_url") = 1)) THEN
    ? "Customer payment page: " + loInvoice.StringOf("detail.metadata.recipient_view_url")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loStamp
    RELEASE loIssue
    RELEASE loBody
    RELEASE loSendError
    RELEASE loInvoiceError
    RELEASE loInvoice
    CANCEL
ENDIF

? "Invoice is " + loInvoice.StringOf("status") + ". Retrieve it again when payable."

RELEASE loHttp
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
RELEASE loNow
RELEASE loStamp
RELEASE loIssue
RELEASE loBody
RELEASE loSendError
RELEASE loInvoiceError
RELEASE loInvoice