cashPayment method

dynamic cashPayment(
  1. BuildContext context,
  2. String id,
  3. String cartId,
  4. String amount,
  5. String localCurrencyAmount, {
  6. bool isCashOnDelivery = false,
})

Implementation

cashPayment(
  BuildContext context,
  String id,
  String cartId,
  String amount,
  String localCurrencyAmount, {
  bool isCashOnDelivery = false,
}) async {
  if (cacheCartId.isNotEmpty) {
    ConditionalLogs().customLog("cacheCartId ($cacheCartId) is not empty");
    return;
  } else {
    ConditionalLogs().customLog("cacheCartId ($cacheCartId) is empty");
    cacheCartId.value = cartId;
  }
  Helper.progressDialog(context, "Please wait..");
  String requestTime = DateTime.now().toString();
  // printLogs(dio);
  try {
    var data = null;
    if (selectedDeliveryName.value == "pickup") {
      if (isPaymentSelectName.value == "Pay at Store") {
        data = json.encode({
          "modeList": [
            {
              "id": "13",
              "collection": [
                [
                  {
                    "id": "8",
                    "value": amount,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            }
          ],
          "currency": "USD",
        });
      } else if (isPaymentSelected.value == true &&
          isWalletSelected.value == false) {
        // Handle the case where payment is selected but wallet is NOT selected
        data = json.encode({
          "modeList": [
            {
              "id": "16",
              "collection": [
                [
                  {
                    "id": "29",
                    "value": paymentTransactionId.value,
                    "amount": false,
                    "formatAmount": false,
                    "label": "pos.common.payment.mode.online.transactionId",
                    "fieldMappingKey": "transactionId",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "8",
                    "value": amount,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "34",
                    "value": isPaymentSelectName.value,
                    "amount": false,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.online.modeName",
                    "fieldMappingKey": "onlinePaymentMode",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            }
          ],
          "currency": "USD",
          "isUpFrontOrderCreation": "true"
        });
      } else if (isPaymentSelected.value == true &&
          isWalletSelected.value == true) {
        // Handle the case where both payment and wallet are selected
        data = json.encode({
          "modeList": [
            {
              "id": "16",
              "collection": [
                [
                  {
                    "id": "29",
                    "value": paymentTransactionId.value,
                    "amount": false,
                    "formatAmount": false,
                    "label": "pos.common.payment.mode.online.transactionId",
                    "fieldMappingKey": "transactionId",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "8",
                    "value": toPayAmount.value,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "34",
                    "value": isPaymentSelectName.value,
                    "amount": false,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.online.modeName",
                    "fieldMappingKey": "onlinePaymentMode",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            },
            {
              "id": "7",
              "collection": [
                [
                  {
                    "id": "8",
                    "value": mywalletbalanceUndo.value,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.wallet.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            }
          ],
          "currency": "USD",
          "isUpFrontOrderCreation": "true"
        });
      } else if (isPaymentSelected.value == false &&
          isWalletSelected.value == true) {
        // Handle the case where payment is NOT selected but wallet is selected
        data = json.encode({
          "modeList": [
            {
              "id": "7",
              "collection": [
                [
                  {
                    "id": "8",
                    "value": amount,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.wallet.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            }
          ],
          "currency": "USD",
        });
      } else {
        // Handle the case where neither payment nor wallet is selected (optional)
        print("Neither payment nor wallet selected");
      }

      // ? json.encode({
      //     "modeList": [
      //       {
      //         "id": "13",
      //         "collection": [
      //           [
      //             {
      //               "id": "8",
      //               "value": amount,
      //               "amount": true,
      //               "formatAmount": true,
      //               "label": "pos.common.payment.mode.amount",
      //               "fieldMappingKey": "amount",
      //               "fieldType": "INPUT"
      //             }
      //           ]
      //         ]
      //       }
      //     ],
      //     "currency": "USD",
      //   })
      // :
      //  json.encode({
      //     "modeList": [
      //       {
      //         "id": "7",
      //         "collection": [
      //           [
      //             {
      //               "id": "8",
      //               "value": amount,
      //               "amount": true,
      //               "formatAmount": true,
      //               "label": "pos.common.payment.mode.wallet.amount",
      //               "fieldMappingKey": "amount",
      //               "fieldType": "INPUT"
      //             }
      //           ]
      //         ]
      //       }
      //     ],
      //     "currency": "USD",
      //   });
    } else {
      if (isPaymentSelected.value == true &&
          isWalletSelected.value == false) {
        // Handle the case where payment is selected but wallet is NOT selected
        data = json.encode({
          "modeList": [
            {
              "id": "16",
              "collection": [
                [
                  {
                    "id": "29",
                    "value": paymentTransactionId.value,
                    "amount": false,
                    "formatAmount": false,
                    "label": "pos.common.payment.mode.online.transactionId",
                    "fieldMappingKey": "transactionId",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "8",
                    "value": amount,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "34",
                    "value": isPaymentSelectName.value,
                    "amount": false,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.online.modeName",
                    "fieldMappingKey": "onlinePaymentMode",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            }
          ],
          "currency": "USD",
          "isUpFrontOrderCreation": "true"
        });
      } else if (isPaymentSelected.value == true &&
          isWalletSelected.value == true) {
        // Handle the case where both payment and wallet are selected
        data = json.encode({
          "modeList": [
            {
              "id": "16",
              "collection": [
                [
                  {
                    "id": "29",
                    "value": paymentTransactionId.value,
                    "amount": false,
                    "formatAmount": false,
                    "label": "pos.common.payment.mode.online.transactionId",
                    "fieldMappingKey": "transactionId",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "8",
                    "value": toPayAmount.value,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  },
                  {
                    "id": "34",
                    "value": isPaymentSelectName.value,
                    "amount": false,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.online.modeName",
                    "fieldMappingKey": "onlinePaymentMode",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            },
            {
              "id": "7",
              "collection": [
                [
                  {
                    "id": "8",
                    "value": mywalletbalanceUndo.value,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.wallet.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            }
          ],
          "currency": "USD",
          "isUpFrontOrderCreation": "true"
        });
      } else if (isPaymentSelected.value == false &&
          isWalletSelected.value == true) {
        // Handle the case where payment is NOT selected but wallet is selected
        data = json.encode({
          "modeList": [
            {
              "id": "7",
              "collection": [
                [
                  {
                    "id": "8",
                    "value": amount,
                    "amount": true,
                    "formatAmount": true,
                    "label": "pos.common.payment.mode.wallet.amount",
                    "fieldMappingKey": "amount",
                    "fieldType": "INPUT"
                  }
                ]
              ]
            }
          ],
          "currency": "USD",
        });
      } else {
        // Handle the case where neither payment nor wallet is selected (optional)
        print("Neither payment nor wallet selected");
      }
    }
    dio
        .post(
      '${ApiConstant.posBaseUrl}pos-payment-service-svc/payment-collection/carts/$cartId',
      data: data,

      // json.encode({
      //   "modeList": [
      //     {
      //       "id": "7",
      //       "collection": [
      //         [
      //           {
      //             "id": "8",
      //             "value": amount,
      //             "amount": true,
      //             "formatAmount": true,
      //             "label": "pos.common.payment.mode.amount",
      //             "fieldMappingKey": "amount",
      //             "fieldType": "INPUT"
      //           }
      //         ]
      //       ]
      //     }
      //   ],
      //   "currency": "USD"
      //   // "modeList": [
      //   //   {
      //   //     "id": id,
      //   //     "collection": [
      //   //       [
      //   //         {
      //   //           "id": "40",
      //   //           "value": "USD",
      //   //           "label": "pos.common.payment.mode.currencyType",
      //   //           "fieldMappingKey": "currencyType",
      //   //           "fieldType": "DROPDOWN",
      //   //           "currency": null,
      //   //           "configs": {},
      //   //           "amount": false,
      //   //           "formatAmount": false,
      //   //           "unique": false
      //   //         },
      //   //         {
      //   //           "id": "8",
      //   //           "value": amount,
      //   //           "label": "pos.common.payment.mode.amount",
      //   //           "fieldMappingKey": "cashAmount",
      //   //           "fieldType": "INPUT",
      //   //           "currency": "USD",
      //   //           "configs": {},
      //   //           "amount": true,
      //   //           "formatAmount": true,
      //   //           "unique": false
      //   //         },
      //   //         {
      //   //           "id": "42",
      //   //           "value": null,
      //   //           "label":
      //   //               "pos.common.payment.mode.microPay.placeholder.exchangeRate",
      //   //           "fieldMappingKey": "exchangeRate",
      //   //           "fieldType": "INPUT",
      //   //           "currency": "KHR",
      //   //           "configs": {
      //   //             "maximumFractionDigits": "5",
      //   //             "minimumFractionDigits": "5"
      //   //           },
      //   //           "amount": false,
      //   //           "formatAmount": true,
      //   //           "unique": false
      //   //         },
      //   //         {
      //   //           "id": "41",
      //   //           "value": localCurrencyAmount,
      //   //           "label": "pos.common.payment.mode.localCurrencyAmount",
      //   //           "fieldMappingKey": "localCurrencyAmount",
      //   //           "fieldType": "INPUT",
      //   //           "currency": "KHR",
      //   //           "configs": {},
      //   //           "amount": false,
      //   //           "formatAmount": true,
      //   //           "unique": false
      //   //         }
      //   //       ]
      //   //     ]
      //   //   }
      //   // ],
      //   // "currency": "USD"

      // }),
      options: Options(
        headers: {
          'X-User-Id': SecureStorageService.readSecureData(
            SecureStorageService.xUserId,
          ),
          'X-User-Name': SecureStorageService.readSecureData(
            SecureStorageService.xUserName,
          ),
          'Accept-Language': storage.read("selected_language") ?? "en",
          "Authorization": "Bearer ${SecureStorageService.readSecureData(
            SecureStorageService.accessToken,
          )}",
          'x-request-txn-id': UDID.uDID,
          'x-trace-id': UDID.uDIDTraceId,
        },
      ),
    )
        .then((response) {
      Helper.logEvent(
        "RESPONSE_EVENT",
        success: true,
        endPoint: 'pos-payment-service-svc/payment-collection/carts/$cartId',
        responseDate: DateTime.now().toString(),
        screenName: "stockOrder",
        requestDate: requestTime,
      );
      if (response.data != null) {
        ConditionalLogs().customLog('paymentCollect${response.data}');
        PaymentCollection model = PaymentCollection();
        Map<String, dynamic> creditData = response.data;
        model = PaymentCollection.fromJson(creditData);
        if (model.status == 'POS200') {
          Helper.close();
          if (model.data?.orderId != null) {
            paymentmodestatus = 0;
            Get.to(
              () => SellInSuccessScreen(orderId: model.data?.orderId ?? ""),
            );
          } else {
            orderCreation(
              context,
              model.data!.paymentCollectionByModeList![0].cartId!,
              model.data!.transactionId!,
              storage.read("transactionType") ?? "SELL_IN",
              isBank: false,
              isCashOnDelivery: isCashOnDelivery,
            );
          }
        } else {
          Helper.close();
          cacheCartId.value = "";
          ConditionalLogs()
              .customLog("cacheCartId ($cacheCartId) is removed");
          if (isPaymentSelected.value == true) {
            goToPaymentPendingSuccessScreen();
          } else {
            Helper.messageDialog(
              Get.context!,
              model.errors?[0]?["errorItem"]["errorCode"] ??
                  localization.translate("tryAgain"),
              model.errors?[0]?["errorItem"]["errorMessage"] ??
                  localization.translate("somethingWentWrong"),
            );
          }
        }
      } else {
        Helper.close();
        cacheCartId.value = "";
        ConditionalLogs().customLog("cacheCartId ($cacheCartId) is removed");
        if (isPaymentSelected.value == true) {
          goToPaymentPendingSuccessScreen();
        }
      }
    }).catchError((err) {
      ConditionalLogs().customLog("$err");
      cacheCartId.value = "";
      ConditionalLogs().customLog("cacheCartId ($cacheCartId) is removed");
      if (err is DioException) {
        UDID.setTraceId(
          err.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
        );
        Helper.logEvent(
          "ERROR_EVENT",
          failure: true,
          requestDate: requestTime,
          endPoint:
              'pos-payment-service-svc/payment-collection/carts/$cartId',
          responseDate: DateTime.now().toString(),
          screenName: "stockOrder",
          error: err,
          transactionId: cartId,
        );
      }
      Helper.close();
      // MainController mainController = Get.put(MainController());
      // mainController.showErrorPopup();
      if (isPaymentSelected.value == true) {
        goToPaymentPendingSuccessScreen();
      } else {
        MainController mainController = Get.put(MainController());
        mainController.showErrorPopup();
      }
    });
  } catch (err, stacktrace) {
    cacheCartId.value = "";
    ConditionalLogs().customLog("cacheCartId ($cacheCartId) is removed");
    ConditionalLogs().customLog("$stacktrace");
    if (err is DioException) {
      UDID.setTraceId(
        err.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
      );
      Helper.logEvent(
        "ERROR_EVENT",
        failure: true,
        requestDate: requestTime,
        endPoint: 'pos-payment-service-svc/payment-collection/carts/$cartId',
        responseDate: DateTime.now().toString(),
        screenName: "stockOrder",
        error: err,
        transactionId: cartId,
      );
    }
    Helper.close();
    if (isPaymentSelected.value == true) {
      goToPaymentPendingSuccessScreen();
    }
  }
}