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

PayPal: Find invoices for a recipient

See more PayPal Examples

Filter by a sandbox recipient email address in the JSON body. Pagination belongs in the URL. Read every matching invoice ID; several invoices can share the same recipient.

PayPal documentation

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oBody
LOCAL nPage
LOCAL nLastPage
LOCAL oUrl
LOCAL oErrorInfo
LOCAL oResult
LOCAL nParsed
LOCAL i
LOCAL nCount

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")

//  Use a JSON search body on every page; the recipient email does not belong in the
//  URL.
oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("recipient_email", "buyer@example.com")
nPage := 1
nLastPage := 1
DO WHILE (nPage <= nLastPage)
    oUrl := CreateObject("Chilkat.StringBuilder")
    oUrl:Append("https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=20&page=")
    oUrl:AppendInt(nPage)
    nSuccess := oHttp:HttpJson("POST", oUrl:GetAsString(), oBody, "application/json", oResp)
    IF (nSuccess != 1)
        ? "No HTTP response: " + oHttp:LastErrorText
        oHttp:destroy()
        oResp:destroy()
        oBody:destroy()
        oUrl: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()
        oBody:destroy()
        oUrl: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()
        oBody:destroy()
        oUrl:destroy()
        oErrorInfo:destroy()
        oResult:destroy()
        RETURN
    ENDIF

    i := 0
    nCount := oResult:SizeOfArray("items")
    DO WHILE (i < nCount)
        oResult:I := i
        ? oResult:StringOf("items[i].id") + "  " + oResult:StringOf("items[i].detail.invoice_number") + "  " + oResult:StringOf("items[i].status")
        i := i + 1
    ENDDO
    nLastPage := oResult:IntOf("total_pages")
    nPage := nPage + 1
ENDDO

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