pay<T> method

  1. @override
Future<void> pay<T>({
  1. required T setupPayment,
})
override

Triggers the Paymob payment flow using the provided setupPayment configuration.

This method executes the following steps in order:

Step 1: Authentication

  • Sends API key to https://accept.paymob.com/api/auth/tokens.
  • Receives an auth token that identifies the merchant.

Step 2: Create Order

  • Sends a new order request with cart data to https://accept.paymob.com/api/ecommerce/orders.
  • Requires authToken, amount, and cart item details.
  • Returns a unique orderId.

Step 3: Generate Payment Key

  • Sends order info and billing data to https://accept.paymob.com/api/acceptance/payment_keys.
  • Requires authToken, orderId, and integrationId.
  • Returns a secure paymentToken.

Step 4: Navigate to WebView

  • Constructs the payment URL using:
    https://accept.paymob.com/api/acceptance/iframes/{frameId}?payment_token={paymentToken}
    
  • Navigates to the PaymobWebView to complete the payment.

Note: Exceptions during API calls are handled internally.

Example:

await PaymobPaymentService.instance.pay(
  setupPayment: SetupPayment(
    amount: 10000,
    currency: 'EGP',
    integrationId: 12345,
    frameId: 98765,
    items: [...],
    // billing data and other options...
  ),
);

Implementation

@override
Future<void> pay<T>({required T setupPayment}) async {
  try {
    if (setupPayment is! SetupPaymobPayment) {
      throw ArgumentError('setupPayment must be of type SetupPaymobPayment');
    }
    String? authToken =
        await AuthTokenRepo.fetchTokenAndHandleStates(setupPayment);

    if (authToken != null) {
      int? orderId = await OrderIdRepo.fetchOrderIdAndHandleStates(
        Order(
          curreny: setupPayment.currency,
          authToken: authToken,
          deliveryNeeded: setupPayment.delliveyNeeded,
          amount: setupPayment.amount,
          items: setupPayment.items,
        ),
      );
      if (orderId != null) {
        var paymentKey = await PaymentKeyRepo.fetchPaymentKeyAndHadleStates(
            authToken, orderId, setupPayment);

        if (paymentKey != null) {
          PaymentUrlGetter.getPaymentUrlAndNavigateToView(
              setupPayment, paymentKey);
        }
      }
    }
  } catch (e) {
    debugPrint(e.toString());
  }
}