You could simplify this by doing it similar to how they do it here - https://github.com/globaldev/active_merchant/blob/avs-parsing-bug/lib/active_merchant/billing/gateways/wirecard.rb#L260-L262