0

我正在使用 payfort 在我们的网站上集成 Apple Pay。

如果输入值是字符串,Payfort 会要求计算签名。但问题是如果值是数组怎么办。

如下图所示,来自那里的文档。他们要求apple_headerapple_paymentMethod字段为 List 数据类型。现在我应该如何计算这种情况下的签名,因为它将键和值作为字符串。但在我们的例子中,值是数组。(即apple_paymentMethodapple_paymentMethod

我使用 json_encode 函数尝试了这两个字段。

如果我将这 2 个字段作为字符串发送,则 payfort api 返回字段的无效格式: apple_header。如果我将这 2 个字段作为数组发送,则 payfort api 返回Signature Mismatch

我不知道我错过了什么。

这是我的代码:

控制器代码:

$parameters = [
            'digital_wallet'        => 'APPLE_PAY',
            'command'               => 'PURCHASE',
            'access_code'           => config('payfort.APPLE_ACCESS_CODE'),
            'merchant_identifier'   => config('payfort.APPLE_MERCHANT_IDENTIFIER'),
            'merchant_reference'    => date( 'Y' ).str_pad($request->iOrderId, 6, 0, STR_PAD_LEFT) . '_' . time(),
            'amount'                => 7000,
            'currency'              => 'SAR',
            'language'              => 'en',
            'customer_email'        => 'test@gmail.com',
            'apple_data'            => $request->token['paymentData']['data'],
            'apple_signature'       => $request->token['paymentData']['signature'],
            
            'apple_header'          => json_encode([
                'apple_transactionId'=> $request->token['paymentData']['header']['transactionId'],
                'apple_ephemeralPublicKey'=> $request->token['paymentData']['header']['ephemeralPublicKey'],
                'apple_publicKeyHash'=> $request->token['paymentData']['header']['publicKeyHash']
            ]),

            'apple_paymentMethod'   => json_encode([
                'apple_displayName'=> $request->token['paymentMethod']['displayName'],
                'apple_network'=> $request->token['paymentMethod']['network'],
                'apple_type'=> $request->token['paymentMethod']['type']
            ]),
         
            'customer_ip'           => $request->ip(),
            'customer_name'         => 'ABC',
            'merchant_extra'        => $request->iOrderId,
            'merchant_extra1'       => $request->order_number,
            'merchant_extra2'       => $request->locale_info,
        ];

        // canculate signature
        $parameters['signature'] = $this->payfortCoreHelpers->calculateSignature($parameters, 'request', true);

        // Add Array or List fields in back to parameters before send
        $parameters['apple_header'] = [
            'apple_transactionId'=> $request->token['paymentData']['header']['transactionId'],
            'apple_ephemeralPublicKey'=> $request->token['paymentData']['header']['ephemeralPublicKey'],
            'apple_publicKeyHash'=> $request->token['paymentData']['header']['publicKeyHash']
        ];
        $parameters['apple_paymentMethod'] = [
            'apple_displayName'=> $request->token['paymentMethod']['displayName'],
            'apple_network'=> $request->token['paymentMethod']['network'],
            'apple_type'=> $request->token['paymentMethod']['type']
        ];
        

计算签名函数

public function calculateSignature($arrData, $signType = 'request', $isAppleRequest = false)
{
    $shaString = '';
    ksort($arrData);

    foreach ($arrData as $k => $v) {
        $shaString .= "$k=$v";
    }

    $shaString = config('payfort.SHA_REQUEST_PHRASE') . $shaString . config('payfort.SHA_REQUEST_PHRASE');

    $signature = hash(config('payfort.SHA_TYPE'), $shaString);

    return $signature;
}

在此处输入图像描述

支付响应

{
  "amount": "7000",
  "response_code": "00008",
  "digital_wallet": "APPLE_PAY",
  "signature": "****",
  "merchant_identifier": "****",
  "access_code": "****",
  "customer_ip": "::1",
  "language": "en",
  "command": "PURCHASE",
  "merchant_extra": "****",
  "response_message": "Signature mismatch",
  "merchant_reference": "20201599817035025",
  "customer_email": "test@gmail.com",
  "merchant_extra1": "1599817035025",
  "merchant_extra2": "SAR",
  "currency": "SAR",
  "customer_name": "ABC",
  "status": "00"
}
4

2 回答 2

0
ksort($arrData);
        foreach ($arrData as $key => $value) {
            if(is_array($value)){
                $shaSubString = '{';
                foreach ($value as $k => $v) {
                    $shaSubString .= "$k=$v, ";
                }
                $shaSubString = substr($shaSubString, 0, -2).'}';
                $shaString .= "$key=$shaSubString";
            }else{
                $shaString .= "$key=$value";
            }
        }
于 2021-02-03T09:35:27.200 回答
0

您可能会发现此链接很有帮助:https ://github.com/devinweb/payment/blob/fa595fe60df4dff3c4f2189e37fd64fffdc8421b/src/Traits/Payfort/ApplePay.php#L51

这显示了为您的请求生成签名的 calculateSignature 方法。

特别是,第 67 行表明您需要在嵌套数组中的项目之间使用逗号后跟空格。

于 2020-11-26T00:49:10.187 回答