PureBasic HMRC VAT MTD: Submit VAT return for period

Back to Index

Submit VAT return for period

Documentation: https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-api/1.0#_submit-vat-return-for-period_post_accordion

CURL Command

curl -X POST  https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns \
-H 'Authorization: Bearer HMRC_ACCESS_TOKEN' \
-H 'Accept: application/vnd.hmrc.1.0+json' \
-H 'Content-Type: application/json' \
-d '{
  "periodKey": "A001",
  "vatDueSales": 105.50,
  "vatDueAcquisitions": -100.45,
  "totalVatDue": 5.05,
  "vatReclaimedCurrPeriod": 105.15,
  "netVatDue": 100.10,
  "totalValueSalesExVAT": 300,
  "totalValuePurchasesExVAT": 300,
  "totalValueGoodsSuppliedExVAT": 3000,
  "totalAcquisitionsExVAT": 3000,
  "finalised": true
}'

PureBasic Example

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"test-api.service.hmrc.gov.uk",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  See the Online Tool for Generating JSON Creation Code
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"periodKey","A001")
    CkJsonObject::ckUpdateNumber(json,"vatDueSales","105.50")
    CkJsonObject::ckUpdateNumber(json,"vatDueAcquisitions","-100.45")
    CkJsonObject::ckUpdateNumber(json,"totalVatDue","5.05")
    CkJsonObject::ckUpdateNumber(json,"vatReclaimedCurrPeriod","105.15")
    CkJsonObject::ckUpdateNumber(json,"netVatDue","100.10")
    CkJsonObject::ckUpdateNumber(json,"totalValueSalesExVAT","300")
    CkJsonObject::ckUpdateNumber(json,"totalValuePurchasesExVAT","300")
    CkJsonObject::ckUpdateNumber(json,"totalValueGoodsSuppliedExVAT","3000")
    CkJsonObject::ckUpdateNumber(json,"totalAcquisitionsExVAT","3000")
    CkJsonObject::ckUpdateBool(json,"finalised",1)

    CkRest::ckAddHeader(rest,"Content-Type","application/json")
    CkRest::ckAddHeader(rest,"Authorization","Bearer HMRC_ACCESS_TOKEN")
    CkRest::ckAddHeader(rest,"Accept","application/vnd.hmrc.1.0+json")

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/organisations/vat/MY_HMRC_VRN/returns",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jsonResponse.i = CkJsonObject::ckCreate()
    If jsonResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)

    ;  See the Online Tool for Generating JSON Parse Code
    processingDate.s
    paymentIndicator.s
    formBundleNumber.s

    processingDate = CkJsonObject::ckStringOf(jsonResponse,"processingDate")
    paymentIndicator = CkJsonObject::ckStringOf(jsonResponse,"paymentIndicator")
    formBundleNumber = CkJsonObject::ckStringOf(jsonResponse,"formBundleNumber")


    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jsonResponse)


    ProcedureReturn
EndProcedure

Sample JSON Response Body

{
  "processingDate": "2019-02-18T14:37:56.561Z",
  "paymentIndicator": "BANK",
  "formBundleNumber": "948551092841"
}