Swift Requires Chilkat v11.0.0+
Swift
PayPal: List merchant invoices across pages
See more PayPal Examples
List invoices for the authenticated merchant. Request totals, iterate every page, and keep the PayPal ID alongside the human-readable number and current status.
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()!
// Request total_pages so a merchant with more than one page sees every invoice.
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/invoices?total_required=true&page_size=20&page=")
url.appendInt(value: page)
success = http.httpNoBody(verb: "GET", url: url.getAsString(), 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 parsed: Bool = result.load(json: resp.bodyStr)
if parsed != 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].id")!) \(result.string(of: "items[i].detail.invoice_number")!) \(result.string(of: "items[i].status")!)")
i = i + 1
}
lastPage = result.int(of: "total_pages").intValue
page = page + 1
}
}