SagePay API - 创建交易时请求格式不正确 - Php
SagePay API - Incorrect request format when creating a transaction - Php
我重新发布这个问题是因为它已经被问过了。但没有人回答这个问题。我在将 Sage pay api 集成到我的网页时遇到了问题。
我正在关注 sage pay 的文档。一切都很顺利,但是当我要进行交易并向 link https://pi-test.sagepay.com/api/v1/transactions 发出请求时,它给了我响应错误
stdClass 对象([描述] => 请求格式不正确 [代码] => 1000 )
我做了与他们文档中提到的相同的事情,即使我尝试使用他们在示例中使用的相同密钥和凭据,但它在交易时给了我同样的错误。
我的交易请求码是
curl_setopt_array($curl, array(
CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/transactions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{' .
'"transactionType": "Payment",' .
'"paymentMethod": {' .
' "card": {' .
' "merchantSessionKey": "' . $merchant_session_key . '",' .
' "cardIdentifier": "' . $cardIdentifier . '",' .
' "save": "false",' .
' }' .
'},' .
'"vendorTxCode": "demotransaction' . time() . '",' .
'"amount": 10000,' .
'"currency": "GBP",' .
'"description": "Demo transaction",' .
'"apply3DSecure": "UseMSPSetting",' .
'"customerFirstName": "Sam",' .
'"customerLastName": "Jones",' .
'"billingAddress": {' .
' "address1": "407 St. John Street",' .
' "city": "London",' .
' "postalCode": "EC1V 4AB",' .
' "country": "GB"' .
'},' .
'"entryMethod": "Ecommerce"' .
'}',
CURLOPT_HTTPHEADER => array(
"Authorization: Basic aEpZeHN3N0hMYmo0MGNCOHVkRVM4Q0RSRkxodUo4RzU0TzZyRHBVWHZFNmhZRHJyaWE6bzJpSFNyRnliWU1acG1XT1FNdWhzWFA1MlY0ZkJ0cHVTRHNocktEU1dzQlkxT2lONmh3ZDlLYjEyejRqNVVzNXU=",
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
谁能帮我解决这个问题。我无法解决我做错了什么。到目前为止,我在文档中使用相同的代码。
文档 link 是 https://test.sagepay.com/documentation/
POST 正文中发送的 JSON 字符串无效 JSON。
"save": "false",
.
后有一个非法尾随逗号
一个更好的方式来表达你的请求是这样的:
$postArray = [
'transactionType' => 'Payment',
'paymentMethod' => [
'card' => [
'merchantSessionKey' => '1234',
'cardIdentifier' => '555',
'save' => 'false',
],
],
'vendorTxCode' => 'demotransaction1529515635',
'amount' => 10000,
'currency' => 'GBP',
'description' => 'Demo transaction',
'apply3DSecure' => 'UseMSPSetting',
'customerFirstName' => 'Sam',
'customerLastName' => 'Jones',
'billingAddress' => [
'address1' => '407 St. John Street',
'city' => 'London',
'postalCode' => 'EC1V 4AB',
'country' => 'GB',
],
'entryMethod' => 'Ecommerce',
];
$postBody = json_encode($postArray);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/transactions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postBody,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic xyz=",
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
将数据设置为数组,然后通过json_encode()
发送,这样修改和更改就容易多了。这也将处理任何特殊字符的正确编码。
我重新发布这个问题是因为它已经被问过了。但没有人回答这个问题。我在将 Sage pay api 集成到我的网页时遇到了问题。
我正在关注 sage pay 的文档。一切都很顺利,但是当我要进行交易并向 link https://pi-test.sagepay.com/api/v1/transactions 发出请求时,它给了我响应错误
stdClass 对象([描述] => 请求格式不正确 [代码] => 1000 )
我做了与他们文档中提到的相同的事情,即使我尝试使用他们在示例中使用的相同密钥和凭据,但它在交易时给了我同样的错误。
我的交易请求码是
curl_setopt_array($curl, array(
CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/transactions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{' .
'"transactionType": "Payment",' .
'"paymentMethod": {' .
' "card": {' .
' "merchantSessionKey": "' . $merchant_session_key . '",' .
' "cardIdentifier": "' . $cardIdentifier . '",' .
' "save": "false",' .
' }' .
'},' .
'"vendorTxCode": "demotransaction' . time() . '",' .
'"amount": 10000,' .
'"currency": "GBP",' .
'"description": "Demo transaction",' .
'"apply3DSecure": "UseMSPSetting",' .
'"customerFirstName": "Sam",' .
'"customerLastName": "Jones",' .
'"billingAddress": {' .
' "address1": "407 St. John Street",' .
' "city": "London",' .
' "postalCode": "EC1V 4AB",' .
' "country": "GB"' .
'},' .
'"entryMethod": "Ecommerce"' .
'}',
CURLOPT_HTTPHEADER => array(
"Authorization: Basic aEpZeHN3N0hMYmo0MGNCOHVkRVM4Q0RSRkxodUo4RzU0TzZyRHBVWHZFNmhZRHJyaWE6bzJpSFNyRnliWU1acG1XT1FNdWhzWFA1MlY0ZkJ0cHVTRHNocktEU1dzQlkxT2lONmh3ZDlLYjEyejRqNVVzNXU=",
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
谁能帮我解决这个问题。我无法解决我做错了什么。到目前为止,我在文档中使用相同的代码。
文档 link 是 https://test.sagepay.com/documentation/
POST 正文中发送的 JSON 字符串无效 JSON。
"save": "false",
.
一个更好的方式来表达你的请求是这样的:
$postArray = [
'transactionType' => 'Payment',
'paymentMethod' => [
'card' => [
'merchantSessionKey' => '1234',
'cardIdentifier' => '555',
'save' => 'false',
],
],
'vendorTxCode' => 'demotransaction1529515635',
'amount' => 10000,
'currency' => 'GBP',
'description' => 'Demo transaction',
'apply3DSecure' => 'UseMSPSetting',
'customerFirstName' => 'Sam',
'customerLastName' => 'Jones',
'billingAddress' => [
'address1' => '407 St. John Street',
'city' => 'London',
'postalCode' => 'EC1V 4AB',
'country' => 'GB',
],
'entryMethod' => 'Ecommerce',
];
$postBody = json_encode($postArray);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://pi-test.sagepay.com/api/v1/transactions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postBody,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic xyz=",
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
将数据设置为数组,然后通过json_encode()
发送,这样修改和更改就容易多了。这也将处理任何特殊字符的正确编码。