Xbase++ Requires Chilkat v11.0.0+
Xbase++
PayPal: Show available, withheld and total balances by currency
See more PayPal Examples
Read the merchant balance snapshot using reporting access. Keep each currency separate and show the last refresh time so accounting knows when PayPal last updated the figures.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsedResult
LOCAL i
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")
nSuccess := oHttp:HttpNoBody("GET", "https://api-m.sandbox.paypal.com/v1/reporting/balances", 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")
nParsedResult := oResult:Load(oResp:BodyStr)
IF (nParsedResult != 1)
? "Expected a JSON response; HTTP " + Str(oResp:StatusCode)
oHttp:destroy()
oResp:destroy()
oErrorInfo:destroy()
oResult:destroy()
RETURN
ENDIF
? "Last refresh: " + oResult:StringOf("last_refresh_time")
i := 0
DO WHILE (i < oResult:SizeOfArray("balances"))
oResult:I := i
? oResult:StringOf("balances[i].currency") + " total " + oResult:StringOf("balances[i].total_balance.value") + " available " + oResult:StringOf("balances[i].available_balance.value") + " withheld " + oResult:StringOf("balances[i].withheld_balance.value")
i := i + 1
ENDDO
oHttp:destroy()
oResp:destroy()
oErrorInfo:destroy()
oResult:destroy()