pay method

Future<PaymobResponse?> pay({
  1. required BuildContext context,
  2. required String currency,
  3. required String amountInCents,
  4. void onPayment(
    1. PaymobResponse response
    )?,
  5. required PaymentType paymentType,
  6. List? items,
  7. PaymobBillingData? billingData,
})

Proceed to pay with only calling this function. Opens a WebView at Paymob redirectedURL to accept user payment info.

Implementation

Future<PaymobResponse?> pay(
    {
    /// BuildContext for navigation to WebView
    required BuildContext context,

    /// Which Currency you would pay in.
    required String currency,

    /// Payment amount in cents EX: 20000 is an 200 EGP
    required String amountInCents,

    /// Optional Callback if you can use return result of pay function or use this callback
    void Function(PaymobResponse response)? onPayment,
    required PaymentType paymentType,

    /// list of json objects contains the contents of the purchase.
    List? items,

    /// The billing data related to the customer related to this payment.
    PaymobBillingData? billingData}) async {
  if (!_isInitialized) {
    throw Exception('PaymobPayment is not initialized call:`PaymobPayment.instance.initialize`');
  }
  final authToken = await _getAuthToken();
  final orderID = await _addOrder(
    authToken: authToken,
    currency: currency,
    amount: amountInCents,
    items: items ?? [],
  );

  // switch (paymentType) {
  //   case PaymentType.card:
  //     break;

  //   case PaymentType.easypaisa:
  //     break;

  //   case PaymentType.jazzcash:
  //     break;

  //   default:
  //     throw Exception("Please Select a Payment Type in Order to Continue");
  // }
  final purchaseToken = await _getPurchaseToken(
    authToken: authToken,
    currency: currency,
    orderID: orderID,
    paymentType: paymentType,
    amount: amountInCents,
    billingData: billingData ?? PaymobBillingData(),
  );
  if (context.mounted) {
    if (paymentType == PaymentType.card) {
      final response = await PaymobIFrame.show(
        context: context,
        redirectURL: _iFrameURL + purchaseToken,
        onPayment: onPayment,
      );
      return response;
    } else {
      final response = await PaymobIFrame.show(
        context: context,
        redirectURL: _mobileAccountiFrame + purchaseToken,
        onPayment: onPayment,
      );
      return response;
    }
  }
  return null;
}