DataFlex Requires Chilkat v11.0.0+
DataFlex
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.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoErrorInfo
Handle hoResult
Boolean iParsedResult
Handle hoNow
Variant vQuery
Handle hoQuery
Handle hoHistoryError
Handle hoHistory
Boolean iParsedHistory
Integer i
String sTemp1
String sTemp2
String sTemp3
String sTemp4
String sTemp5
Integer iTemp1
Integer iTemp2
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComAuthToken Of hoHttp To "SANDBOX_ACCESS_TOKEN"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get ComSetUrlVar Of hoHttp "id" "SUBSCRIPTION_ID" To iSuccess
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}" vResp To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln "No HTTP response: " sTemp1
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoErrorInfo
If (Not(IsComObjectCreated(hoErrorInfo))) Begin
Send CreateComObject of hoErrorInfo
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoErrorInfo sTemp1 To iSuccess
Get ComStatusCode Of hoResp To iTemp1
Get ComStringOf Of hoErrorInfo "name" To sTemp1
Get ComStringOf Of hoErrorInfo "details[0].issue" To sTemp2
Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
Get ComStringOf Of hoErrorInfo "debug_id" To sTemp1
Showln "PayPal debug ID: " sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iParsedResult
If (iParsedResult <> True) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Expected a JSON response; HTTP " iTemp1
Procedure_Return
End
Get ComStringOf Of hoResult "status" To sTemp1
Showln "Status: " sTemp1
Get ComStringOf Of hoResult "billing_info.next_billing_time" To sTemp1
Showln "Next billing: " sTemp1
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoQuery
If (Not(IsComObjectCreated(hoQuery))) Begin
Send CreateComObject of hoQuery
End
Get ComGetAsTimestamp Of hoNow False To sTemp1
Get ComUpdateString Of hoQuery "end_time" sTemp1 To iSuccess
Get ComAddDays Of hoNow -30 To iSuccess
Get ComGetAsTimestamp Of hoNow False To sTemp1
Get ComUpdateString Of hoQuery "start_time" sTemp1 To iSuccess
Get pvComObject of hoQuery to vQuery
Get pvComObject of hoResp to vResp
Get ComHttpParams Of hoHttp "GET" "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions" vQuery vResp To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln "No HTTP response: " sTemp1
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoHistoryError
If (Not(IsComObjectCreated(hoHistoryError))) Begin
Send CreateComObject of hoHistoryError
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoHistoryError sTemp1 To iSuccess
Get ComStatusCode Of hoResp To iTemp1
Get ComStringOf Of hoHistoryError "name" To sTemp1
Get ComStringOf Of hoHistoryError "details[0].issue" To sTemp2
Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
Get ComStringOf Of hoHistoryError "debug_id" To sTemp1
Showln "PayPal debug ID: " sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoHistory
If (Not(IsComObjectCreated(hoHistory))) Begin
Send CreateComObject of hoHistory
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoHistory sTemp1 To iParsedHistory
If (iParsedHistory <> True) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Expected a JSON response; HTTP " iTemp1
Procedure_Return
End
Move 0 To i
While (i < (ComSizeOfArray(hoHistory, "transactions")))
Set ComI Of hoHistory To i
Get ComStringOf Of hoHistory "transactions[i].time" To sTemp1
Get ComStringOf Of hoHistory "transactions[i].id" To sTemp2
Get ComStringOf Of hoHistory "transactions[i].status" To sTemp3
Get ComStringOf Of hoHistory "transactions[i].amount_with_breakdown.gross_amount.value" To sTemp4
Get ComStringOf Of hoHistory "transactions[i].amount_with_breakdown.gross_amount.currency_code" To sTemp5
Showln sTemp1 " " sTemp2 " " sTemp3 " " sTemp4 " " sTemp5
Move (i + 1) To i
Loop
End_Procedure