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

PayPal: Save a payable invoice QR code as a PNG

See more PayPal Examples

Generate a 300-pixel payment QR code for a sent, payable invoice. Decode the base64 image into a PNG for a printed invoice; the code opens the payment page, and is not proof of payment.

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

    //  Use a sent, payable invoice. Width and height are JSON numbers, in pixels.

    http.SetUrlVar("id","INVOICE_ID")
    body := chilkat.NewJsonObject()
    body.UpdateInt("width",300)
    body.UpdateInt("height",300)
    body.UpdateString("action","pay")

    success = http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code",body,"application/json",resp)
    if success != true {
        fmt.Println("No HTTP response: ", http.LastErrorText())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        body.DisposeJsonObject()
        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()
        body.DisposeJsonObject()
        errorInfo.DisposeJsonObject()
        return
    }

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

    png := chilkat.NewBinData()
    if result.HasMember("image") != true {
        fmt.Println("QR response did not contain an image.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        body.DisposeJsonObject()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        png.DisposeBinData()
        return
    }

    decoded := png.AppendEncoded(*result.StringOf("image"),"base64")
    if decoded != true {
        fmt.Println("Invalid base64 image.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        body.DisposeJsonObject()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        png.DisposeBinData()
        return
    }

    savedImage := png.WriteFile("paypal-invoice-qr.png")
    if savedImage != true {
        fmt.Println("Could not write the invoice QR image.")
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        body.DisposeJsonObject()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        png.DisposeBinData()
        return
    }

    fmt.Println("Saved paypal-invoice-qr.png.")

    http.DisposeHttp()
    resp.DisposeHttpResponse()
    body.DisposeJsonObject()
    errorInfo.DisposeJsonObject()
    result.DisposeJsonObject()
    png.DisposeBinData()