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

PayPal: Revise a draft invoice from its full billing record

See more PayPal Examples

PUT replaces the invoice. This example revises the quantity on the simple two-inspection draft to three. Rebuild the complete editable document from your billing record; omitted items, terms or recipients can be lost. Do not use this body for an unrelated 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()!

    //  Use only the ID of the matching draft from the create example, while it is still a
    //  draft. Preserve all of your real draft fields, including any custom invoice number.
    http.setUrlVar(name: "id", value: "INVOICE_ID")
    http.setRequestHeader(name: "Prefer", value: "return=representation")
    let body = CkoJsonObject()!
    body.updateString(jsonPath: "detail.currency_code", value: "USD")
    body.updateString(jsonPath: "detail.payment_term.term_type", value: "NET_30")
    body.updateString(jsonPath: "primary_recipients[0].billing_info.email_address", value: "buyer@example.com")
    body.updateString(jsonPath: "items[0].name", value: "Equipment inspection")
    body.updateString(jsonPath: "items[0].quantity", value: "3")
    body.updateString(jsonPath: "items[0].unit_amount.currency_code", value: "USD")
    body.updateString(jsonPath: "items[0].unit_amount.value", value: "45.00")

    success = http.httpJson(verb: "PUT", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", 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")!)")
    print("New total: \(result.string(of: "amount.value")!)")
    print("Currency: \(result.string(of: "amount.currency_code")!)")

}