Skip to content

Commit

Permalink
Rapyd: send customer object on us payment types (activemerchant#4919)
Browse files Browse the repository at this point in the history
Summary:
------------------------------
Introduces a change for authorize/purchase transactions
to only include the custome_object for us payment types.

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

Remote Test:
------------------------------
Finished in 138.6279 seconds.
42 tests, 118 assertions, 1 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
97.619% passed

*Note*: The failure test, fails becase is reference
transaction related to a wallet.

Unit Tests:
------------------------------
Finished in 42.121938 seconds.
5643 tests, 78206 assertions, 0 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
100% passed

RuboCop:
------------------------------
773 files inspected, no offenses detected
  • Loading branch information
Heavyblade committed Oct 20, 2023
1 parent 0215dd5 commit acfa39b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* Rapyd: Add recurrence_type field [yunnydang] #4912
* Revert "Adyen: Update MIT flagging for NT" [almalee24] #4914
* SumUp: Void and partial refund calls [sinourain] #4891
* Rapyd: send customer object on us payment types [Heavyblade] #4919

== Version 1.135.0 (August 24, 2023)
* PaymentExpress: Correct endpoints [steveh] #4827
Expand Down
28 changes: 20 additions & 8 deletions lib/active_merchant/billing/gateways/rapyd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def add_creditcard(post, payment, options)
add_stored_credential(post, options)
end

def send_customer_object?(options)
options[:stored_credential] && options[:stored_credential][:reason_type] == 'recurring'
def recurring?(options = {})
options.dig(:stored_credential, :reason_type) == 'recurring'
end

def valid_network_transaction_id?(options)
Expand All @@ -205,7 +205,7 @@ def add_tokens(post, payment, options)

customer_id, card_id = payment.split('|')

post[:customer] = customer_id unless send_customer_object?(options)
post[:customer] = customer_id unless recurring?(options)
post[:payment_method] = card_id
end

Expand Down Expand Up @@ -248,19 +248,31 @@ def add_payment_urls(post, options, action = '')
end

def add_customer_data(post, payment, options, action = '')
phone_number = options.dig(:billing_address, :phone) || options.dig(:billing_address, :phone_number)
post[:phone_number] = phone_number.gsub(/\D/, '') unless phone_number.nil?
post[:email] = options[:email] unless send_customer_object?(options)
post[:phone_number] = phone_number(options) unless phone_number(options).blank?
post[:email] = options[:email] unless options[:email].blank? || recurring?(options)

return if payment.is_a?(String)
return add_customer_id(post, options) if options[:customer_id]
return add_customer_id(post, options) if options[:customer_id].present?

if action == 'store'
post.merge!(customer_fields(payment, options))
else
post[:customer] = customer_fields(payment, options) unless send_customer_object?(options)
post[:customer] = customer_fields(payment, options) unless recurring?(options) || non_us_payment_type?(options)
end
end

def phone_number(options)
return '' unless address = options[:billing_address]

(address[:phone] || address[:phone_number] || '').gsub(/\D/, '')
end

def non_us_payment_type?(options = {})
return false unless options[:pm_type].present?

!options.fetch(:pm_type, '').start_with?('us_')
end

def customer_fields(payment, options)
return if options[:customer_id]

Expand Down
46 changes: 46 additions & 0 deletions test/unit/gateways/rapyd_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,18 @@ def test_not_send_customer_object_for_recurring_transactions
reason_type: 'recurring',
network_transaction_id: '12345'
}
@options[:pm_type] = 'us_debit_mastercard_card'

stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, @credit_card, @options)
end.check_request(skip_response: true) do |_method, _endpoint, data, _headers|
request = JSON.parse(data)
assert_nil request['customer']
assert_nil request['email']
end
end

def test_request_should_not_include_customer_object_on_non_use_paymen_types
stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, @credit_card, @options)
end.check_request(skip_response: true) do |_method, _endpoint, data, _headers|
Expand All @@ -448,6 +460,40 @@ def test_not_send_customer_object_for_recurring_transactions
end
end

def test_request_should_include_customer_object_and_email_for_us_payment_types
@options[:pm_type] = 'us_debit_mastercard_card'

stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, @credit_card, @options)
end.check_request(skip_response: true) do |_method, _endpoint, data, _headers|
request = JSON.parse(data)

refute_nil request['customer']
refute_nil request['email']
assert_match(/Longbob/, request['customer']['name'])
assert_equal 1, request['customer']['addresses'].size
end
end

def test_getting_phone_number_from_address_object
assert_empty @gateway.send(:phone_number, {})
assert_equal '123', @gateway.send(:phone_number, { billing_address: { phone: '123' } })
assert_equal '123', @gateway.send(:phone_number, { billing_address: { phone_number: '123' } })
assert_equal '123', @gateway.send(:phone_number, { billing_address: { phone_number: '1-2.3' } })
end

def test_detect_non_us_payment_type
refute @gateway.send(:non_us_payment_type?)
refute @gateway.send(:non_us_payment_type?, { pm_type: 'us_debit_visa_card' })
assert @gateway.send(:non_us_payment_type?, { pm_type: 'in_amex_card' })
end

def test_indicates_if_transaction_is_recurring
refute @gateway.send(:recurring?)
refute @gateway.send(:recurring?, { stored_credential: { reason_type: 'unschedule' } })
assert @gateway.send(:recurring?, { stored_credential: { reason_type: 'recurring' } })
end

private

def pre_scrubbed
Expand Down

0 comments on commit acfa39b

Please sign in to comment.