Skip to content

Commit

Permalink
Simetrik: Fixes review's requested changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Velásquez Montoya committed Mar 14, 2022
1 parent c9f4924 commit b3e8e30
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 165 deletions.
39 changes: 22 additions & 17 deletions lib/active_merchant/billing/gateways/simetrik.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def capture(money, authorization, options = {})
forward_payload: {
amount: {
total_amount: amount(money).to_f,
vat: options[:vat],
currency: (options[:currency] || currency(money))
},
transaction: {
Expand All @@ -71,6 +70,7 @@ def capture(money, authorization, options = {})
acquire_extra_options: options[:acquire_extra_options] || {}
}
}
post[:forward_payload][:amount][:vat] = options[:vat] if options[:vat]

add_forward_route(post, options)
commit('capture', post, { token_acquirer: options[:token_acquirer] })
Expand All @@ -85,12 +85,12 @@ def refund(money, authorization, options = {})
currency: (options[:currency] || currency(money))
},
transaction: {
id: authorization,
comment: options[:comment]
id: authorization
},
acquire_extra_options: options[:acquire_extra_options] || {}
}
}
post[:forward_payload][:transaction][:comment] = options[:comment] if options[:comment]

add_forward_route(post, options)
commit('refund', post, { token_acquirer: options[:token_acquirer] })
Expand Down Expand Up @@ -150,22 +150,27 @@ def add_forward_payload(post, money, payment, options)
add_user(forward_payload, options[:user]) if options[:user]
add_order(forward_payload, money, options[:order]) if options[:order] || money
add_payment_method(forward_payload, payment, options[:payment_method]) if options[:payment_method] || payment

forward_payload[:payment_method] = {} unless forward_payload[:payment_method]
forward_payload[:payment_method][:card] = {} unless forward_payload[:payment_method][:card]
add_address('billing_address', forward_payload[:payment_method][:card], options[:billing_address]) if options[:billing_address]

add_three_ds_fields(forward_payload[:authentication] = {}, options[:three_ds_fields]) if options[:three_ds_fields]
add_sub_merchant(forward_payload, options[:sub_merchant])
add_sub_merchant(forward_payload, options[:sub_merchant]) if options[:sub_merchant]
forward_payload[:acquire_extra_options] = options[:acquire_extra_options] || {}
post[:forward_payload] = forward_payload
end

def add_sub_merchant(post, sub_merchant_options)
sub_merchant = {}
sub_merchant[:merchant_id] = sub_merchant_options[:merchant_id]
sub_merchant[:extra_params] = sub_merchant_options[:extra_params]
sub_merchant[:mcc] = sub_merchant_options[:mcc]
sub_merchant[:name] = sub_merchant_options[:name]
sub_merchant[:address] = sub_merchant_options[:address]
sub_merchant[:postal_code] = sub_merchant_options[:postal_code]
sub_merchant[:url] = sub_merchant_options[:url]
sub_merchant[:phone_number] = sub_merchant_options[:phone_number]
sub_merchant[:merchant_id] = sub_merchant_options[:merchant_id] if sub_merchant_options[:merchant_id]
sub_merchant[:extra_params] = sub_merchant_options[:extra_params] if sub_merchant_options[:extra_params]
sub_merchant[:mcc] = sub_merchant_options[:mcc] if sub_merchant_options[:mcc]
sub_merchant[:name] = sub_merchant_options[:name] if sub_merchant_options[:name]
sub_merchant[:address] = sub_merchant_options[:address] if sub_merchant_options[:address]
sub_merchant[:postal_code] = sub_merchant_options[:postal_code] if sub_merchant_options[:postal_code]
sub_merchant[:url] = sub_merchant_options[:url] if sub_merchant_options[:url]
sub_merchant[:phone_number] = sub_merchant_options[:phone_number] if sub_merchant_options[:phone_number]

post[:sub_merchant] = sub_merchant
end
Expand All @@ -174,7 +179,7 @@ def add_payment_method(post, payment, payment_method_options)
payment_method = {}
opts = nil
opts = payment_method_options[:card] if payment_method_options
add_card(payment_method, payment, opts)
add_card(payment_method, payment, opts) if opts || payment

post[:payment_method] = payment_method
end
Expand Down Expand Up @@ -205,7 +210,6 @@ def add_card(post, card, card_options = {})
card_hash[:type] = card.brand
card_hash[:holder_first_name] = card.first_name
card_hash[:holder_last_name] = card.last_name
add_address('billing_address', card_hash, card_options[:billing_address]) if card_options
post[:card] = card_hash
end

Expand Down Expand Up @@ -233,7 +237,7 @@ def add_order(post, money, order_options)
order[:installments] = order_options[:installments] if order_options[:installments]
order[:datetime_local_transaction] = order_options[:datetime_local_transaction] if order_options[:datetime_local_transaction]

add_amount(order, money, order_options[:amount])
add_amount(order, money, order_options[:amount]) if order_options[:amount]
add_address('shipping_address', order, order_options[:shipping_address]) if order_options[:shipping_address]

post[:order] = order
Expand All @@ -251,14 +255,15 @@ def add_amount(post, money, amount_options)
def add_address(tag, post, address_options)
address = {}
address[:name] = address_options[:name] if address_options[:name]
address[:company] = address_options[:company] if address_options[:company]
address[:address1] = address_options[:address1] if address_options[:address1]
address[:address2] = address_options[:address2] if address_options[:address2]
address[:company] = address_options[:company] if address_options[:company]
address[:city] = address_options[:city] if address_options[:city]
address[:state] = address_options[:state] if address_options[:state]
address[:country] = address_options[:country] if address_options[:country]
address[:zip] = address_options[:zip] if address_options[:zip]
address[:country] = address_options[:country] if address_options[:country]
address[:phone] = address_options[:phone] if address_options[:phone]
address[:fax] = address_options[:fax] if address_options[:fax]

post[tag] = address
end
Expand Down
134 changes: 35 additions & 99 deletions test/remote/gateways/remote_simetrik_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def setup
@credit_card_invalid = CreditCard.new(
first_name: 'Joe',
last_name: 'Doe',
number: '2440605262758053',
month: 7,
year: 2022,
number: '3551708161768059',
month: 3,
year: 2026,
verification_value: '111'
)
@amount = 100
Expand All @@ -41,78 +41,8 @@ def setup
}

}
@authorize_void_options_success = {
acquire_extra_options: {},
trace_id: SecureRandom.uuid,
user: {
id: '123',
email: '[email protected]'
},
order: {
id: rand(100000000000..999999999999).to_s,
description: 'apopsicle',
installments: 1,
datetime_local_transaction: Time.new.strftime('%Y-%m-%dT%H:%M:%S.%L%:z'),
amount: {
currency: 'USD',
vat: 19
}
},
authentication: {
three_ds_fields: {
version: '2.1.0',
eci: '05',
cavv: 'jJ81HADVRtXfCBATEp01CJUAAAA',
ds_transaction_id: '97267598-FAE6-48F2-8083-C23433990FBC',
acs_transaction_id: '13c701a3-5a88-4c45-89e9-ef65e50a8bf9',
cavv_algorithm: '1',
xid: '333333333',
directory_response_status: 'Y',
authentication_response_status: 'Y',
enrolled: 'test',
three_ds_server_trans_id: '24f701e3-9a85-4d45-89e9-af67e70d8fg8'
}
},
sub_merchant: @sub_merchant,
psp_info: @psp_info,
token_acquirer: @token_acquirer
}

@authorize_capture_options_failed = {
acquire_extra_options: {},
trace_id: SecureRandom.uuid,
user: {},
order: {
id: rand(100000000000..999999999999).to_s,
description: 'apopsicle',
installments: 1,
datetime_local_transaction: Time.new.strftime('%Y-%m-%dT%H:%M:%S.%L%:z'),
amount: {
currency: 'USD',
vat: 19
}
},
authentication: {
three_ds_fields: {
version: '2.1.0',
eci: '05',
cavv: 'jJ81HADVRtXfCBATEp01CJUAAAA',
ds_transaction_id: '97267598-FAE6-48F2-8083-C23433990FBC',
acs_transaction_id: '13c701a3-5a88-4c45-89e9-ef65e50a8bf9',
cavv_algorithm: '1',
xid: '333333333',
directory_response_status: 'Y',
authentication_response_status: 'Y',
enrolled: 'test',
three_ds_server_trans_id: '24f701e3-9a85-4d45-89e9-af67e70d8fg8'
}
},
sub_merchant: @sub_merchant,
psp_info: @psp_info,
token_acquirer: @token_acquirer
}

@authorize_capture_options_success = {
@authorize_options_success = {
acquire_extra_options: {},
trace_id: SecureRandom.uuid,
user: {
Expand Down Expand Up @@ -151,7 +81,7 @@ def setup
end

def test_success_authorize
response = @gateway.authorize(@amount, @credit_card, @authorize_capture_options_success)
response = @gateway.authorize(@amount, @credit_card, @authorize_options_success)
assert_success response
assert_instance_of Response, response
assert_equal response.message, 'successful authorize'
Expand All @@ -162,27 +92,30 @@ def test_success_authorize
end

def test_failed_authorize
response = @gateway.authorize(@amount, @credit_card, @authorize_capture_options_failed)
options = @authorize_options_success.clone()
options.delete(:user)

response = @gateway.authorize(@amount, @credit_card, options)
assert_failure response
assert_instance_of Response, response
assert_not_equal response.error_code, nil, 'Should expected error code not equal to nil'
assert_equal response.error_code, 'config_error'
assert_equal response.avs_result['code'], 'I'
assert_equal response.cvv_result['code'], 'P'
assert response.test?
end

def test_failed_authorize_by_invalid_card
response = @gateway.authorize(@amount, @credit_card_invalid, @authorize_capture_options_success)
response = @gateway.authorize(@amount, @credit_card_invalid, @authorize_options_success)
assert_failure response
assert_instance_of Response, response
assert_not_equal response.error_code, nil, 'Should expected error code not equal to nil'
assert_equal response.error_code, 'invalid_number'
assert_equal response.avs_result['code'], 'G'
assert_equal response.cvv_result['code'], 'P'
assert response.test?
end

def test_success_purchase
response = @gateway.purchase(@amount, @credit_card, @authorize_capture_options_success)
response = @gateway.purchase(@amount, @credit_card, @authorize_options_success)
assert_success response
assert_instance_of Response, response
assert_equal response.message, 'successful charge'
Expand All @@ -193,35 +126,38 @@ def test_success_purchase
end

def test_failed_purchase
response = @gateway.purchase(@amount, @credit_card, @authorize_capture_options_failed)
options = @authorize_options_success.clone()
options.delete(:user)

response = @gateway.purchase(@amount, @credit_card, options)
assert_failure response
assert_instance_of Response, response
assert_not_equal response.error_code, nil, 'Should expected error code not equal to nil'
assert_equal response.error_code, 'config_error'
assert_equal response.avs_result['code'], 'I'
assert_equal response.cvv_result['code'], 'P'
assert response.test?
end

def test_failed_purchase_by_invalid_card
response = @gateway.purchase(@amount, @credit_card_invalid, @authorize_capture_options_success)
response = @gateway.purchase(@amount, @credit_card_invalid, @authorize_options_success)
assert_failure response
assert_instance_of Response, response
assert_not_equal response.error_code, nil, 'Should expected error code not equal to nil'
assert_equal response.error_code, 'invalid_number'
assert_equal response.avs_result['code'], 'G'
assert_equal response.cvv_result['code'], 'P'
assert response.test?
end

def test_successful_authorize_and_capture
auth = @gateway.authorize(@amount, @credit_card, @authorize_capture_options_success)
auth = @gateway.authorize(@amount, @credit_card, @authorize_options_success)
assert_success auth
sleep(3)
option = {
vat: @authorize_capture_options_success[:order][:amount][:vat],
currency: @authorize_capture_options_success[:order][:amount][:currency],
vat: @authorize_options_success[:order][:amount][:vat],
currency: @authorize_options_success[:order][:amount][:currency],
transaction_id: auth.authorization,
token_acquirer: @token_acquirer,
trace_id: @authorize_capture_options_success[:trace_id]
trace_id: @authorize_options_success[:trace_id]
}

assert capture = @gateway.capture(@amount, auth.authorization, option)
Expand All @@ -230,15 +166,15 @@ def test_successful_authorize_and_capture
end

def test_failed_capture
auth = @gateway.authorize(@amount, @credit_card, @authorize_capture_options_success)
auth = @gateway.authorize(@amount, @credit_card, @authorize_options_success)
assert_success auth

option = {
vat: @authorize_capture_options_success[:order][:amount][:vat],
currency: @authorize_capture_options_success[:order][:amount][:currency],
vat: @authorize_options_success[:order][:amount][:vat],
currency: @authorize_options_success[:order][:amount][:currency],
transaction_id: auth.authorization,
token_acquirer: @token_acquirer,
trace_id: @authorize_capture_options_success[:trace_id]
trace_id: @authorize_options_success[:trace_id]
}
sleep(3)
capture = @gateway.capture(@amount, auth.authorization, option)
Expand All @@ -248,7 +184,7 @@ def test_failed_capture
currency: 'USD',
transaction_id: auth.authorization,
token_acquirer: @token_acquirer,
trace_id: @authorize_capture_options_success[:trace_id]
trace_id: @authorize_options_success[:trace_id]
}

assert capture = @gateway.capture(@amount, auth.authorization, option)
Expand All @@ -257,12 +193,12 @@ def test_failed_capture
end

def test_successful_void
auth = @gateway.authorize(@amount, @credit_card, @authorize_void_options_success)
auth = @gateway.authorize(@amount, @credit_card, @authorize_options_success)
assert_success auth

option = {
token_acquirer: @token_acquirer,
trace_id: @authorize_capture_options_success[:trace_id],
trace_id: @authorize_options_success[:trace_id],
acquire_extra_options: {}
}
sleep(3)
Expand All @@ -273,12 +209,12 @@ def test_successful_void

def test_failed_void
# First successful void
auth = @gateway.authorize(@amount, @credit_card, @authorize_void_options_success)
auth = @gateway.authorize(@amount, @credit_card, @authorize_options_success)
assert_success auth

option = {
token_acquirer: @token_acquirer,
trace_id: @authorize_capture_options_success[:trace_id],
trace_id: @authorize_options_success[:trace_id],
acquire_extra_options: {}
}
sleep(3)
Expand All @@ -297,7 +233,7 @@ def test_failed_void
end

def test_failed_refund
response = @gateway.purchase(@amount, @credit_card, @authorize_capture_options_success)
response = @gateway.purchase(@amount, @credit_card, @authorize_options_success)
option = {
token_acquirer: @token_acquirer,
trace_id: '2717a3e0-0db2-4971-b94f-686d3b72c44b',
Expand All @@ -315,7 +251,7 @@ def test_failed_refund

def test_transcript_scrubbing
transcript = capture_transcript(@gateway) do
@gateway.purchase(@amount, @credit_card, @authorize_capture_options_success)
@gateway.purchase(@amount, @credit_card, @authorize_options_success)
end
transcript = @gateway.scrub(transcript)
assert_scrubbed(@credit_card.number, transcript)
Expand Down
Loading

0 comments on commit b3e8e30

Please sign in to comment.