Swift Requires Chilkat v11.0.0+
Swift
PayPal: Resume a suspended subscription
See more PayPal Examples
Resume billing on a separate operator request after verifying the subscription is suspended. Save the reason with the customer support record.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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.
let http = CkoHttp()!
http.authToken = "SANDBOX_ACCESS_TOKEN"
let resp = CkoHttpResponse()!
http.setUrlVar(name: "id", value: "SUBSCRIPTION_ID")
success = http.httpNoBody(verb: "GET", url: "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}", response: resp)
if success != true {
print("No HTTP response: \(http.lastErrorText!)")
return
}
if (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
let errorInfo = CkoJsonObject()!
errorInfo.load(json: resp.bodyStr)
print("HTTP \(resp.statusCode.intValue) \(errorInfo.string(of: "name")!) \(errorInfo.string(of: "details[0].issue")!)")
print("PayPal debug ID: \(errorInfo.string(of: "debug_id")!)")
return
}
let result = CkoJsonObject()!
var parsedResult: Bool = result.load(json: resp.bodyStr)
if parsedResult != true {
print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
return
}
if result.string(ofEquals: "status", value: "SUSPENDED", caseSensitive: true) != true {
print("Subscription status is \(result.string(of: "status")!). No change made.")
return
}
let body = CkoJsonObject()!
body.updateString(jsonPath: "reason", value: "Customer requested that billing resume.")
success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/activate", json: body, contentType: "application/json", response: resp)
if success != true {
print("No HTTP response: \(http.lastErrorText!)")
return
}
if (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
let changeError = CkoJsonObject()!
changeError.load(json: resp.bodyStr)
print("HTTP \(resp.statusCode.intValue) \(changeError.string(of: "name")!) \(changeError.string(of: "details[0].issue")!)")
print("PayPal debug ID: \(changeError.string(of: "debug_id")!)")
return
}
print("PayPal accepted the subscription change.")
}