Swift Requires Chilkat v11.0.0+
Swift
PayPal: Partially capture an authorization
See more PayPal Examples
Capture USD 10 of the sample USD 30 authorization and leave the rest open. Multiple captures require merchant eligibility. Use a separate request ID for each intended capture.
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()!
// Keep final_capture false if another shipment will be billed against this hold.
http.setUrlVar(name: "id", value: "AUTHORIZATION_ID")
// Create and persist this key once per business operation. On a timeout,
// retry the same request with its saved key, not a newly generated one.
let crypt = CkoCrypt2()!
var requestId: String? = crypt.generateUuid()
http.setRequestHeader(name: "PayPal-Request-Id", value: requestId)
http.setRequestHeader(name: "Prefer", value: "return=representation")
let body = CkoJsonObject()!
body.updateString(jsonPath: "amount.currency_code", value: "USD")
body.updateString(jsonPath: "amount.value", value: "10.00")
body.updateBool(jsonPath: "final_capture", value: false)
success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/payments/authorizations/{$id}/capture", 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("ID: \(result.string(of: "id")!)")
print("Status: \(result.string(of: "status")!)")
print("Amount: \(result.string(of: "amount.value")!)")
print("Currency: \(result.string(of: "amount.currency_code")!)")
}