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

PayPal: Update the price on a reusable payment link

See more PayPal Examples

Retrieve the simple USD inspection link from the create example. Check its item and currency, retain its configuration, and replace the price with USD 95. PUT sends the complete editable definition.

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

    http.setUrlVar(name: "id", value: "PAYMENT_RESOURCE_ID")

    success = http.httpNoBody(verb: "GET", url: "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}", 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
    }

    if (result.size(ofArray: "line_items").intValue != 1) || (result.string(ofEquals: "line_items[0].name", value: "Annual inspection", caseSensitive: true) != true) || (result.string(ofEquals: "line_items[0].unit_amount.currency_code", value: "USD", caseSensitive: true) != true) {
        print("Select the USD inspection link from your catalog.")
        return
    }

    //  Preserve line items, taxes, shipping and checkout settings from the retrieved link.
    //  Remove the resource metadata PayPal added to the GET response.
    if result.hasMember(jsonPath: "id") == true {
        result.delete(name: "id")
    }

    if result.hasMember(jsonPath: "create_time") == true {
        result.delete(name: "create_time")
    }

    if result.hasMember(jsonPath: "update_time") == true {
        result.delete(name: "update_time")
    }

    if result.hasMember(jsonPath: "status") == true {
        result.delete(name: "status")
    }

    if result.hasMember(jsonPath: "links") == true {
        result.delete(name: "links")
    }

    if result.hasMember(jsonPath: "payment_link") == true {
        result.delete(name: "payment_link")
    }

    result.updateString(jsonPath: "line_items[0].unit_amount.value", value: "95.00")
    success = http.httpJson(verb: "PUT", url: "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}", json: result, 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 updateError = CkoJsonObject()!
        updateError.load(json: resp.bodyStr)
        print("HTTP \(resp.statusCode.intValue) \(updateError.string(of: "name")!) \(updateError.string(of: "details[0].issue")!)")
        print("PayPal debug ID: \(updateError.string(of: "debug_id")!)")
        return
    }

    print("The inspection price is now USD 95.00.")

}