add method

void add(
  1. BuildContext context, {
  2. required void onCreate(
    1. Create create
    ),
  3. required void onCancelled(
    1. Map<String, dynamic> payStatus
    ),
  4. required void onPaid(
    1. Map<String, dynamic> payStatus
    ),
  5. required void onError(
    1. Map error
    )?,
})

Adds a payment session.

context - The BuildContext for navigation. onCreate - Callback function to be executed on successful session creation. onCancelled - Callback function to be executed if payment is cancelled. onPaid - Callback function to be executed on successful payment. onError - Callback function to be executed on an error.

Implementation

void add(
  BuildContext context, {
  required void Function(Create create) onCreate,
  required void Function(Map<String, dynamic> payStatus) onCancelled,
  required void Function(Map<String, dynamic> payStatus) onPaid,
  required void Function(Map error)? onError,
}) {
  String url = keysViewModel.isTestMode
      ? "https://uatcheckout.thawani.om/api/v1/checkout/session"
      : "https://checkout.thawani.om/api/v1/checkout/session";
  Request.post(url: url, data: {
    "customer_id": keysViewModel.userCustomerID,
    "save_card_on_success": true,
    "client_reference_id": keysViewModel.userClintID,
    "mode": "payment",
    "products": keysViewModel.userProducts.map((e) => e.toJson()).toList(),
    "success_url": keysViewModel.userSuccessUrl ??
        'https://abom.me/package/thawani/suc.php',
    "cancel_url": keysViewModel.userCancelUrl ??
        "https://abom.me/package/thawani/can.php",
    "metadata": keysViewModel.userMetadata,
  }, headers: {
    'Content-Type': "application/json",
    'thawani-api-key': keysViewModel.userApiKey
  }).then((value) {
    if (value['data']['code'] == 2004) {
      // Call onCreate callback on successful session creation and navigate to PayWidget.
      onCreate(Create.fromJson(value['data']));
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => PayWidget(
            api: keysViewModel.userApiKey,
            uri: value['data']['data']['session_id'],
            url: keysViewModel.isTestMode == true
                ? 'https://uatcheckout.thawani.om/pay/${value['data']['data']['session_id']}?key=${keysViewModel.userPKey}'
                : 'https://checkout.thawani.om/pay/${value['data']['data']['session_id']}?key=${keysViewModel.userPKey}',
            paid: (statusClass) {
              onPaid(statusClass);
            },
            unpaid: (statusClass) {
              onCancelled(statusClass);
            },
            testMode: keysViewModel.isTestMode,
          ),
        ),
      );
    } else if (value['data']['code'] != 2004) {
      // Call onError callback for other error cases.
      onError!(value);
    } else if (value['data']['code'] == null) {
      // Call onError callback if no error code is provided.
      onError!(value['data']);
    }
  });
}