Swift Requires Chilkat v11.0.0+
Swift
PayPal: Record a bank transfer against an invoice
See more PayPal Examples
Record USD 45 already received by bank transfer. The invoice may still have a balance. This makes a bookkeeping entry; it does not debit the customer. Save payment_id for reconciliation.
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()!
// Record only a bank transfer you have already received. Reconcile first if a previous
// attempt timed out.
http.setUrlVar(name: "id", value: "INVOICE_ID")
let body = CkoJsonObject()!
body.updateString(jsonPath: "method", value: "BANK_TRANSFER")
body.updateString(jsonPath: "amount.currency_code", value: "USD")
body.updateString(jsonPath: "amount.value", value: "45.00")
// Use the actual date of the transfer; this example records one made today.
let date = CkoDateTime()!
date.setFromCurrentSystemTime()
body.updateString(jsonPath: "payment_date", value: date.get(asIso8601: "YYYY-MM-DD", bLocal: false))
success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments", 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 parsed: Bool = result.load(json: resp.bodyStr)
if parsed != true {
print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
return
}
print("Record ID: \(result.string(of: "payment_id")!)")
}