Reputation: 334
I am re posting this issue because its already asked. But nobody answer that question. i am into the problem while integrating sage pay api into my web page.
i am following the documentation of sage pay. Everything is going fine and smooth but when i am going to make a transaction and make a request to link https://pi-test.sagepay.com/api/v1/transactions it give me the response error
stdClass Object ( [description] => Incorrect request format [code] => 1000 )
i did the same as mentioned on their documentation even i tried to use the same keys and credentials they are using in the example but it gives me the same error at the point of transaction.
My transaction request code is
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);
Can any one help me out with this. i am unable to resolve what i am doing wrong. so far i am following the same code using in documentation.
Documentation link is https://test.sagepay.com/documentation/
Upvotes: 3
Views: 1320
Reputation: 69937
The JSON string being sent in the POST body is not valid JSON.
There is an illegal trailing comma after "save": "false",
.
A much better way to formulate your request would be something like this:
$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"
),
));
Setting up the data as an array and then sending it through json_encode()
makes it much easier to modify and change. That will also handle proper encoding of any special characters.
Upvotes: 3