Sample code for 30+ languages & platforms
Swift 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 Swift Downloads

Swift

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
    }

    print("Status: \(result.string(of: "status")!)")
    print("Next billing: \(result.string(of: "billing_info.next_billing_time")!)")
    let now = CkoDateTime()!
    now.setFromCurrentSystemTime()
    let query = CkoJsonObject()!
    query.updateString(jsonPath: "end_time", value: now.get(asTimestamp: false))
    now.addDays(numDays: -30)
    query.updateString(jsonPath: "start_time", value: now.get(asTimestamp: false))
    success = http.httpParams(verb: "GET", url: "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions", json: query, response: resp)
    if success != true {
        print("No HTTP response: \(http.lastErrorText!)")
        return
    }

    if (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
        let historyError = CkoJsonObject()!
        historyError.load(json: resp.bodyStr)
        print("HTTP \(resp.statusCode.intValue) \(historyError.string(of: "name")!) \(historyError.string(of: "details[0].issue")!)")
        print("PayPal debug ID: \(historyError.string(of: "debug_id")!)")
        return
    }

    let history = CkoJsonObject()!
    var parsedHistory: Bool = history.load(json: resp.bodyStr)
    if parsedHistory != true {
        print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
        return
    }

    var i: Int = 0
    while (i < history.size(ofArray: "transactions").intValue) {
        history.i = i
        print("\(history.string(of: "transactions[i].time")!)  \(history.string(of: "transactions[i].id")!)  \(history.string(of: "transactions[i].status")!)  \(history.string(of: "transactions[i].amount_with_breakdown.gross_amount.value")!) \(history.string(of: "transactions[i].amount_with_breakdown.gross_amount.currency_code")!)")
        i = i + 1
    }


}