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

PayPal: Delete an unsent draft invoice

See more PayPal Examples

This example deliberately checks for DRAFT before deletion. Use the invoice ID from your own records. A successful DELETE has no response body; use cancellation for a sent, unpaid invoice.

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: "INVOICE_ID")

    success = http.httpNoBody(verb: "GET", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$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 parsed: Bool = result.load(json: resp.bodyStr)
    if parsed != true {
        print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
        return
    }

    if result.string(ofEquals: "status", value: "DRAFT", caseSensitive: true) != true {
        print("Leaving invoice unchanged: \(result.string(of: "status")!)")
        return
    }

    success = http.httpNoBody(verb: "DELETE", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", response: resp)
    if success != true {
        print("No HTTP response: \(http.lastErrorText!)")
        return
    }

    if (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
        let deleteError = CkoJsonObject()!
        deleteError.load(json: resp.bodyStr)
        print("HTTP \(resp.statusCode.intValue) \(deleteError.string(of: "name")!) \(deleteError.string(of: "details[0].issue")!)")
        print("PayPal debug ID: \(deleteError.string(of: "debug_id")!)")
        return
    }

    print("Draft deleted; HTTP \(resp.statusCode.intValue)")

}