Swift Requires Chilkat v11.0.0+
Swift
PayPal: Start a PayPal wallet checkout
See more PayPal Examples
Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.
Create a USD 30 digital order from a trusted cart. Store the order ID and redirect the buyer to the payer-action link. After approval, capture this same order from your merchant application.
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()!
// Replace the two callback URLs with routes on your site. This order contains no
// physical shipment.
// 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: "intent", value: "CAPTURE")
body.updateString(jsonPath: "purchase_units[0].amount.currency_code", value: "USD")
body.updateString(jsonPath: "purchase_units[0].amount.value", value: "30.00")
body.updateString(jsonPath: "payment_source.paypal.experience_context.return_url", value: "https://merchant.example/paypal/return")
body.updateString(jsonPath: "payment_source.paypal.experience_context.cancel_url", value: "https://merchant.example/paypal/cancel")
body.updateString(jsonPath: "payment_source.paypal.experience_context.shipping_preference", value: "NO_SHIPPING")
success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/checkout/orders", 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")!)")
// Link order is not fixed. Send the buyer to the matching URL in a browser.
var linkIndex: Int = 0
while (linkIndex < result.size(ofArray: "links").intValue) {
result.i = linkIndex
if result.string(ofEquals: "links[i].rel", value: "payer-action", caseSensitive: true) {
print("Approval URL: \(result.string(of: "links[i].href")!)")
}
linkIndex = linkIndex + 1
}
}