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

PayPal: Get a payable invoice link for your own email

See more PayPal Examples

Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.

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 parsedResult: Bool = result.load(json: resp.bodyStr)
    if parsedResult != true {
        print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
        return
    }

    if result.string(ofEquals: "status", value: "DRAFT", caseSensitive: true) != true {
        print("Select an unsent draft.")
        return
    }

    //  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
    let now = CkoDateTime()!
    now.setFromCurrentSystemTime()
    let stamp = CkoStringBuilder()!
    stamp.append(value: result.string(of: "detail.invoice_date"))
    stamp.append(value: "T00:00:00Z")
    let issue = CkoDateTime()!
    var validDate: Bool = issue.set(fromTimestamp: stamp.getAsString())
    if (validDate != true) || (now.diffSeconds(dateTimeArg: issue).intValue < 0) {
        print("Select a draft with an issue date of today or earlier.")
        return
    }

    let body = CkoJsonObject()!
    body.updateBool(jsonPath: "send_to_recipient", value: false)
    body.updateBool(jsonPath: "send_to_invoicer", value: false)
    success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send", 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 sendError = CkoJsonObject()!
        sendError.load(json: resp.bodyStr)
        print("HTTP \(resp.statusCode.intValue) \(sendError.string(of: "name")!) \(sendError.string(of: "details[0].issue")!)")
        print("PayPal debug ID: \(sendError.string(of: "debug_id")!)")
        return
    }

    //  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
    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 invoiceError = CkoJsonObject()!
        invoiceError.load(json: resp.bodyStr)
        print("HTTP \(resp.statusCode.intValue) \(invoiceError.string(of: "name")!) \(invoiceError.string(of: "details[0].issue")!)")
        print("PayPal debug ID: \(invoiceError.string(of: "debug_id")!)")
        return
    }

    let invoice = CkoJsonObject()!
    var parsedInvoice: Bool = invoice.load(json: resp.bodyStr)
    if parsedInvoice != true {
        print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
        return
    }

    if ((invoice.string(ofEquals: "status", value: "SENT", caseSensitive: true) == true) || (invoice.string(ofEquals: "status", value: "UNPAID", caseSensitive: true) == true) || (invoice.string(ofEquals: "status", value: "SHARED", caseSensitive: true) == true)) && (invoice.hasMember(jsonPath: "detail.metadata.recipient_view_url") == true) {
        print("Customer payment page: \(invoice.string(of: "detail.metadata.recipient_view_url")!)")
        return
    }

    print("Invoice is \(invoice.string(of: "status")!). Retrieve it again when payable.")

}