diff --git a/lib/active_merchant/billing/gateways/rapyd.rb b/lib/active_merchant/billing/gateways/rapyd.rb index a8a3c308a49..9e41885097d 100644 --- a/lib/active_merchant/billing/gateways/rapyd.rb +++ b/lib/active_merchant/billing/gateways/rapyd.rb @@ -63,11 +63,7 @@ def verify(credit_card, options = {}) def store(payment, options = {}) post = {} add_payment(post, payment, options) - 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[:name] = "#{payment.first_name} #{payment.last_name}" unless payment.is_a?(String) - post[:addresses] = [address(options)] + add_customer_object(post, payment, options, 'store') add_metadata(post, options) add_ewallet(post, options) add_payment_fields(post, options) @@ -111,7 +107,6 @@ def add_auth_purchase(post, money, payment, 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) @@ -217,13 +212,25 @@ 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) - 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[:customer] = {} - post[:customer][:name] = "#{payment.first_name} #{payment.last_name}" unless payment.is_a?(String) - post[:customer][:addresses] = [address(options)] + def add_customer_object(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)