Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

PayPal: Read an invoice and its outstanding balance

See more PayPal Examples

Use the PayPal invoice ID to retrieve its number, status, total and balance due. A partial external payment can reduce the balance without settling the whole invoice.

PayPal documentation

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsed

nSuccess := 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.
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := "SANDBOX_ACCESS_TOKEN"
oResp := CreateObject("Chilkat.HttpResponse")

//  The original total and the balance still due may differ after a payment.

oHttp:SetUrlVar("id", "INVOICE_ID")

nSuccess := oHttp:HttpNoBody("GET", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", oResp)
IF (nSuccess != 1)
    ? "No HTTP response: " + oHttp:LastErrorText
    oHttp:destroy()
    oResp:destroy()
    RETURN
ENDIF

IF ((oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
    oErrorInfo := CreateObject("Chilkat.JsonObject")
    oErrorInfo:Load(oResp:BodyStr)
    ? "HTTP " + Str(oResp:StatusCode) + " " + oErrorInfo:StringOf("name") + " " + oErrorInfo:StringOf("details[0].issue")
    ? "PayPal debug ID: " + oErrorInfo:StringOf("debug_id")
    oHttp:destroy()
    oResp:destroy()
    oErrorInfo:destroy()
    RETURN
ENDIF

oResult := CreateObject("Chilkat.JsonObject")
nParsed := oResult:Load(oResp:BodyStr)
IF (nParsed != 1)
    ? "Expected a JSON response; HTTP " + Str(oResp:StatusCode)
    oHttp:destroy()
    oResp:destroy()
    oErrorInfo:destroy()
    oResult:destroy()
    RETURN
ENDIF

? "ID: " + oResult:StringOf("id")
? "Status: " + oResult:StringOf("status")
? "Number: " + oResult:StringOf("detail.invoice_number")
? "Total: " + oResult:StringOf("amount.value")
? "Currency: " + oResult:StringOf("amount.currency_code")
? "Balance due: " + oResult:StringOf("due_amount.value")

oHttp:destroy()
oResp:destroy()
oErrorInfo:destroy()
oResult:destroy()