SagePay API - Incorrect request format when creating a transaction - Php
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.