payApi method

dynamic payApi({
  1. required BuildContext context,
  2. required String api,
  3. required String publishKey,
  4. required String clintID,
  5. required String customerID,
  6. required List<Product> products,
  7. String? successUrl,
  8. String? cancelUrl,
  9. required Map<String, dynamic> metadata,
  10. required bool testMode,
  11. required void onCreate(
    1. Create create
    ),
  12. required void onCancelled(
    1. Map<String, dynamic> payStatus
    ),
  13. required void onPaid(
    1. Map<String, dynamic> payStatus
    ),
  14. required dynamic onError(
    1. Map error
    )?,
})

Implementation

payApi({
  required BuildContext context,
  required String api,
  required String publishKey,
  required String clintID,
  required String customerID,
  required List<Product> products,
  String? successUrl,
  String? cancelUrl,
  required Map<String, dynamic> metadata,
  required bool testMode,
  required void Function(Create create) onCreate,

  ///The Function And The Result Of Data If The User  Cancelled The Payment.
  required void Function(Map<String, dynamic> payStatus) onCancelled,

  ///The Function And The Result Of Data If The User  Cancelled The Payment.
  required void Function(Map<String, dynamic> payStatus) onPaid,

  ///The Function And The Reason Of The Error,  If Any Error Happen.
  required Function(Map error)? onError,
}) async {
  late Map<String, dynamic> dataBack;

  Future<Create> createS() async {
    return Create.fromJson(dataBack);
  }

  // Future<StatusClass> payStatus(dataStatute) async {
  //   return StatusClass.fromJson(dataStatute);
  // }

  dataBack = await RequestHelper.postRequest(
      api,
      {
        if (userSaveCard) "customer_id": customerID,
        "save_card_on_success": saveCard,
        if (expiredInMinuets !=null) "expired_in_minutes": expiredInMinuets,
        "client_reference_id": clintID,
        "mode": "payment",
        "products": products.map((e) => e.toJson()).toList(),
        "success_url":
            successUrl ?? 'https://abom.me/package/thawani/suc.php',
        "cancel_url": cancelUrl ?? "https://abom.me/package/thawani/can.php",
       "metadata": metadata,
      },
      testMode);
  if (dataBack['code'] == 2004) {
    createS().then((value) => {onCreate(value)});
    if (context.mounted) {
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => PayWidget(
                    api: api,
                    uri: dataBack['data']['session_id'],
                    url: testMode == true
                        ? 'https://uatcheckout.thawani.om/pay/${dataBack['data']['session_id']}?key=$publishKey'
                        : 'https://checkout.thawani.om/pay/${dataBack['data']['session_id']}?key=$publishKey',
                    paid: (statusClass) {
                      onPaid(statusClass);
                    },
                    unpaid: (statusClass) {
                      onCancelled(statusClass);
                    },
                    testMode: testMode,
                  )));
    }
    if (context.mounted) return;
  } else if (dataBack['code'] != 2004) {
    return onError!(dataBack);
  } else if (dataBack['code'] == null) {
    return onError!(dataBack);
  }
}