toJson method

Map<String, dynamic> toJson(
  1. CardData card
)

JSON body for PayOrc POST .../sdk/payment ({ "data": { ... } }).

Merges this request with card (token, CVV, expiry, per-card email/mobile).

Implementation

//
// @override
// String toString() {
//   try {
//     final card = cardDataForSdkPayment();
//     return const JsonEncoder.withIndent('  ').convert(toJson(card));
//   } catch (e) {
//     return 'PaymentRequest(error while converting to JSON: $e)';
//   }
// }




Map<String, dynamic> toJson(CardData card) {
  if (orderDetails.isEmpty) {
    throw StateError(
      'PaymentRequest.orderDetails must not be empty for payment JSON.',
    );
  }
  final order = orderDetails.first;
  final month = card.expiryMonth.trim();
  final yearRaw = card.expiryYear.trim();
  final String expiry;
  if (month.isEmpty && yearRaw.isEmpty) {
    expiry = '';
  } else {
    final year = yearRaw.isEmpty
        ? ''
        : (yearRaw.length == 2 ? '20$yearRaw' : yearRaw);
    if (month.isEmpty) {
      expiry = year;
    } else if (year.isEmpty) {
      expiry = month;
    } else {
      expiry = '$month/$year';
    }
  }
  final conv = order.convenienceFee.trim();
  final cardTok = card.paymentToken?.trim() ?? '';
  final reqTok = paymentToken?.trim() ?? '';
  final token = cardTok.isNotEmpty ? cardTok : reqTok;
  final orderDetailsMap = kind == PaymentRequestType.addCard
      ? (<String, dynamic>{
          'm_order_id': order.mOrderId,
          'currency': order.currency,
          'description': order.description,
        })
      : (<String, dynamic>{
          'm_order_id': order.mOrderId,
          'amount': order.amount,
          'currency': order.currency,
          'convenience_fee': conv.isEmpty ? '' : conv,
          'description': order.description,
          if (order.quantity.trim().isNotEmpty) 'quantity': order.quantity,
        });
  return {
    'data': {
      'action': action,
      'class': 'ECOM',
      'capture_method': 'MANUAL',
      'type': 'CARD',
      'customer_details': {
        'm_customer_id': customerDetails.mCustomerId,
        'name': customerDetails.name,
        'email': card.email ?? customerDetails.email,
        'mobile': card.mobile ?? customerDetails.mobile,
        'code': card.countryCode ?? customerDetails.code,
      },
      'billing_details': ({
        'address_line1': billingDetails.addressLine1,
        'address_line2': billingDetails.addressLine2,
        'city': billingDetails.city,
        'province': billingDetails.province,
        'country': billingDetails.country,
        'pin': billingDetails.pin,
      }),
      // if (orderDetailsMap.isNotEmpty)
      'order_details': orderDetailsMap,
      'card_details': _cardDetailsJson(
        card: card,
        expiry: expiry,
        token: token,
      ),
      if (_shippingDetailsJson().isNotEmpty)
        'shipping_details': _shippingDetailsJson(),
      if (urls != null && urls!.webhookUrl.trim().isNotEmpty)
        'urls': {'webhook_url': urls!.webhookUrl.trim()},
      'parameters': payOrcNormalizedFiveSlots(parameters),
      'custom_data': payOrcNormalizedFiveSlots(customData),
    },
  };
}