Skip to content

Commit 0d3d012

Browse files
committed
Fix Rapyd error on billing address
1 parent 0625c74 commit 0d3d012

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

lib/active_merchant/billing/gateways/rapyd.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def add_idempotency(options)
124124
end
125125

126126
def add_address(post, creditcard, options)
127-
return unless address = options[:billing_address]
127+
address = options[:billing_address]
128+
return unless valid_address?(address)
128129

129130
post[:address] = {}
130131
# name and line_1 are required at the gateway
@@ -138,6 +139,15 @@ def add_address(post, creditcard, options)
138139
post[:address][:phone_number] = address[:phone].gsub(/\D/, '') if address[:phone]
139140
end
140141

142+
def valid_address?(address)
143+
return false unless address
144+
145+
required_fields = %i[name address1 city state country zip]
146+
missing_fields = required_fields.select { |field| address[field].nil? || address[field].to_s.strip.empty? }
147+
148+
missing_fields.any? ? false : true
149+
end
150+
141151
def add_invoice(post, money, options)
142152
post[:amount] = money.zero? ? 0 : amount(money).to_f.to_s
143153
post[:currency] = (options[:currency] || currency(money))
@@ -265,7 +275,6 @@ def add_customer_data(post, payment, options, action = '')
265275
phone_number = options.dig(:billing_address, :phone) || options.dig(:billing_address, :phone_number)
266276
post[:phone_number] = phone_number.gsub(/\D/, '') unless phone_number.nil?
267277
post[:receipt_email] = options[:email] if payment.is_a?(String) && options[:customer_id].present? && !send_customer_object?(options)
268-
269278
return if payment.is_a?(String)
270279
return add_customer_id(post, options) if options[:customer_id]
271280

@@ -283,12 +292,15 @@ def customer_fields(payment, options)
283292
customer_data = {}
284293
customer_data[:name] = "#{payment.first_name} #{payment.last_name}" unless payment.is_a?(String)
285294
customer_data[:email] = options[:email] unless payment.is_a?(String) && options[:customer_id].blank?
295+
customer_data[:phone_number] = options.dig(:billing_address, :phone) || options.dig(:billing_address, :phone_number)
296+
customer_data[:phone_number] = customer_data[:phone_number].gsub(/\D/, '') if customer_data[:phone_number]
286297
customer_data[:addresses] = [customer_address] if customer_address
287298
customer_data
288299
end
289300

290301
def address(options)
291-
return unless address = options[:billing_address]
302+
address = options[:billing_address]
303+
return unless valid_address?(address)
292304

293305
formatted_address = {}
294306

test/remote/gateways/remote_rapyd_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def setup
99
@declined_card = credit_card('4111111111111105')
1010
@check = check
1111
@options = {
12-
pm_type: 'us_debit_visa_card',
12+
pm_type: 'GI_visa_card',
1313
currency: 'USD',
1414
complete_payment_url: 'www.google.com',
1515
error_payment_url: 'www.google.com',
@@ -70,6 +70,12 @@ def test_successful_purchase
7070
assert_equal 'SUCCESS', response.message
7171
end
7272

73+
def test_successful_purchase_without_address
74+
response = @gateway.purchase(@amount, @credit_card, @options.merge(billing_address: { phone_number: '12125559999' }))
75+
assert_success response
76+
assert_equal 'SUCCESS', response.message
77+
end
78+
7379
def test_successful_purchase_for_idempotent_requests
7480
response = @gateway.purchase(@amount, @credit_card, @options.merge(idempotency_key: '1234567890'))
7581
assert_success response

test/unit/gateways/rapyd_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ def test_successful_purchase
7272
assert_equal 'payment_716ce0efc63aa8d91579e873d29d9d5e', response.authorization.split('|')[0]
7373
end
7474

75+
def test_successful_purchase_without_address
76+
response = stub_comms(@gateway, :ssl_request) do
77+
@gateway.purchase(@amount, @credit_card, @options.merge(billing_address: { phone_number: '12125559999' }))
78+
end.check_request do |_method, _endpoint, data, _headers|
79+
assert_equal JSON.parse(data)['phone_number'], '12125559999'
80+
assert_nil JSON.parse(data)['address']
81+
assert_nil JSON.parse(data)['customer']['addresses']
82+
end.respond_with(successful_purchase_response)
83+
84+
assert_success response
85+
assert_equal 'payment_716ce0efc63aa8d91579e873d29d9d5e', response.authorization.split('|')[0]
86+
end
87+
7588
def test_send_month_and_year_with_two_digits
7689
credit_card = credit_card('4242424242424242', month: '9', year: '30')
7790
stub_comms(@gateway, :ssl_request) do

0 commit comments

Comments
 (0)