1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Bank Alfalah Payment Gateway in Flutter: Exception Thrown Despite Success Response

Discussão em 'Mobile' iniciado por Stack, Novembro 7, 2024 às 14:32.

  1. Stack

    Stack Membro Participativo

    I'm integrating the Bank Alfalah payment gateway in my Flutter app. I have the following code for initiating a payment request. Even when I receive a "success": true response, I'm still encountering an exception. I've tried generating the request hash using HMAC SHA-256 encryption, but my generated hash doesn't match the static hash key provided, and I'm not sure if this is causing the issue.

    int bankOrderId = Random().nextInt(1786612);
    var transactionReference = bankOrderId.toString();

    var dataString = "HS_ChannelId=$channelId&HS_IsRedirectionRequest=0"
    "&HS_MerchantId=$merchantId&HS_StoreId=$storeId"
    "&HS_ReturnURL=$returnUrl&HS_MerchantUsername=$username"
    "&HS_MerchantPassword=$password&HS_TransactionReferenceNumber=$transactionReference";

    var encryptedRequestHash = _encryptData(dataString);

    try {
    var response = await http.post(
    Uri.parse(url),
    body: {
    // "HS_ChannelId": channelId,
    // "HS_IsRedirectionRequest": "0",
    // "HS_MerchantId": merchantId,
    // "HS_StoreId": storeId,
    // "HS_ReturnURL": returnUrl,
    // "HS_MerchantHash":
    // "My MerchantHash",
    // "HS_MerchantUsername": username,
    // "HS_MerchantPassword": password,
    // "HS_TransactionReferenceNumber": transactionReference,
    // "HS_RequestHash": encryptedRequestHash,
    "HS_ChannelId": "My channel Id",
    "HS_MerchantId": "My Merchant Id",
    "HS_StoreId": "My Store Id",
    "HS_ReturnURL": "xyz",
    "HS_MerchantHash": "My HS_MerchantHash",
    "HS_MerchantUsername": "my username",
    "HS_MerchantPassword": "my pass",
    "HS_TransactionReferenceNumber": "12345", "HS_RequestHash":"vU9+EdDzpTIjhxJMT4v/EfAycy/Eo6+U4wuCo0lMaf2gOz95IXHe6LdwxcVvQh7VEp2cqz32OPtJIPQlebnlVCpmCKXMOiVExLn87vq0Fd31Wm/VpyUbogHMrLoYQv23gMOpvethLE2CA39VLGDeYI98VA8wKM+ZVmLZv1QqZ3FCfkgM+ciIh8cygpa4A0zKQD7hj8x+xlSKsRY2dPrSFdPDdkUPw37ST5v7Mj7UMSvljdiBQTuY0ri9goWowpfclgpBdu1RFbOSJSr/AshY/NYCOgTC638YS1g=="
    },
    );

    // debugPrint the raw response body
    debugPrint("Response Body: ${response.body}");

    // Decode the response body as JSON
    var jsonResponse;
    try {
    jsonResponse = json.decode(response.body);
    debugPrint("Decoded JSON Response: $jsonResponse");
    //debugPrint("Type of success: ${jsonResponse["success"]?.runtimeType}");
    } catch (e) {
    debugPrint("Error decoding JSON: $e");
    throw Exception("Failed to decode JSON response: $e");
    }

    // Ensure jsonResponse is a Map and contains 'success'

    // debugPrint type of 'success' and other fields
    // debugPrint("Type of success: ${jsonResponse['success']?.runtimeType}");
    // debugPrint(
    // "Type of AuthToken: ${jsonResponse['AuthToken']?.runtimeType}");
    // debugPrint(
    // "Type of 'ErrorMessage': ${jsonResponse['ErrorMessage']?.runtimeType}");

    if (response.statusCode == 200) {
    // debugPrint("Type of AuthToken: ${jsonResponse['AuthToken']}");
    // return {
    // "authToken": jsonResponse["AuthToken"],
    // };
    } else {
    // Log the error message returned by the API
    // debugPrint("Error Message: ${jsonResponse['ErrorMessage']}");
    throw Exception(
    "Payment initiation failed: ${jsonResponse['ErrorMessage']}");
    }
    } catch (error) {
    // debugPrint("Error initiating payment: $error");
    throw Exception("Error initiating payment: $error");
    }
    throw Exception();
    }

    // Encrypt data using HMAC SHA-256 with base64 encoding
    static dynamic _encryptData(var data) {
    var key = utf8.encode(key1); // Use key1 as the HMAC key
    var hmac = Hmac(sha256, key); // HMAC with SHA-256
    var digest = hmac.convert(utf8.encode(data)); // Convert data to hash
    return base64.encode(digest.bytes); // Encode hash in base64
    }```

    Thanks for any guidance or insight!

    Continue reading...

Compartilhe esta Página