Adyen,无法获得正确的SHA-256加密



我使用的是签名字符串和一个私钥,使用SHA-256加密生成公共密钥。

哈希功能是标准C#SHA-256哈希功能:

string CalculateHMAC256(string hmacKey, string signingstring)
    {
        byte[] key = Encoding.UTF8.GetBytes(hmacKey);
        byte[] data = Encoding.UTF8.GetBytes(signingstring);
        using (HMACSHA256 hmac = new HMACSHA256(key))
        {
            byte[] result = hmac.ComputeHash(data);
            return Convert.ToBase64String(result);
        }
    }

字符串签名看起来像这样: 07:10 01:00:2017-04-30 :: en_us :: nedrf4H4

我还尝试了逃脱的":",如:34:48 01:00:2017-05-01 :: en_us :: nedrf4H4

然后我的HTML形式看起来像这样:

 <form ngNoForm name="frmsp" id="frmsp" target="_blank" 
    action="https://test.barclaycardsmartpay.com/hpp/pay.shtml"
    method="post">
    <input type="hidden" name="merchantSig" [value]="merchantSignature" />
    <input type="hidden" name="currencyCode" [value]="smartPayment?.currencyCode" />
    <input type="hidden" name="merchantAccount" [value]="smartPayment?.merchantAccount" />
    <input type="hidden" name="merchantReference" [value]="smartPayment?.merchantReference" />
    <input type="hidden" name="paymentAmount" [value]="smartPayment?.paymentAmount" />
    <input type="hidden" name="sessionValidity" [value]="smartPayment?.sessionValidity" />
    <input type="hidden" name="shipBeforeDate" [value]="smartPayment?.shipBeforeDate" />
    <input type="hidden" name="shopperLocale" [value]="smartPayment?.shopperLocale" />
    <input type="hidden" name="orderData" [value]="smartPayment?.orderData" />
    <input type="hidden" name="skinCode" [value]="smartPayment?.skinCode" />
    <input type="hidden" name="countryCode" [value]="smartPayment?.countryCode" />
    <input type="hidden" name="shopperEmail" [value]="smartPayment?.shopperEmail" />
    <input type="hidden" name="shopperReference" [value]="smartPayment?.shopperReference" />
    <input type="hidden" name="allowedMethods" [value]="smartPayment?.allowedMethods" />
    <input type="hidden" name="blockedMethods" [value]="smartPayment?.blockedMethods" />
    <input type="hidden" name="offset" [value]="smartPayment?.offset" />    
    <input type="submit" value="Process Payment" (click)="buttonClicked()">

使用Angular2计算字段值的地方 - 使用用于构建签名字符串的相同的字段值,因此我相信数据匹配。

当我发送表格时,我会发现一个错误,说明商人签名不正确。

也许签名字符串的格式可能不正确?我正在尝试使用托管付款来实施对Barclaycard SmartPay的呼叫。文档此处:托管付款页集成指南

注意:Barclays文档已过时(2012年上一次更新(,并且对SHA-1的引用现在使用SHA-256。它是使用Adyen支付系统构建的,我已经使用以下示例代码重复了字符串生成示例:https://github.com/adyen/adyen-asp.net-sample-comple-code

您的签名字符串存在问题。您需要将" "用" "one_answers":"替换为":"。

我还建议在git上使用该示例中的代码来生成创建加密签名。

使用下面的代码,我获得了与Adyen提供的测试页面相同的签名。

 // Computes the Base64 encoded signature using the HMAC algorithm with the HMACSHA256 hashing function.
    string CalculateHMAC(string hmacKey, string signingstring)
{
    byte[] key = PackH(hmacKey);
    byte[] data = Encoding.UTF8.GetBytes(signingstring);
    try
    {
        using (HMACSHA256 hmac = new HMACSHA256(key))
        {
            // Compute the hmac on input data bytes
            byte[] rawHmac = hmac.ComputeHash(data);
            // Base64-encode the hmac
            return Convert.ToBase64String(rawHmac);
        }
    }
    catch (Exception e)
    {
        throw new Exception("Failed to generate HMAC : " + e.Message);
    }
}
byte[] PackH(string hex)
{
    if ((hex.Length % 2) == 1)
    {
        hex += '0';
    }
    byte[] bytes = new byte[hex.Length / 2];
    for (int i = 0; i < hex.Length; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    }
    return bytes;
}

尝试仅使用有限的字段,看看是否获得任何结果。我使用了以下字段(也要考虑他们的订单!(

currencyCode:merchantAccount:merchantReference:paymentAmount:sessionValidity:shipBeforeDate:shopperLocale:skinCode

最新更新