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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final http = CkHttp();
  http.authToken = 'SANDBOX_ACCESS_TOKEN';
  final resp = CkHttpResponse();

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

  http.setUrlVar('id', 'INVOICE_ID');
  final body = CkJsonObject();
  body.updateInt('width', 300);
  body.updateInt('height', 300);
  body.updateString('action', 'pay');

  try {
    http.httpJson('POST', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{\$id}/generate-qr-code', body, 'application/json', resp);
  } on ChilkatException catch (e) {
    print('No HTTP response: ${e.lastErrorText}');
    return;
  }

  if ((resp.statusCode < 200) || (resp.statusCode >= 300)) {
    final errorInfo = CkJsonObject();
    errorInfo.load(resp.bodyStr);
    print('HTTP ${resp.statusCode} ${errorInfo.stringOf('name')} ${errorInfo.stringOf('details[0].issue')}');
    print('PayPal debug ID: ${errorInfo.stringOf('debug_id')}');
    return;
  }

  final result = CkJsonObject();
  try {
    result.load(resp.bodyStr);
  } on ChilkatException {
    print('Expected a JSON response; HTTP ${resp.statusCode}');
    return;
  }

  final png = CkBinData();
  if (!result.hasMember('image')) {
    print('QR response did not contain an image.');
    return;
  }

  try {
    png.appendEncoded(result.stringOf('image'), 'base64');
  } on ChilkatException {
    print('Invalid base64 image.');
    return;
  }

  try {
    png.writeFile('paypal-invoice-qr.png');
  } on ChilkatException {
    print('Could not write the invoice QR image.');
    return;
  }

  print('Saved paypal-invoice-qr.png.');
}