Sample code for 30+ languages & platforms
Go 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 Go Downloads

Go
    success := 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.
    http := chilkat.NewHttp()
    http.SetAuthToken("SANDBOX_ACCESS_TOKEN")
    resp := chilkat.NewHttpResponse()

    http.SetUrlVar("id","INVOICE_ID")

    success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
    if success != true {
        fmt.Println("No HTTP response: ", http.LastErrorText())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        return
    }

    if (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
        errorInfo := chilkat.NewJsonObject()
        errorInfo.Load(resp.BodyStr())
        fmt.Println("HTTP ", resp.StatusCode(), " ", *errorInfo.StringOf("name"), " ", *errorInfo.StringOf("details[0].issue"))
        fmt.Println("PayPal debug ID: ", *errorInfo.StringOf("debug_id"))
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        return
    }

    result := chilkat.NewJsonObject()
    parsedResult := result.Load(resp.BodyStr())
    if parsedResult != true {
        fmt.Println("Expected a JSON response; HTTP ", resp.StatusCode())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        return
    }

    if result.StringOfEquals("status","DRAFT",true) != true {
        fmt.Println("Select an unsent draft.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        return
    }

    //  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
    now := chilkat.NewCkDateTime()
    now.SetFromCurrentSystemTime()
    stamp := chilkat.NewStringBuilder()
    stamp.Append(*result.StringOf("detail.invoice_date"))
    stamp.Append("T00:00:00Z")
    issue := chilkat.NewCkDateTime()
    validDate := issue.SetFromTimestamp(*stamp.GetAsString())
    if (validDate != true) || (now.DiffSeconds(issue) < 0) {
        fmt.Println("Select a draft with an issue date of today or earlier.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        now.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        issue.DisposeCkDateTime()
        return
    }

    body := chilkat.NewJsonObject()
    body.UpdateBool("send_to_recipient",false)
    body.UpdateBool("send_to_invoicer",false)
    success = http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",body,"application/json",resp)
    if success != true {
        fmt.Println("No HTTP response: ", http.LastErrorText())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        now.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        issue.DisposeCkDateTime()
        body.DisposeJsonObject()
        return
    }

    if (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
        sendError := chilkat.NewJsonObject()
        sendError.Load(resp.BodyStr())
        fmt.Println("HTTP ", resp.StatusCode(), " ", *sendError.StringOf("name"), " ", *sendError.StringOf("details[0].issue"))
        fmt.Println("PayPal debug ID: ", *sendError.StringOf("debug_id"))
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        now.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        issue.DisposeCkDateTime()
        body.DisposeJsonObject()
        sendError.DisposeJsonObject()
        return
    }

    //  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
    success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
    if success != true {
        fmt.Println("No HTTP response: ", http.LastErrorText())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        now.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        issue.DisposeCkDateTime()
        body.DisposeJsonObject()
        sendError.DisposeJsonObject()
        return
    }

    if (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
        invoiceError := chilkat.NewJsonObject()
        invoiceError.Load(resp.BodyStr())
        fmt.Println("HTTP ", resp.StatusCode(), " ", *invoiceError.StringOf("name"), " ", *invoiceError.StringOf("details[0].issue"))
        fmt.Println("PayPal debug ID: ", *invoiceError.StringOf("debug_id"))
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        now.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        issue.DisposeCkDateTime()
        body.DisposeJsonObject()
        sendError.DisposeJsonObject()
        invoiceError.DisposeJsonObject()
        return
    }

    invoice := chilkat.NewJsonObject()
    parsedInvoice := invoice.Load(resp.BodyStr())
    if parsedInvoice != true {
        fmt.Println("Expected a JSON response; HTTP ", resp.StatusCode())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        now.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        issue.DisposeCkDateTime()
        body.DisposeJsonObject()
        sendError.DisposeJsonObject()
        invoiceError.DisposeJsonObject()
        invoice.DisposeJsonObject()
        return
    }

    if ((invoice.StringOfEquals("status","SENT",true) == true) || (invoice.StringOfEquals("status","UNPAID",true) == true) || (invoice.StringOfEquals("status","SHARED",true) == true)) && (invoice.HasMember("detail.metadata.recipient_view_url") == true) {
        fmt.Println("Customer payment page: ", *invoice.StringOf("detail.metadata.recipient_view_url"))
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        now.DisposeCkDateTime()
        stamp.DisposeStringBuilder()
        issue.DisposeCkDateTime()
        body.DisposeJsonObject()
        sendError.DisposeJsonObject()
        invoiceError.DisposeJsonObject()
        invoice.DisposeJsonObject()
        return
    }

    fmt.Println("Invoice is ", *invoice.StringOf("status"), ". Retrieve it again when payable.")

    http.DisposeHttp()
    resp.DisposeHttpResponse()
    errorInfo.DisposeJsonObject()
    result.DisposeJsonObject()
    now.DisposeCkDateTime()
    stamp.DisposeStringBuilder()
    issue.DisposeCkDateTime()
    body.DisposeJsonObject()
    sendError.DisposeJsonObject()
    invoiceError.DisposeJsonObject()
    invoice.DisposeJsonObject()