Swift Requires Chilkat v11.0.0+
Swift
PayPal: Remove an external payment recorded on the wrong invoice
See more PayPal Examples
Use the saved external payment record ID. Verify that it belongs to the selected invoice, then remove the bookkeeping entry. The bank transfer itself is unaffected. Record it on the correct invoice as a separate step.
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()!
http.setUrlVar(name: "id", value: "INVOICE_ID")
success = http.httpNoBody(verb: "GET", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$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
}
var paymentId: String? = "EXTERNAL_PAYMENT_ID"
var matched: Bool = false
var i: Int = 0
while (i < result.size(ofArray: "payments.transactions").intValue) {
result.i = i
if (result.string(ofEquals: "payments.transactions[i].payment_id", value: paymentId, caseSensitive: true) == true) && (result.string(ofEquals: "payments.transactions[i].type", value: "EXTERNAL", caseSensitive: true) == true) {
matched = true
}
i = i + 1
}
if matched != true {
print("This invoice has no matching external payment record.")
return
}
http.setUrlVar(name: "paymentId", value: paymentId)
success = http.httpNoBody(verb: "DELETE", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}", response: resp)
if success != true {
print("No HTTP response: \(http.lastErrorText!)")
return
}
if (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
let removeError = CkoJsonObject()!
removeError.load(json: resp.bodyStr)
print("HTTP \(resp.statusCode.intValue) \(removeError.string(of: "name")!) \(removeError.string(of: "details[0].issue")!)")
print("PayPal debug ID: \(removeError.string(of: "debug_id")!)")
return
}
print("External payment entry removed.")
}