charge method
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,
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));
});
}