CkJava Requires Chilkat v11.0.0+
CkJava
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 CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean 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.
CkHttp http = new CkHttp();
http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
CkHttpResponse resp = new CkHttpResponse();
// Use a sent, payable invoice. Width and height are JSON numbers, in pixels.
http.SetUrlVar("id","INVOICE_ID");
CkJsonObject body = new CkJsonObject();
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) {
System.out.println("No HTTP response: " + http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject errorInfo = new CkJsonObject();
errorInfo.Load(resp.bodyStr());
System.out.println("HTTP " + resp.get_StatusCode() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
return;
}
CkJsonObject result = new CkJsonObject();
boolean parsed = result.Load(resp.bodyStr());
if (parsed != true) {
System.out.println("Expected a JSON response; HTTP " + resp.get_StatusCode());
return;
}
CkBinData png = new CkBinData();
if (result.HasMember("image") != true) {
System.out.println("QR response did not contain an image.");
return;
}
boolean decoded = png.AppendEncoded(result.stringOf("image"),"base64");
if (decoded != true) {
System.out.println("Invalid base64 image.");
return;
}
boolean savedImage = png.WriteFile("paypal-invoice-qr.png");
if (savedImage != true) {
System.out.println("Could not write the invoice QR image.");
return;
}
System.out.println("Saved paypal-invoice-qr.png.");
}
}