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

PayPal: Cancel a sent, unpaid invoice

See more PayPal Examples

Cancel an unpaid invoice that has already been sent and notify the customer why. For a draft use delete instead. Cancellation does not return funds.

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

    //  This cancels a sent, unpaid invoice. It does not send a refund.

    http.setUrlVar(name: "id", value: "INVOICE_ID")
    let body = CkoJsonObject()!
    body.updateString(jsonPath: "subject", value: "Inspection invoice canceled")
    body.updateString(jsonPath: "note", value: "The inspection was canceled before work began.")
    body.updateBool(jsonPath: "send_to_recipient", value: true)

    success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/cancel", 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
    }

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

}