Skip to content

Commit

Permalink
VersaPay: refactor authorization from structure (#5363)
Browse files Browse the repository at this point in the history
* VersaPay: refactor authorization from structure

Summary
----------------
This refactor the returned structure
in the authorization.

[SER-1516](https://spreedly.atlassian.net/browse/SER-1516)

Unit Tests
----------------
Finished in 0.034439 seconds.
33 tests, 170 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

Remote Tests
----------------
Finished in 46.01067 seconds.
25 tests, 42 assertions, 19 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
24% passed

Rubocop
----------------
806 files inspected, no offenses detected

* Changelog

---------

Co-authored-by: Nick Ashton <[email protected]>
  • Loading branch information
gasb150 and naashton authored Jan 13, 2025
1 parent dfee2a8 commit 0c8d47a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
* Orbital: Add XSD version testing to unit test [almalee24] #5375
* Priority: fix for bin lookup [Buitragox] #5366
* CommerceHub: Update merchantInvoiceNumber & merchantTransactionId [almalee24] #5374
* VersaPay: refactor authorization from structure [gasb150] #5363

== Version 1.137.0 (August 2, 2024)
* Unlock dependency on `rexml` to allow fixing a CVE (#5181).
Expand Down
10 changes: 5 additions & 5 deletions lib/active_merchant/billing/gateways/versa_pay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def authorize(money, payment, options = {})
def capture(money, authorization, options = {})
post = {
amount_cents: money,
transaction: authorization.split('|').first
transaction: authorization
}
commit('capture', post)
end
Expand All @@ -40,13 +40,13 @@ def verify(credit_card, options = {})
end

def void(authorization, options = {})
commit('void', { transaction: authorization.split('|').first })
commit('void', { transaction: authorization })
end

def refund(money, authorization, options = {})
post = {
amount_cents: money,
transaction: authorization.split('|').first
transaction: authorization
}
commit('refund', post)
end
Expand All @@ -65,7 +65,7 @@ def store(payment_method, options = {})
end

def unstore(authorization, options = {})
_, wallet_token, fund_token = authorization.split('|')
wallet_token, fund_token = authorization.split('|')
commit('unstore', {}, :delete, { fund_token:, wallet_token: })
end

Expand Down Expand Up @@ -218,7 +218,7 @@ def authorization_from(response)
transaction = response['transaction']
wallet_token = response['wallet_token'] || response.dig('wallets', 0, 'token')
fund_token = response['fund_token'] || response.dig('wallets', 0, 'credit_cards', 0, 'token')
[transaction, wallet_token, fund_token].join('|')
[transaction, wallet_token, fund_token].compact.join('|')
end

def error_code_from(response, action)
Expand Down
28 changes: 14 additions & 14 deletions test/remote/gateways/remote_versa_pay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_successful_authorize
assert_success response
assert_equal response.message, 'Succeeded'
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'authorize'
assert_nil response.error_code
end
Expand All @@ -40,7 +40,7 @@ def test_successful_authorize_with_shipping_address
assert_success response
assert_equal response.message, 'Succeeded'
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'authorize'
end

Expand All @@ -50,7 +50,7 @@ def test_failed_authorize_declined_credit_card
assert_failure response
assert_equal response.message, 'gateway_error_message: DECLINED | gateway_response_errors: [gateway - DECLINED]'
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'verify'

assert_equal response.error_code, 'gateway_error_code: 567.005 | response_code: 999'
Expand All @@ -61,7 +61,7 @@ def test_failed_authorize_declined_amount
assert_failure response
assert_equal response.message, 'gateway_error_message: DECLINED | gateway_response_errors: [gateway - DECLINED]'
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'verify'

assert_equal response.error_code, 'gateway_error_code: 567.005 | response_code: 999'
Expand All @@ -72,7 +72,7 @@ def test_successful_purchase
assert_success response
assert_equal 'Succeeded', response.message
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'sale'
end

Expand Down Expand Up @@ -120,7 +120,7 @@ def test_successful_capture
assert_equal 'Succeeded', response.message
assert_equal authorize.params['order'], response.params['order']
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'capture'
end

Expand All @@ -134,7 +134,7 @@ def test_successful_partial_capture
assert_equal 'Succeeded', response.message
assert_equal authorize.params['order'], response.params['order']
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'capture'
end

Expand All @@ -144,7 +144,7 @@ def test_successful_verify
assert_success response
assert_equal response.message, 'Succeeded'
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'verify'
end

Expand Down Expand Up @@ -194,7 +194,7 @@ def test_successful_void
assert_equal 'Succeeded', response.message
assert_equal authorize.params['order'], response.params['order']
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'void'
end

Expand All @@ -213,7 +213,7 @@ def test_successful_refund
assert_equal 'Succeeded', response.message
assert_equal purchase.params['order'], response.params['order']
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'refund'
end

Expand All @@ -229,7 +229,7 @@ def test_successful_credit
assert_success response
assert_equal 'Succeeded', response.message
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'credit'
end

Expand All @@ -247,7 +247,7 @@ def test_successful_store

wallet_token = response.params['wallet_token']
fund_token = response.params['fund_token']
assert_equal response.authorization, "|#{wallet_token}|#{fund_token}"
assert_equal response.authorization, "#{wallet_token}|#{fund_token}"
assert_include response.params, 'wallet_token'
assert_include response.params, 'fund_token'
assert_include response.params, 'wallets'
Expand All @@ -265,7 +265,7 @@ def test_successful_purchase_after_store
assert_success response
assert_equal 'Succeeded', response.message
assert_equal @options[:order_id], response.params['order']
assert_equal response.authorization, "#{response.params['transaction']}||"
assert_equal response.authorization, response.params['transaction']
assert_equal response.params['transactions'][0]['action'], 'sale'
end

Expand All @@ -276,7 +276,7 @@ def test_successful_unstore
response = @gateway.unstore(store.authorization, @options)
assert_success response
assert_equal 'Succeeded', response.message
assert_equal response.authorization, "||#{fund_token}"
assert_equal response.authorization, fund_token
end

def test_transcript_scrubbing
Expand Down
34 changes: 32 additions & 2 deletions test/unit/gateways/versa_pay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ def test_build_store_unstore_request_url
assert_match %r{^#{Regexp.escape(@test_url)}/api/gateway/v1/wallets//methods/$}, @gateway.send(:url, action)
end

def test_authorization_from
response = { 'transaction' => 'token_from_transaction', 'wallet_token' => 'token_from_wallet', 'fund_token' => 'token_from_fund' }
auth = @gateway.send(:authorization_from, response)
assert_equal auth, 'token_from_transaction|token_from_wallet|token_from_fund'
end

def test_authorization_from_diggin
response = { 'transaction' => 'token_from_transaction', 'wallets' => [{ 'token' => 'token_from_wallets', 'credit_cards' => [{ 'token' => 'token_from_credit_cards' }] }] }
auth = @gateway.send(:authorization_from, response)
assert_equal auth, 'token_from_transaction|token_from_wallets|token_from_credit_cards'
end

def test_authorization_from_with_no_info_returned
response = {}
auth = @gateway.send(:authorization_from, response)
assert_equal auth, ''
end

def test_authorization_from_with_one_filed_returned
response = { 'transaction' => 'token_from_transaction' }
auth = @gateway.send(:authorization_from, response)
assert_equal auth, 'token_from_transaction'
end

def test_authorization_from_with_two_fields_returned
response = { 'wallet_token' => 'token_from_wallet', 'fund_token' => 'token_from_fund' }
auth = @gateway.send(:authorization_from, response)
assert_equal auth, 'token_from_wallet|token_from_fund'
end

def test_error_code_from_errors
# a HTTP 412 response structure
error = @gateway.send(:error_code_from, { 'success' => false, 'errors' => ['fund_address_unspecified'], 'response_code' => 999 }, 'sale')
Expand Down Expand Up @@ -209,7 +239,7 @@ def test_successful_store

def test_successful_purchase_third_party_token
stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, '||third_party_token', @options)
@gateway.purchase(@amount, 'wallet_token|third_party_token', @options)
end.check_request do |_method, endpoint, data, _headers|
assert_match 'sale', endpoint
parsed_data = JSON.parse(data)
Expand All @@ -221,7 +251,7 @@ def test_successful_purchase_third_party_token

def test_successful_unstore
stub_comms(@gateway, :ssl_request) do
@gateway.unstore('auth_token|wallet_token|fund_token')
@gateway.unstore('wallet_token|fund_token')
end.check_request do |_method, endpoint, data, _headers|
assert_match 'wallets/wallet_token/methods/fund_token', endpoint
assert_equal({}, JSON.parse(data))
Expand Down

0 comments on commit 0c8d47a

Please sign in to comment.