charge method

Future<DVSale> charge({
  1. required String orderId,
  2. required Object customer,
  3. required List<DVOrderLine> lines,
  4. required DVTaxAddress to,
  5. List<String> codes = const <String>[],
  6. Set<Entitlement> entitlements = const <Entitlement>{},
  7. DVTaxBehavior behavior = DVTaxBehavior.exclusive,
})

Prices lines for customer on the server and captures the total.

Nothing about the amount comes from the caller except the lines and the codes. Charging the same orderId again -- a double click, a retried request -- returns the sale rather than capturing a second time.

Throws DVSaleRefused when tax cannot be resolved, DVPromotionUnavailable when a promotion's limit was reached since it was shown, and whatever the gateway throws when it declines; in each case nothing stays redeemed, recorded or granted.

Implementation

Future<DVSale> charge({
  required String orderId,
  required Object customer,
  required List<DVOrderLine> lines,
  required DVTaxAddress to,
  List<String> codes = const <String>[],
  Set<Entitlement> entitlements = const <Entitlement>{},
  DVTaxBehavior behavior = DVTaxBehavior.exclusive,
}) {
  if (orderId.isEmpty) {
    throw ArgumentError.value(orderId, 'orderId', 'must not be empty');
  }
  if (codes.isNotEmpty && promotions == null) {
    throw ArgumentError.value(codes, 'codes',
        'were given and no promotions are configured to resolve them');
  }
  final String customerKey = dvBillingCustomerKey(customer);
  // The callback must not return the removed future: whenComplete waits on
  // a future its callback returns, and this one is the future completing.
  return _charging[orderId] ??= _charge(
    orderId: orderId,
    customer: customer,
    customerKey: customerKey,
    lines: lines,
    to: to,
    codes: codes,
    entitlements: entitlements,
    behavior: behavior,
  ).whenComplete(() {
    unawaited(_charging.remove(orderId));
  });
}