tabbyPaymentFromRequest function
Builds Tabby Payment from Payorc PaymentRequest.
Used when user selects Tabby (Buy Now Pay Later) in the payment options sheet.
Implementation
Payment tabbyPaymentFromRequest(PaymentRequest request) {
final tabbyCurrency = _currencyFromString(
request.orderDetails.isNotEmpty
? request.orderDetails.first.currency
: 'AED',
);
final now = DateTime.now().toUtc().toIso8601String();
double totalAmount = 0;
final items = <OrderItem>[];
String referenceId = '';
for (final o in request.orderDetails) {
final amount =
double.tryParse(o.amount.replaceAll(RegExp(r'[^\d.]'), '')) ?? 0;
totalAmount += amount;
referenceId = o.mOrderId;
items.add(
OrderItem(
title: o.description,
quantity: int.tryParse(o.quantity) ?? 1,
unitPrice: amount.toStringAsFixed(2),
category: 'general',
description: o.description,
referenceId: o.mOrderId,
),
);
}
if (items.isEmpty) {
items.add(
OrderItem(
title: 'Order',
quantity: 1,
unitPrice: '0.00',
category: 'general',
referenceId: referenceId.isNotEmpty
? referenceId
: 'ORD-${DateTime.now().millisecondsSinceEpoch}',
),
);
}
final customer = request.customerDetails;
final shipping = request.shippingDetails;
final phone = customer.code.startsWith('+')
? '${customer.code}${customer.mobile}'
: '+${customer.code}${customer.mobile}';
return Payment(
amount: totalAmount.toStringAsFixed(2),
currency: tabbyCurrency,
buyer: Buyer(email: customer.email, phone: phone, name: customer.name),
buyerHistory: BuyerHistory(
loyaltyLevel: 0,
registeredSince: now,
wishlistCount: 0,
),
shippingAddress: ShippingAddress(
city: shipping.city,
address: shipping.addressLine1,
zip: shipping.pin,
),
order: Order(referenceId: "1000018370", items: items),
orderHistory: [],
);
}