Supplement for PR #797

I've taken the code from #797 and added tests here. If this PR is applied, it will merge #797 and fix #770, and spree/spree_gateway#105.