SagePay API-创建交易时请求格式不正确-PHP

时间:2018-06-20 16:03:45

标签: php curl sagepay php-curl

我已重新发布此问题,因为已被问到。但是没有人回答这个问题。将鼠尾草支付api集成到我的网页时,我遇到了问题。

我正在跟踪圣人薪水的文件。一切都进行得很顺利,但当我要进行交易并请求链接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);

有人可以帮我这个忙吗?我无法解决我做错了的事情。到目前为止,我遵循的是文档中使用的相同代码。

文档链接为https://test.sagepay.com/documentation/

1 个答案:

答案 0 :(得分:3)

在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()发送,使修改和更改变得更加容易。这样也可以处理任何特殊字符的正确编码。