Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

PayPal: Void an unused authorization

See more PayPal Examples

Release an unused hold with the authorization ID. A fully captured authorization cannot be voided; refund its capture instead. A successful void can have an empty response body.

PayPal documentation

Chilkat Swift Downloads

Swift

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 original authorization ID to release the hold; do not send an amount.

    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)

    success = http.httpNoBody(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/payments/authorizations/{$id}/void", 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
    }

    print("Authorization void accepted; HTTP \(resp.statusCode.intValue)")

}