Barclays Bank has been ordered by the Treasury to pay half-a-billion pounds in tax which it had tried to avoid.

Barclays was accused by HM Revenue and Customs of designing and using two schemes that were intended to avoid substantial amounts of tax.

The government has taken the unusual step of introducing retrospective legislation to end such "aggressive tax avoidance" by financial institutions.

Tax rules forced the bank to tell the authorities about its plans.

The government has closed the schemes to retrieve £500m of lost tax and safeguard payments of billions of more tax in the future.

BBC business editor Robert Peston has been told by Barclays that it is surprised by HMRC's reaction to the two schemes, which it believed to be in line with those used by other banks.

Our business editor says it is highly embarrassing for Barclays, because Britain's big banks have all signed a code committing them not to engage in tax avoidance.

However, he adds that Barclays may end up paying no more than £150m of additional tax.

'Decision justified'

Announcing the crackdown, Exchequer Secretary to the Treasury, David Gauke, said the bank should never have devised the schemes in the first place.

All Britain's big banks have signed a code committing them not to engage in tax avoidance Read Robert Peston's blog

"The bank that disclosed these schemes to HM Revenue & Customs (HMRC) has adopted the Banking Code of Practice on Taxation which contains a commitment not to engage in tax avoidance," he said.

"The government is clear that these are not transactions that a bank that has adopted the code should be undertaking.

"We do not take today's action lightly, but the potential tax loss from this scheme and the history of previous abuse in this area mean that this is a circumstance where the decision to change the law with full retrospective effect is justified."

One tax scheme involved Barclays claiming it should not have to pay corporation tax on profits made when buying back its own IOUs.

The second tax avoidance scheme, also designed by Barclays, involved investment funds claiming that non-taxable income entitled the funds to tax credits that could be reclaimed from HMRC.

The Treasury described this as "an attempt to secure 'repayment' from the Exchequer of tax that has not been paid".

Forced disclosure

A Treasury source suggested that outlawing the tax schemes immediately would save the government a further £2bn in tax that would otherwise have been foregone.

Banks are simply not going to be able to get away with it David Gauke, Exchequer Secretary to the Treasury

Barclays disclosed the two schemes to the tax authorities under rules which have been in place since 2004.

Anyone, such as a bank, accountant, lawyer or tax adviser, who devises a seemingly legal tax avoidance plan, is obliged to tell the tax authorities about it within a few days of using it or marketing it to clients.

More than 2,000 schemes have been disclosed in the past eight years.

Mr Gauke told BBC Radio 4's Today programme that the experience of Barclays showed that the system of compulsory disclosure for legal tax avoidance schemes was working.

"They have got caught, they disclosed this information, the HMRC has acted very quickly, there will be no benefit to the bank, they are clearly taking a substantial reputational hit and we have demonstrated that banks are simply not going to be able to get away with it," he said.

John Whiting, of the Chartered Institute of Taxation (CIOT), said: "Quite a few of the disclosures have come from banks in the past.

"They [the schemes] are usually intended for sale to others such as clients."

Tax obligations

Thebanking code on taxationwas first introduced by the Labour government in June 2009.

It followed reports that some big banks used large scale tax avoidance schemes involving complex transactions and financial instruments.

The code - which was supported by the incoming coalition government the following year - demands that banks which sign ensure that their tax and the tax obligations of their customers are observed.

It says they should not go out of their way to avoid tax for themselves or clients.

The 15 biggest banks operating in the UK have signed up.

'Treated even-handedly'

In a separate development, HMRC said it would appoint a senior official to act as an "assurance commissioner" for any tax deals struck with big companies for more than £100m.

The job of the commissioner will be to make sure taxpayers in general do not suffer from any such settlements.

The move follows severe criticism last December from MPs on the Public Accounts Committee who denounced HMRC for appearing to cut contentious tax deals with companies such as Vodafone and Goldman Sachs.

Lin Homer, the new HMRC chief executive said: "This commissioner will take the role of challenging whether any proposed settlement secured the correct amount of tax efficiently and that taxpayers had been treated even-handedly.

"The commissioner will also make sure that the governance procedures have been followed," she added.