C# Requires Chilkat v11.0.0+
C#
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.
Chilkat C# Downloads
bool 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.
Chilkat.Http http = new Chilkat.Http();
http.AuthToken = "SANDBOX_ACCESS_TOKEN";
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
// Use a sent, payable invoice. Width and height are JSON numbers, in pixels.
http.SetUrlVar("id","INVOICE_ID");
Chilkat.JsonObject body = new Chilkat.JsonObject();
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) {
Debug.WriteLine("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
Chilkat.JsonObject errorInfo = new Chilkat.JsonObject();
errorInfo.Load(resp.BodyStr);
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"));
Debug.WriteLine("PayPal debug ID: " + errorInfo.StringOf("debug_id"));
return;
}
Chilkat.JsonObject result = new Chilkat.JsonObject();
bool parsed = result.Load(resp.BodyStr);
if (parsed != true) {
Debug.WriteLine("Expected a JSON response; HTTP " + Convert.ToString(resp.StatusCode));
return;
}
Chilkat.BinData png = new Chilkat.BinData();
if (result.HasMember("image") != true) {
Debug.WriteLine("QR response did not contain an image.");
return;
}
bool decoded = png.AppendEncoded(result.StringOf("image"),"base64");
if (decoded != true) {
Debug.WriteLine("Invalid base64 image.");
return;
}
bool savedImage = png.WriteFile("paypal-invoice-qr.png");
if (savedImage != true) {
Debug.WriteLine("Could not write the invoice QR image.");
return;
}
Debug.WriteLine("Saved paypal-invoice-qr.png.");