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

PayPal: Suspend an active subscription

See more PayPal Examples

Pause future billing after checking that the subscription is active. Save the reason with the customer support record.

PayPal documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsedResult
LOCAL loBody
LOCAL loChangeError

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

IF (loResult.StringOfEquals("status","ACTIVE",1) <> 1) THEN
    ? "Subscription status is " + loResult.StringOf("status") + ". No change made."
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateString("reason","Customer requested a billing pause.")
lnSuccess = loHttp.HttpJson("POST","https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/suspend",loBody,"application/json",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loBody
    CANCEL
ENDIF

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

? "PayPal accepted the subscription change."

RELEASE loHttp
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
RELEASE loBody
RELEASE loChangeError