我在JavaScript中生成哈希字符串时遇到了问题

disho6za  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(93)

我正在尝试将支付网关作为一个模块集成到WHMCS中;脚本没有按照以下格式生成hashkey;&支付网关返回信息未完成错误;因此;参考:以下URL工作正常:
示例代码由网关的作品100%:https://websol.biz/order/modules/gateways/test.php

Hashkey Generated as per following format

99yv9hbw88&1000&TBANK&billRef&Description of transaction&EN&MC10761&9x19xxy1uv&RETL&https://websol.biz/&PKR&20200802112944
&20200803112944&T20200802112944&1.1&1&2&3&4&5

我的模块代码:

<?php
/**
 * WHMCS Jazzcash Payment Gateway Module
 *
 * Payment Gateway modules allow you to integrate payment solutions with the
 * WHMCS platform.
 *
 * This sample file demonstrates how a payment gateway module for WHMCS should
 * be structured and all supported functionality it can contain.
 *
 * Within the module itself, all functions must be prefixed with the module
 * filename, followed by an underscore, and then the function name. For this
 * example file, the filename is "jazzcash" and therefore all functions
 * begin "jazzcash_".
 *
 * If your module or third party API does not support a given function, you
 * should not define that function within your module. Only the _config
 * function is required.
 *
 * For more information, please refer to the online documentation.
 *
 * @see https://developers.whmcs.com/payment-gateways/
 *
 * @copyright Copyright (c) WHMCS Limited 2017
 * @license http://www.whmcs.com/license/ WHMCS Eula
 */

if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}

/**
 * Define module related meta data.
 *
 * Values returned here are used to determine module related capabilities and
 * settings.
 *
 * @see https://developers.whmcs.com/payment-gateways/meta-data-params/
 *
 * @return array
 */
function jazzcash_MetaData()
{
    return array(
        'DisplayName' => 'JazzCash Payment Gateway Module',
        'APIVersion' => '1.1', // Use API Version 1.1
        'DisableLocalCredtCardInput' => false,
        'TokenisedStorage' => false,
    );
}

/**
 * Define gateway configuration options.
 *
 * The fields you define here determine the configuration options that are
 * presented to administrator users when activating and configuring your
 * payment gateway module for use.
 *
 * Supported field types include:
 * * text
 * * password
 * * yesno
 * * dropdown
 * * radio
 * * textarea
 *
 * Examples of each field type and their possible configuration parameters are
 * provided in the sample function below.
 *
 * @return array
 */
function jazzcash_config()
{
    return array(
        // the friendly display name for a payment gateway should be
        // defined here for backwards compatibility
         'FriendlyName' => array(
            'Type' => 'System',
            'Value' => 'JazzCash',
        ),
        // a text field type allows for single line text input
        'merchantID' => array(
            'FriendlyName' => 'Merchant ID',
            'Type' => 'text',
            'Size' => '25',
            'Default' => '',
            'Description' => 'Enter your Merchant ID here',
        ),
        // a password field type allows for masked text input
        'salt' => array(
            'FriendlyName' => 'Integerity Salt',
            'Type' => 'text',
            'Size' => '25',
            'Default' => '',
            'Description' => 'Enter Integerity Salt key here',
        ),
        'password' => array(
            'FriendlyName' => 'Integerity Password',
            'Type' => 'password',
            'Size' => '25',
            'Default' => '',
            'Description' => 'Enter Integerity Password here',
        ),
        /*'securehash' => array(
            'FriendlyName' => 'SecureHash',
            'Type' => 'text',
            'Size' => '255',
            'Default' => '',
            'Description' => 'Enter SecureHash here',
        ),*/
    );
}

/**
 * Payment link.
 *
 * Required by third party payment gateway modules only.
 *
 * Defines the HTML output displayed on an invoice. Typically consists of an
 * HTML form that will take the user to the payment gateway endpoint.
 *
 * @param array $params Payment Gateway Module Parameters
 *
 * @see https://developers.whmcs.com/payment-gateways/third-party-gateway/
 *
 * @return string
 */
function jazzcash_link($params)
{
    
    // Gateway Configuration Parameters
    $merchantID = $params['merchantID'];
    $moduleDisplayName = $params['name'];
    $password = $params['password'];
    $salt = $params['salt'];
    //$securehash = $params['securehash'];

    // Invoice Parameters
    $invoiceId = $params['invoiceid'];
    $invoicenum = $params['invoicenum'];
    $dueDate = $params['dueDate'];
    $description = $params["description"];
    $amount = $params['amount'];
    $currencyCode = $params['currency'];

    // System Parameters
    $companyName = $params['companyname'];
    $systemUrl = $params['systemurl'];
    $returnUrl = $params['returnurl'];
    $langPayNow = $params['langpaynow'];
    $moduleName = $params['paymentmethod'];
    $whmcsVersion = $params['whmcsVersion'];

    $url = 'https://sandbox.jazzcash.com.pk/CustomerPortal/transactionmanagement/merchantform/';
    
    $postfields = array();
    $postfields['pp_Version'] = '1.1';
    
    //DD -Direct Debit
    //MIGS - Card Payment
    //MWALLET - Mobile Voucher
    //OTC - Voucher Payment
    
    $postfields['pp_TxnType'] = '';
    $postfields['pp_Language'] = 'EN';
    $postfields['pp_MerchantID'] = $merchantID;
    $postfields['pp_SubMerchantID'] = '';
    $postfields['pp_Password'] = $password;
    $postfields['pp_BankID'] = 'TBANK';
    $postfields['pp_ProductID'] = 'RETL';
    
    //generate the referenec number
    $postfields['pp_TxnRefNo'] = 'T'. $invoicenum . '' . rand(1111111111 , 9999999999) ;
    
    $postfields['pp_Amount'] = str_replace('.' , '' ,$amount);
    $postfields['pp_TxnCurrency'] = 'PKR';
    
    //Generate it Like 20180523111537 - yyyyMMddHHmmss
    $postfields['pp_TxnDateTime'] = date('YmdHis');
    
    $postfields['pp_BillReference'] = $invoiceId;
    $postfields['pp_Description'] = $description;
    
    //Generate The Time Like: 20180524111537
    $postfields['pp_TxnExpiryDateTime'] = date('YmdHis' , strtotime(date('YmdHis'). ' + 2 days'));
    
    //$postfields['callback_url'] = $systemUrl . '/modules/gateways/callback/' . $moduleName . '.php';
    $postfields['pp_ReturnURL'] = $returnUrl;
    //$postfields['pp_ReturnURL'] = urlencode('http://hosting.kayecommerce.net/viewinvoice.php');
    
    
    //pp_SecureHash
    //$securehash;
    $postfields['pp_SecureHash'] = '';
    $postfields['ppmpf_1'] = '1';
    $postfields['ppmpf_2'] = '2';
    $postfields['ppmpf_3'] = '3';
    $postfields['ppmpf_4'] = '4';
    $postfields['ppmpf_5'] = '5';
    
    
    
    ?> 
    
                    <script>
                                            function submitForm() {

                                                CalculateHash();
                                                var IntegritySalt = document.getElementById("salt").innerText;
                                                var hash = CryptoJS.HmacSHA256(document.getElementById("hashValuesString").value, IntegritySalt);
                                                document.getElementsByName("pp_SecureHash")[0].value = hash + '';

                                                console.log('string: ' + hashString);
                                                console.log('hash: ' + document.getElementsByName("pp_SecureHash")[0].value);

                                                document.jsform.submit();
                                            }


                                        </script>
                                        <script src="https://sandbox.jazzcash.com.pk/Sandbox/Scripts/hmac-sha256.js"></script>
    
    
    
    <?php
    
    
    $htmlOutput = '
                                        
                                        <div class="jsformWrapper">';
                       $htmlOutput .= '<form name="jsform" method="post" action="' . $url . '">' ;  
    
    foreach ($postfields as $k => $v) {
        $htmlOutput .= '<input type="text" name="' . $k . '" value="'.$v.'">' ;
    }
    
    
    $htmlOutput .= '<button type="button" class="text-center" onclick="submitForm()">' . $langPayNow . '</button><!--<input type="submit" onclick="submitForm()" value="' . $langPayNow . '" />-->';
    $htmlOutput .= '</form>';                   
                                     

                                        $htmlOutput .= '<label id="salt" style="display:none;">'.$salt.'</label>
                                            <br><br>
                                            <div class="formFielWrapper">
                                              
                                                <input type="hidden" id="hashValuesString" value="">
                                                <br><br>
                                            </div>

                                        </div>';
    
    ?>
    
                  <script>
                                            function CalculateHash() {
                                                var IntegritySalt = document.getElementById("salt").innerText;
                                                hashString = '';

                                                hashString += IntegritySalt + '&';

                                                if (document.getElementsByName("pp_Amount")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_Amount")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_BankID")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_BankID")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_BillReference")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_BillReference")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_Description")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_Description")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_Language")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_Language")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_MerchantID")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_MerchantID")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_Password")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_Password")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_ProductID")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_ProductID")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_ReturnURL")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_ReturnURL")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_SubMerchantID")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_SubMerchantID")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_TxnCurrency")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_TxnCurrency")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_TxnDateTime")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_TxnDateTime")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_TxnExpiryDateTime")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_TxnExpiryDateTime")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_TxnRefNo")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_TxnRefNo")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_TxnType")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_TxnType")[0].value + '&';
                                                }
                                                if (document.getElementsByName("pp_Version")[0].value != '') {
                                                    hashString += document.getElementsByName("pp_Version")[0].value + '&';
                                                }
                                                if (document.getElementsByName("ppmpf_1")[0].value != '') {
                                                    hashString += document.getElementsByName("ppmpf_1")[0].value + '&';
                                                }
                                                if (document.getElementsByName("ppmpf_2")[0].value != '') {
                                                    hashString += document.getElementsByName("ppmpf_2")[0].value + '&';
                                                }
                                                if (document.getElementsByName("ppmpf_3")[0].value != '') {
                                                    hashString += document.getElementsByName("ppmpf_3")[0].value + '&';
                                                }
                                                if (document.getElementsByName("ppmpf_4")[0].value != '') {
                                                    hashString += document.getElementsByName("ppmpf_4")[0].value + '&';
                                                }
                                                if (document.getElementsByName("ppmpf_5")[0].value != '') {
                                                    hashString += document.getElementsByName("ppmpf_5")[0].value + '&';
                                                }

                                                hashString = hashString.slice(0, -1);
                                                document.getElementById("hashValuesString").value = hashString;
                                            }

                                        </script>
    

    
    
    
    
    <?php 

    
    return $htmlOutput;
    
}
?>


<?php

/**
 * Refund transaction.
 *
 * Called when a refund is requested for a previously successful transaction.
 *
 * @param array $params Payment Gateway Module Parameters
 *
 * @see https://developers.whmcs.com/payment-gateways/refunds/
 *
 * @return array Transaction response status
 */
function jazzcash_refund($params)
{
    return true;
}

/**
 * Cancel subscription.
 *
 * If the payment gateway creates subscriptions and stores the subscription
 * ID in tblhosting.subscriptionid, this function is called upon cancellation
 * or request by an admin user.
 *
 * @param array $params Payment Gateway Module Parameters
 *
 * @see https://developers.whmcs.com/payment-gateways/subscription-management/
 *
 * @return array Transaction response status
 */
function jazzcash_cancelSubscription($params)
{
    return true;
} 

?>

生成以下Hashkey:

ad12f44e5e7dfd135d204b6e31229e3b13232a180493f26e61a15251333899a8
egmofgnx

egmofgnx1#

你为什么不试试PHP,它非常简单和干净的方式在PHP中,这是我的代码,

ksort($postfields); //Sort in Assendig order
    $str = $integrity_salt;  //Your Seceret key to add
    foreach($postfields as $key => $value)
    {
        if(!empty($value) && $value != 'undefined' && $value != null && $value != '' && $value)
        {
            $str .=  "&".$value;
        }
    }
    

    
    $pp_SecureHash = hash_hmac('sha256', $str, $integrity_salt);
hc8w905p

hc8w905p2#

C#开发者代码

为键值创建简单类模型

public class KeyValueModel
{
    public string Key { get; set; }
    public string Value { get; set; }
}

为Json对象创建Jazz Cash模型以用于发布请求

var model = new JazzCashModel
{
    pp_Version = "1.1",
    pp_TxnType = "MWALLET",
    pp_Language = "EN",
    pp_MerchantID = "MC58085",
    pp_SubMerchantID = "",
    pp_Password = "33t8ve7s5z",
    pp_BankID = "",
    pp_ProductID = "",
    pp_TxnRefNo = "T20230623141244",
    pp_Amount = "50000",
    pp_TxnCurrency = "PKR",
    pp_TxnDateTime = "20230623141244",
    pp_BillReference = "billref",
    pp_Description = "Description of transaction",
    pp_TxnExpiryDateTime = "20230623141244",
    pp_ReturnURL = "www.fqmsys.com",
    pp_SecureHash = "",
    ppmpf_1 = "03426791550",
    ppmpf_2 = "",
    ppmpf_3 = "",
    ppmpf_4 = "",
    ppmpf_5 = ""
};

创建键值模型的列表

var list = new List<KeyValueModel>();

list.Add(new KeyValueModel { Key = "pp_Version", Value =model.pp_Version});
list.Add(new KeyValueModel { Key = "pp_TxnType", Value =model.pp_TxnType});
list.Add(new KeyValueModel { Key = "pp_Language", Value =model.pp_Language});
list.Add(new KeyValueModel { Key = "pp_MerchantID", Value =model.pp_MerchantID});
list.Add(new KeyValueModel { Key = "pp_SubMerchantID", Value =model.pp_SubMerchantID});
list.Add(new KeyValueModel { Key = "pp_Password", Value =model.pp_Password});
list.Add(new KeyValueModel { Key = "pp_BankID", Value =model.pp_BankID});
list.Add(new KeyValueModel { Key = "pp_ProductID", Value =model.pp_ProductID});
list.Add(new KeyValueModel { Key = "pp_TxnRefNo", Value =model.pp_TxnRefNo});
list.Add(new KeyValueModel { Key = "pp_Amount", Value =model.pp_Amount});
list.Add(new KeyValueModel { Key = "pp_TxnCurrency", Value =model.pp_TxnCurrency});
list.Add(new KeyValueModel { Key = "pp_TxnDateTime", Value =model.pp_TxnDateTime});
list.Add(new KeyValueModel { Key = "pp_BillReference", Value =model.pp_BillReference});
list.Add(new KeyValueModel { Key = "pp_Description", Value =model.pp_Description});
list.Add(new KeyValueModel { Key = "pp_TxnExpiryDateTime", Value =model.pp_TxnExpiryDateTime});
list.Add(new KeyValueModel { Key = "pp_ReturnURL", Value =model.pp_ReturnURL});
list.Add(new KeyValueModel { Key = "ppmpf_1", Value =model.ppmpf_1});
list.Add(new KeyValueModel { Key = "ppmpf_2", Value =model.ppmpf_2});
list.Add(new KeyValueModel { Key = "ppmpf_3", Value =model.ppmpf_3});
list.Add(new KeyValueModel { Key = "ppmpf_4", Value =model.ppmpf_4});
list.Add(new KeyValueModel { Key = "ppmpf_5", Value =model.ppmpf_5});

使用键对列表进行排序

list = list.OrderBy(x => x.Key).ToList();
var hash = "";
foreach (var item in list)
{
    if (string.IsNullOrEmpty(item.Value))
    {
        continue;
    }
    if (list.IndexOf(item)!=0 && (list.IndexOf(item) < list.Count - 1))
    {
        if (!string.IsNullOrEmpty(item.Value))
        {
            hash += "&";
        }
    }
    hash += item.Value;
   
}

使作为哈希字符串和计算哈希与盐你的盐密钥和哈希

var secure_has = ComputeHashWithSalt(hash,saltKey);

一种哈希计算方法

string ComputeHashWithSalt(string input, string salt)
{
string combinedString =   salt+"&"+input;
byte[] saltKeyBytes = Encoding.UTF8.GetBytes(salt);
byte[] inputBytes = Encoding.UTF8.GetBytes(combinedString);
using (HMACSHA256 sha256 = new HMACSHA256(saltKeyBytes))
{
   
    byte[] hashBytes = sha256.ComputeHash(inputBytes);
    string hashedString = BitConverter.ToString(hashBytes).Replace("-", "");
    return hashedString;
}

}

相关问题