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

PayPal: Look up a subscription and its recent payments

See more PayPal Examples

Start with the PayPal subscription ID saved with the local customer. Display its status and next billing time, then list transactions for the previous 30 days with each gross amount and currency.

PayPal documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsedResult
LOCAL loNow
LOCAL loQuery
LOCAL loHistoryError
LOCAL loHistory
LOCAL lnParsedHistory
LOCAL i

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

lnSuccess = loHttp.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$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

? "Status: " + loResult.StringOf("status")
? "Next billing: " + loResult.StringOf("billing_info.next_billing_time")
loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
loQuery = CreateObject('Chilkat.JsonObject')
loQuery.UpdateString("end_time",loNow.GetAsTimestamp(0))
loNow.AddDays(-30)
loQuery.UpdateString("start_time",loNow.GetAsTimestamp(0))
lnSuccess = loHttp.HttpParams("GET","https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions",loQuery,loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loNow
    RELEASE loQuery
    CANCEL
ENDIF

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

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

i = 0
DO WHILE (i < loHistory.SizeOfArray("transactions"))
    loHistory.I = i
    ? loHistory.StringOf("transactions[i].time") + "  " + loHistory.StringOf("transactions[i].id") + "  " + loHistory.StringOf("transactions[i].status") + "  " + loHistory.StringOf("transactions[i].amount_with_breakdown.gross_amount.value") + " " + loHistory.StringOf("transactions[i].amount_with_breakdown.gross_amount.currency_code")
    i = i + 1
ENDDO

RELEASE loHttp
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
RELEASE loNow
RELEASE loQuery
RELEASE loHistoryError
RELEASE loHistory