verifyCheckout method

Future<DVBachsCheckout> verifyCheckout(
  1. String checkoutId
)

Reads a checkout session back from Bachs.

The success redirect carries checkout_id, and anyone can type a URL with one in it, so a page that confirms a purchase asks Bachs rather than believing the query string. Entitlements still come from the webhook; this is for telling the customer what happened.

Implementation

Future<DVBachsCheckout> verifyCheckout(String checkoutId) async {
  final Map<String, Object?> json = await _request(
      'GET', '/v1/checkout-sessions/${Uri.encodeComponent(checkoutId)}');
  final Object? customer = json['customer'];
  final Object? metadata = json['metadata'];
  final Object? currency = json['currency'];
  final int? minor = currency is String
      ? _minorUnits('${json['amount'] ?? ''}', currency)
      : null;
  return DVBachsCheckout(
    id: '${json['checkout_id'] ?? checkoutId}',
    status: '${json['status'] ?? ''}',
    paymentStatus:
        json['payment_status'] is String ? json['payment_status'] as String : null,
    amount: minor == null ? null : DVMoney(amount: minor, currency: currency! as String),
    customer: customer is Map && customer['customer_id'] is String
        ? customer['customer_id'] as String
        : null,
    plan: metadata is Map && metadata['dartvel_plan'] is String
        ? metadata['dartvel_plan'] as String
        : null,
  );
}