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

PayPal: Create a reusable payment link for a service

See more PayPal Examples

Offer an annual inspection for USD 90 through PayPal-hosted checkout. Store the returned resource ID with the catalog item and share the payment_link URL with customers.

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()!

    //  Replace the thank-you URL with your business website. The same link can be shared
    //  with several customers.
    //  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)
    let body = CkoJsonObject()!
    body.updateString(jsonPath: "integration_mode", value: "LINK")
    body.updateString(jsonPath: "type", value: "BUY_NOW")
    body.updateString(jsonPath: "reusable", value: "MULTIPLE")
    body.updateString(jsonPath: "return_url", value: "https://merchant.example/thank-you")
    body.updateString(jsonPath: "line_items[0].name", value: "Annual inspection")
    body.updateString(jsonPath: "line_items[0].unit_amount.currency_code", value: "USD")
    body.updateString(jsonPath: "line_items[0].unit_amount.value", value: "90.00")

    success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources", 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 parsedResult: Bool = result.load(json: resp.bodyStr)
    if parsedResult != true {
        print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
        return
    }

    print("Catalog payment resource ID: \(result.string(of: "id")!)")
    //  Link order is not fixed. Find the customer-facing link to copy into an email.
    var linkIndex: Int = 0
    while (linkIndex < result.size(ofArray: "links").intValue) {
        result.i = linkIndex
        if result.string(ofEquals: "links[i].rel", value: "payment_link", caseSensitive: true) {
            print("Share this payment link: \(result.string(of: "links[i].href")!)")
        }

        linkIndex = linkIndex + 1
    }


}