无法提交增值税申报表至 https://test-api.service.hmrc.gov.uk/organizations/vat/{vrn}/returns

问题描述 投票:0回答:1

根据https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-api/1.0/oas/page#tag/organizations/operation/SubmitVATreturnforperiod尝试提交增值税返回。

下面是提交数据的代码部分。

$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);

在 HMRC 授予权限后,某些内容会在后台执行约一分钟,然后重定向到我的网站,我看到错误:接收失败:对等点 125 重置连接

我的代码可能有什么问题?

php curl
1个回答
0
投票

首先在 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
);

然后将数组转换为 JSON 格式:

$vatReturnJson = json_encode($vatReturnData);

您使用 json 编码两次,这将产生要发送的无效 json

© www.soinside.com 2019 - 2024. All rights reserved.