Can not submit VAT Return to https://test-api.service.hmrc.gov.uk/organisations/vat/{vrn}/returns

34 Views Asked by At

According to https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-api/1.0/oas/page#tag/organisations/operation/SubmitVATreturnforperiod trying to submit VAT Return.

Below is the part of code that submits data.

$vrn = 'test';
$endpoint = 'https://test-api.service.hmrc.gov.uk/organisations/vat/'.$vrn.'/returns'; 
$periodKey = 'A001';
$authToken = $access_token;// it is $token_data['access_token']

$vatReturnData = json_encode([
"periodKey" => $periodKey,
"vatDueSales" => 105.5,
"vatDueAcquisitions" => -100.45,
"totalVatDue" => 5.05,
"vatReclaimedCurrPeriod" => 105.15,
"netVatDue" => 100.1,
"totalValueSalesExVAT" => 300,
"totalValuePurchasesExVAT" => 300,
"totalValueGoodsSuppliedExVAT" => 3000,
"totalAcquisitionsExVAT" => 3000,
"finalised" => true
]);

$vatReturnJson = json_encode($vatReturnData);

$headers = [
'Accept: application/vnd.hmrc.1.0+json',
'Authorization: Bearer ' . $authToken, 
'Content-Type: application/json', 
'Content-Length: ' . strlen($vatReturnJson)
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vatReturnData);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($ch);

if ($response === false) {
echo 'Error: ' . curl_error($ch). ' '. show_line_number();
} else {
echo 'Response: ' . $response. ' '. show_line_number();
}
curl_close($ch);

After i give permission in HMRC, something executes in background for ~ one minute, then redirect to my website and i see Error: Recv failure: Connection reset by peer 125

What may be wrong with my code?

After correction

$vatReturnData = array(
"periodKey" => $periodKey,
"vatDueSales" => 105.5,
"vatDueAcquisitions" => -100.45,
"totalVatDue" => 5.05,
...
);

got Response: {"code":"INTERNAL_SERVER_ERROR","message":"An internal server error occurred"}.

1

There are 1 best solutions below

2
Lelio Faieta On BEST ANSWER

First you build an array in PHP

$vatReturnData = array(
"periodKey" => $periodKey,
"vatDueSales" => 105.5,
"vatDueAcquisitions" => -100.45,
"totalVatDue" => 5.05,
"vatReclaimedCurrPeriod" => 105.15,
"netVatDue" => 100.1,
"totalValueSalesExVAT" => 300,
"totalValuePurchasesExVAT" => 300,
"totalValueGoodsSuppliedExVAT" => 3000,
"totalAcquisitionsExVAT" => 3000,
"finalised" => true
);

Then you convert the array in a JSON format:

$vatReturnJson = json_encode($vatReturnData);

You are using json encode twice and that will produce a not valid json to be sent