Swift Requires Chilkat v11.0.0+
Swift
PayPal: Find successful Express Checkout transactions
See more PayPal Examples
Filter a reporting window by T0006 (Express Checkout) and S (success). The signed amount distinguishes money received from money sent. This is one transaction class, not all merchant sales; reporting access is required.
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()!
// Use the UTC window containing the transaction (at most 31 days per request).
let dt = CkoDateTime()!
dt.setFromCurrentSystemTime()
let query = CkoJsonObject()!
query.updateString(jsonPath: "end_date", value: dt.get(asTimestamp: false))
dt.addDays(numDays: -1)
query.updateString(jsonPath: "start_date", value: dt.get(asTimestamp: false))
query.updateString(jsonPath: "fields", value: "transaction_info")
query.updateInt(jsonPath: "page_size", value: 100)
query.updateString(jsonPath: "transaction_type", value: "T0006")
query.updateString(jsonPath: "transaction_status", value: "S")
var page: Int = 1
var lastPage: Int = 1
while (page <= lastPage) {
query.updateInt(jsonPath: "page", value: page)
success = http.httpParams(verb: "GET", url: "https://api-m.sandbox.paypal.com/v1/reporting/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 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: "transaction_details").intValue
while (i < count) {
result.i = i
print("\(result.string(of: "transaction_details[i].transaction_info.transaction_id")!) \(result.string(of: "transaction_details[i].transaction_info.transaction_event_code")!) \(result.string(of: "transaction_details[i].transaction_info.transaction_status")!)")
print(" \(result.string(of: "transaction_details[i].transaction_info.transaction_amount.value")!) \(result.string(of: "transaction_details[i].transaction_info.transaction_amount.currency_code")!)")
i = i + 1
}
lastPage = result.int(of: "total_pages").intValue
page = page + 1
}
}