我正在使用 payfort 在我们的网站上集成 Apple Pay。
如果输入值是字符串,Payfort 会要求计算签名。但问题是如果值是数组怎么办。
如下图所示,来自那里的文档。他们要求apple_header和apple_paymentMethod字段为 List 数据类型。现在我应该如何计算这种情况下的签名,因为它将键和值作为字符串。但在我们的例子中,值是数组。(即apple_paymentMethod和apple_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"
}