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

PayPal: Build a worklist of overdue invoices

See more PayPal Examples

Find open invoices due in the past 90 days, including partial payments. Walk all result pages and display the invoice number, due date, and remaining balance for the collections team.

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()!
    let body = CkoJsonObject()!
    body.updateString(jsonPath: "status[0]", value: "SENT")
    body.updateString(jsonPath: "status[1]", value: "PARTIALLY_PAID")
    body.updateString(jsonPath: "status[2]", value: "UNPAID")
    body.updateString(jsonPath: "status[3]", value: "SHARED")
    //  Yesterday is the last overdue date. Include invoices due in the past 90 days.
    let day = CkoDateTime()!
    day.setFromCurrentSystemTime()
    day.addDays(numDays: -1)
    body.updateString(jsonPath: "due_date_range.end", value: day.get(asIso8601: "YYYY-MM-DD", bLocal: false))
    day.addDays(numDays: -89)
    body.updateString(jsonPath: "due_date_range.start", value: day.get(asIso8601: "YYYY-MM-DD", bLocal: false))
    var page: Int = 1
    var lastPage: Int = 1
    while (page <= lastPage) {
        let url = CkoStringBuilder()!
        url.append(value: "https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=")
        url.appendInt(value: page)
        success = http.httpJson(verb: "POST", url: url.getAsString(), 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 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
        }

        var i: Int = 0
        var count: Int = result.size(ofArray: "items").intValue
        while (i < count) {
            result.i = i
            print("\(result.string(of: "items[i].detail.invoice_number")!)  due \(result.string(of: "items[i].detail.payment_term.due_date")!)  \(result.string(of: "items[i].due_amount.value")!) \(result.string(of: "items[i].due_amount.currency_code")!)")
            i = i + 1
        }

        lastPage = result.int(of: "total_pages").intValue
        page = page + 1
    }


}