Implementation
Future<void> callPaymentTransactionCreateApi(
String type, String amount) async {
BuildContext context = Get.context!;
String entityId = pickupSelectedAddress.value.partnerId ?? "";
String firstNamePickUp = pickupSelectedAddress.value.partnerName ?? "";
String lastNamePickUp = pickupSelectedAddress.value.lastName ?? "";
String phoneNumberPickUp = pickupSelectedAddress.value.phone ?? "";
String emailPickUp = pickupSelectedAddress.value.emailId ?? "";
String streetName = pickupSelectedAddress.value.address?.streetName ?? "";
transactionId = "";
String paymentCollectionRequest = "";
if (selectedDeliveryName.value == "pickup") {
if (isPaymentSelectName.value == "Pay at Store") {
paymentCollectionRequest = 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
paymentCollectionRequest = 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
paymentCollectionRequest = 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
paymentCollectionRequest = 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");
}
} else {
if (isPaymentSelected.value == true && isWalletSelected.value == false) {
// Handle the case where payment is selected but wallet is NOT selected
paymentCollectionRequest = 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
paymentCollectionRequest = 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
paymentCollectionRequest = 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");
}
}
var bodyRequest = {
"operationContext": "UpFront",
"paymentMethod": type == "QrCode" ? "WEB_LINK" : "DEEP_LINK",
"isUpFrontOrderCreation": "true",
"orderId": "",
"cartId": cart_id.value,
"relatedParty": [
{
"id": 1,
"role": "source-information",
"name": "entityId",
"value": partnerId
},
{
"id": 2,
"role": "source-information",
"name": "firstName",
"value": firstName.value
},
{
"id": 3,
"role": "source-information",
"name": "lastName",
"value": lastName.value
},
{
"id": 4,
"role": "source-information",
"name": "PhoneNumber",
"value": phone.value
},
{
"id": 5,
"role": "transaction-information",
"name": "amount",
"value": amount
},
{
"id": 6,
"role": "transaction-information",
"name": "currency",
"value": "USD"
},
{
"id": 7,
"role": "transaction-information",
"name": "Discount",
"value": "0.0"
},
{
"id": 8,
"role": "target-information",
"name": "entityId",
"value": entityId
},
{
"id": 9,
"role": "target-information",
"name": "firstName",
"value": firstNamePickUp
},
{
"id": 10,
"role": "target-information",
"name": "lastName",
"value": lastNamePickUp
},
{
"id": 11,
"role": "target-information",
"name": "PhoneNumber",
"value": phoneNumberPickUp
},
{
"id": 12,
"role": "target-information",
"name": "Email",
"value": emailPickUp
},
{
"id": 13,
"role": "target-address-information",
"name": "street",
"value": streetName
},
{
"id": 14,
"role": "target-address-information",
"name": "city",
"value": ""
},
{
"id": 15,
"role": "target-address-information",
"name": "country",
"value": ""
}
],
"paymentCollectionRequest": json.decode(paymentCollectionRequest),
"submitStockRequest": {
"partnerId": partnerId,
"partnerType": storage.read("LEVEL_NAME"),
"distributeId": partnerId,
"distributeType": storage.read("LEVEL_NAME"),
"paymentTransactionId": "",
"billingOrShippingAddressId": "",
"transactionRemarks": noteController.text.trim(),
"purchaseOrderReferenceNumber": "",
"branchId": "",
"externalParentTransactionId": "",
"relatedParty": [
{
"name": SecureStorageService.readSecureData(
SecureStorageService.userId,
),
"role": "CreatedBy"
},
{"name": "Regional Distributor(RD)", "role": "DistributorType"},
{
"name": SecureStorageService.readSecureData(
SecureStorageService.userId,
),
"role": "agentId"
}
],
"checkInId": checkinId,
},
"collectionTypes": "CART",
"transactionType": storage.read("transactionType") ?? "SELL_IN",
};
// : {
// "opeartionContext": "UpFront",
// "paymentMethod": "DEEP_LINK",
// "relatedParty": [
// {
// "id": 1,
// "role": "transaction-information",
// "name": "amount",
// "value": amount
// },
// {
// "id": 1,
// "role": "transaction-information",
// "name": "currency",
// "value": "USD"
// },
// {
// "id": 1,
// "role": "transaction-information",
// "name": "Discount",
// "value": "0.0"
// },
// {
// "id": 1,
// "role": "target-information",
// "name": "entityId",
// "value": entityId
// },
// {
// "id": 2,
// "role": "target-information",
// "name": "firstName",
// "value": firstNamePickUp
// },
// {
// "id": 3,
// "role": "target-information",
// "name": "lastName",
// "value": lastNamePickUp
// },
// {
// "id": 4,
// "role": "target-information",
// "name": "PhoneNumber",
// "value": phoneNumberPickUp
// },
// {
// "id": 1,
// "role": "target-information",
// "name": "Email",
// "value": emailPickUp
// },
// {
// "id": 2,
// "role": "target-address-information",
// "name": "street",
// "value": streetName
// },
// {
// "id": 3,
// "role": "target-address-information",
// "name": "city",
// "value": ""
// },
// {
// "id": 4,
// "role": "target-address-information",
// "name": "country",
// "value": ""
// }
// ]
// };
log("bodyRequest: $bodyRequest");
Helper.progressDialog(Get.context!, "");
String requestTime = "";
String responseTime = "";
try {
await ApiClient(
baseUrl: ApiConstant.posBFF,
onFetchRequestTime: (time) {
requestTime = time;
},
onFetchResponseTime: (time) {
responseTime = time;
},
screenName: "stockOrder",
)
.getPaymentGatewayTransactionCreate(
body: bodyRequest,
xUserId: SecureStorageService.readSecureData(
SecureStorageService.xUserId,
),
xUserName: SecureStorageService.readSecureData(
SecureStorageService.xUserName,
),
)
.then((response) async {
isLoadingMore.value = true;
PaymentResponseModel model = response;
if (model.status == "POS200") {
Helper.logEvent(
"RESPONSE_EVENT",
success: true,
endPoint: Api.paymentGatewayTransactionCreate,
responseDate: responseTime,
screenName: "stockOrder",
requestDate: requestTime,
);
Helper.close();
if (model.data != null) {
var deeplink = model.data?.paymentDetails?.deepLink ?? "";
transactionId = model.data?.paymentDetails?.transactionId ?? "";
paymentCreateTransactionId.value =
model.data?.paymentDetails?.transactionId ?? "";
var transactionCode =
model.data?.paymentDetails?.transactionStatus ?? "";
paymentStatus.value = "Start";
if (transactionCode == "INITIATED") {
if (paymentmodestatus == 2) {
var qrCode = model.data?.paymentDetails?.webLink ?? "";
Future.delayed(const Duration(seconds: 2), () {
startApiPolling(transactionId);
});
orderIdFromWeblinkOrDeeplink = "";
isBackFromWeblink = false;
await Get.to(() => QrcodeScreen(weblink: qrCode));
//Check status api response when we come back to screen.
await _checkDataOnLoad();
isDiscardPayment = false;
} else {
launchPaymentGateway(deeplink, transactionId, amount);
}
} else if (transactionCode == "SUCCESS") {
Helper.messageDialog(
Get.context!,
localization.translate("tryAgain"),
model.data?.paymentDetails?.statusMessage ??
localization.translate("technicalErrorMsg"),
);
cashPayment(
context,
"",
cart_id.toString(),
sellThruOrder.value.totalAmountPaid.toString(),
sellThruOrder.value.localCurrencyGrandTotal.toString(),
isCashOnDelivery: true,
);
} else {
Helper.messageDialog(
Get.context!,
localization.translate("tryAgain"),
model.data?.paymentDetails?.statusMessage ??
localization.translate("technicalErrorMsg"),
);
}
// _launchPaymentGateway();
}
} else {
Helper.close();
final hasError = model.errors?.isNotEmpty == true;
final error = hasError ? model.errors![0] : null;
Helper.popUpDialog(
isPayment: true,
headingText: error?.code ??
localization.translate("sorryPaymentRequestFailed"),
subHeading: error?.localeMessage ??
localization.translate("orderIsNotSuccess"),
onButtonTap: Helper.close,
);
}
}).catchError((error) {
Helper.close();
isLoadingMore.value = true;
if (error is DioException) {
UDID.setTraceId(
error.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
);
Helper.logEvent(
"ERROR_EVENT",
failure: true,
requestDate: requestTime,
endPoint: Api.paymentGatewayTransactionCreate,
responseDate: DateTime.now().toString(),
screenName: "stockOrder",
error: error,
);
}
// if (paymentmodestatus == 2) {
// var qrCode =
// "https://dfhajuscyucpz.cloudfront.net/stage/khqr/market/fllop/qr-23306000000021254.png";
// Get.to(() => QrcodeScreen(qrCode: qrCode));
// } else {
// launchPaymentGateway(
// "https://www.google.com", "TRX-23306000000021254");
// }
Helper.messageDialog(
context,
localization.translate("tryAgain"),
error.toString(),
);
});
} catch (e, st) {
Helper.close();
print(st);
}
}