Skip to content

Commit

Permalink
Rapyd: Add customer object and fix tests
Browse files Browse the repository at this point in the history
The Rapyd gateway requires the Customer object on multiple payment
types now and is optional on all. This commit adds the customer subhash
to requests and updates the remote tests to pass since Rapyd has changed
it's requirements.

Remote:
31 tests, 88 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
  • Loading branch information
aenand committed Aug 4, 2023
1 parent 47f663b commit bb37917
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Paysafe: Truncate address fields [jcreiff] #4841
* Braintree: Support third party Network Tokens [aenand] #4775
* Kushki: Fix add amount default method for subtotalIva and subtotalIva0 [yunnydang] #4845
* Rapyd: Add customer object to requests [aenand] #4838

== Version 1.134.0 (July 25, 2023)
* Update required Ruby version [almalee24] #4823
Expand Down
47 changes: 38 additions & 9 deletions lib/active_merchant/billing/gateways/rapyd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def verify(credit_card, options = {})
def store(payment, options = {})
post = {}
add_payment(post, payment, options)
add_customer_object(post, payment, options)
add_customer_data(post, payment, options, 'store')
add_metadata(post, options)
add_ewallet(post, options)
add_payment_fields(post, options)
Expand Down Expand Up @@ -100,14 +100,13 @@ def add_reference(authorization)
def add_auth_purchase(post, money, payment, options)
add_invoice(post, money, options)
add_payment(post, payment, options)
add_customer_object(post, payment, options)
add_customer_data(post, payment, options)
add_3ds(post, payment, options)
add_address(post, payment, options)
add_metadata(post, options)
add_ewallet(post, options)
add_payment_fields(post, options)
add_payment_urls(post, options)
add_customer_id(post, options)
end

def add_address(post, creditcard, options)
Expand Down Expand Up @@ -213,12 +212,42 @@ def add_payment_urls(post, options)
post[:error_payment_url] = options[:error_payment_url] if options[:error_payment_url]
end

def add_customer_object(post, payment, options)
post[:name] = "#{payment.first_name} #{payment.last_name}" unless payment.is_a?(String)
phone = options.dig(:billing_address, :phone) .gsub(/\D/, '') unless options[:billing_address].nil?
post[:phone_number] = phone || options.dig(:customer, :phone_number)
post[:email] = options[:email] || options.dig(:customer, :email)
post[:addresses] = options.dig(:customer, :addresses) if USA_PAYMENT_METHODS.include?(options[:pm_type])
def add_customer_data(post, payment, options, action = '')
post[:phone_number] = options.dig(:billing_address, :phone) .gsub(/\D/, '') unless options[:billing_address].nil?
post[:email] = options[:email]
return add_customer_id(post, options) if options[:customer_id]

if action == 'store'
post.merge!(customer_fields(payment, options))
else
post[:customer] = customer_fields(payment, options)
end
end

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

customer_data = {}
customer_data[:name] = "#{payment.first_name} #{payment.last_name}" unless payment.is_a?(String)
customer_data[:addresses] = [address(options)]
customer_data
end

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

formatted_address = {}

formatted_address[:name] = address[:name] if address[:name]
formatted_address[:line_1] = address[:address1] if address[:address1]
formatted_address[:line_2] = address[:address2] if address[:address2]
formatted_address[:city] = address[:city] if address[:city]
formatted_address[:state] = address[:state] if address[:state]
formatted_address[:country] = address[:country] if address[:country]
formatted_address[:zip] = address[:zip] if address[:zip]
formatted_address[:phone_number] = address[:phone].gsub(/\D/, '') if address[:phone]

formatted_address
end

def add_customer_id(post, options)
Expand Down
55 changes: 14 additions & 41 deletions test/remote/gateways/remote_rapyd_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def setup
pm_type: 'us_ach_bank',
currency: 'USD',
proof_of_authorization: false,
payment_purpose: 'Testing Purpose'
payment_purpose: 'Testing Purpose',
email: 'test@example.com',
billing_address: address(name: 'Jim Reynolds')
}
@metadata = {
'array_of_objects': [
Expand All @@ -47,14 +49,7 @@ def setup
eci: '02'
}

@address_object = address(line_1: '123 State Street', line_2: 'Apt. 34', phone_number: '12125559999')

@customer_object = {
name: 'John Doe',
phone_number: '1234567890',
email: 'est@example.com',
addresses: [@address_object]
}
@address_object = address(line_1: '123 State Street', line_2: 'Apt. 34', zip: '12345', name: 'john doe', phone_number: '12125559999')
end

def test_successful_purchase
Expand All @@ -63,25 +58,14 @@ def test_successful_purchase
assert_equal 'SUCCESS', response.message
end

def test_successful_authorize_with_customer_object
@options[:customer] = @customer_object
def test_successful_authorize_with_mastercard
@options[:pm_type] = 'us_debit_mastercard_card'
response = @gateway.authorize(@amount, @credit_card, @options)
assert_success response
assert_equal 'SUCCESS', response.message
end

def test_successful_purchase_with_customer_object
@options[:customer] = @customer_object
@options[:pm_type] = 'us_debit_mastercard_card'
response = @gateway.purchase(@amount, @credit_card, @options)
assert_success response
assert_equal 'SUCCESS', response.message
end

def test_success_purchase_without_customer_fullname
@credit_card.first_name = ''
@credit_card.last_name = ''
def test_successful_purchase_with_mastercard
@options[:pm_type] = 'us_debit_mastercard_card'
response = @gateway.purchase(@amount, @credit_card, @options)
assert_success response
Expand All @@ -96,12 +80,13 @@ def test_success_purchase_without_address_object_customer
end

def test_successful_subsequent_purchase_with_stored_credential
@options[:currency] = 'EUR'
@options[:pm_type] = 'gi_visa_card'
@options[:currency] = 'GBP'
@options[:pm_type] = 'gb_visa_card'
@options[:complete_payment_url] = 'https://www.rapyd.net/platform/collect/online/'
@options[:error_payment_url] = 'https://www.rapyd.net/platform/collect/online/'

response = @gateway.purchase(15000, @credit_card, @options.merge({ stored_credential: { network_transaction_id: '123456', reason_type: 'recurring' } }))
# Rapyd requires a random int between 10 and 15 digits for NTID
response = @gateway.purchase(15000, @credit_card, @options.merge({ stored_credential: { network_transaction_id: rand.to_s[2..11], reason_type: 'recurring' } }))
assert_success response
assert_equal 'SUCCESS', response.message
end
Expand Down Expand Up @@ -214,27 +199,15 @@ def test_failed_void
end

def test_successful_verify
response = @gateway.verify(@credit_card, @options.except(:billing_address))
response = @gateway.verify(@credit_card, @options)
assert_success response
assert_equal 'SUCCESS', response.message
end

def test_successful_verify_with_peso
options = {
pm_type: 'mx_visa_card',
currency: 'MXN'
}
response = @gateway.verify(@credit_card, options)
assert_success response
assert_equal 'SUCCESS', response.message
end

def test_successful_verify_with_yen
options = {
pm_type: 'jp_visa_card',
currency: 'JPY'
}
response = @gateway.verify(@credit_card, options)
@options[:pm_type] = 'mx_visa_card'
@options[:currency] = 'MXN'
response = @gateway.verify(@credit_card, @options)
assert_success response
assert_equal 'SUCCESS', response.message
end
Expand Down
10 changes: 1 addition & 9 deletions test/unit/gateways/rapyd_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ def setup
@ewallet_id = 'ewallet_1a867a32b47158b30a8c17d42f12f3f1'

@address_object = address(line_1: '123 State Street', line_2: 'Apt. 34', phone_number: '12125559999')

@customer_object = {
name: 'John Doe',
phone_number: '1234567890',
email: 'est@example.com',
addresses: [@address_object]
}
end

def test_successful_purchase
Expand Down Expand Up @@ -227,14 +220,13 @@ def test_failed_purchase_without_customer_object

def test_successful_purchase_with_customer_object
stub_comms(@gateway, :ssl_request) do
@options[:customer] = @customer_object
@options[:pm_type] = 'us_debit_mastercard_card'
@gateway.purchase(@amount, @credit_card, @options)
end.check_request(skip_response: true) do |_method, _endpoint, data, _headers|
assert_match(/"name":"Jim Reynolds"/, data)
assert_match(/"email":"test@example.com"/, data)
assert_match(/"phone_number":"5555555555"/, data)
assert_match(/"address1":"456 My Street","address2":"Apt 1","company":"Widgets Inc","city":"Ottawa","state":"ON","zip":"K1C2N6","country":"CA"/, data)
assert_match(/"customer":/, data)
end
end

Expand Down

0 comments on commit bb37917

Please sign in to comment.