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

PayPal: Authorize a buyer-approved order

See more PayPal Examples

Authorize the stored order after the buyer approves it. Save the returned authorization ID and amount for capture or void. An authorization reserves funds; it is not a capture.

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 approved order created with intent AUTHORIZE.
    http.setUrlVar(name: "id", value: "ORDER_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()!

    success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/checkout/orders/{$id}/authorize", 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")!)")
    //  A purchase unit may have no authorizations yet, or more than one.
    var i: Int = 0
    while (i < result.size(ofArray: "purchase_units").intValue) {
        result.i = i
        var j: Int = 0
        while (j < result.size(ofArray: "purchase_units[i].payments.authorizations").intValue) {
            result.j = j
            print("Unit: \(result.string(of: "purchase_units[i].reference_id")!)  authorization: \(result.string(of: "purchase_units[i].payments.authorizations[j].id")!)")
            print("  \(result.string(of: "purchase_units[i].payments.authorizations[j].status")!)  \(result.string(of: "purchase_units[i].payments.authorizations[j].amount.value")!) \(result.string(of: "purchase_units[i].payments.authorizations[j].amount.currency_code")!)")
            j = j + 1
        }

        i = i + 1
    }


}