diff --git a/CHANGELOG b/CHANGELOG index c2c851595b5..af2fff09f75 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,6 +9,7 @@ * PaywayDotCom: update `live_url` [jcreiff] #4824 * Stripe & Stripe PI: Update login key validation [almalee24] #4816 * CheckoutV2: Parse the AVS and CVV checks more often [aenand] #4822 +* NMI: Add shipping_firstname, shipping_lastname, shipping_email, and surcharge fields [jcreiff] #4825 == Version 1.131.0 (June 21, 2023) * Redsys: Add supported countries [jcreiff] #4811 diff --git a/lib/active_merchant/billing/gateways/nmi.rb b/lib/active_merchant/billing/gateways/nmi.rb index 0268ae4bb23..aee8fa754a7 100644 --- a/lib/active_merchant/billing/gateways/nmi.rb +++ b/lib/active_merchant/billing/gateways/nmi.rb @@ -149,6 +149,7 @@ def add_level3_fields(post, options) def add_invoice(post, money, options) post[:amount] = amount(money) + post[:surcharge] = options[:surcharge] if options[:surcharge] post[:orderid] = options[:order_id] post[:orderdescription] = options[:description] post[:currency] = options[:currency] || currency(money) @@ -232,6 +233,9 @@ def add_customer_data(post, options) end if (shipping_address = options[:shipping_address]) + first_name, last_name = split_names(shipping_address[:name]) + post[:shipping_firstname] = first_name if first_name + post[:shipping_lastname] = last_name if last_name post[:shipping_company] = shipping_address[:company] post[:shipping_address1] = shipping_address[:address1] post[:shipping_address2] = shipping_address[:address2] @@ -240,6 +244,7 @@ def add_customer_data(post, options) post[:shipping_country] = shipping_address[:country] post[:shipping_zip] = shipping_address[:zip] post[:shipping_phone] = shipping_address[:phone] + post[:shipping_email] = options[:shipping_email] if options[:shipping_email] end if (descriptor = options[:descriptors]) diff --git a/test/remote/gateways/remote_nmi_test.rb b/test/remote/gateways/remote_nmi_test.rb index ad01c314b92..3ba0732241c 100644 --- a/test/remote/gateways/remote_nmi_test.rb +++ b/test/remote/gateways/remote_nmi_test.rb @@ -193,6 +193,26 @@ def test_successful_purchase_with_descriptors assert response.authorization end + def test_successful_purchase_with_shipping_fields + options = @options.merge({ shipping_address: shipping_address, shipping_email: 'test@example.com' }) + + assert response = @gateway.purchase(@amount, @credit_card, options) + assert_success response + assert response.test? + assert_equal 'Succeeded', response.message + assert response.authorization + end + + def test_successful_purchase_with_surcharge + options = @options.merge({ surcharge: '1.00' }) + + assert response = @gateway.purchase(@amount, @credit_card, options) + assert_success response + assert response.test? + assert_equal 'Succeeded', response.message + assert response.authorization + end + def test_failed_authorization assert response = @gateway.authorize(99, @credit_card, @options) assert_failure response diff --git a/test/unit/gateways/nmi_test.rb b/test/unit/gateways/nmi_test.rb index 7cbf9ef6510..160ea8a3f86 100644 --- a/test/unit/gateways/nmi_test.rb +++ b/test/unit/gateways/nmi_test.rb @@ -125,6 +125,70 @@ def test_purchase_with_options assert_success response end + def test_purchase_with_surcharge + options = @transaction_options.merge({ surcharge: '1.00' }) + + response = stub_comms do + @gateway.purchase(@amount, @credit_card, options) + end.check_request do |_endpoint, data, _headers| + test_transaction_options(data) + + assert_match(/surcharge=1.00/, data) + end.respond_with(successful_purchase_response) + + assert_success response + end + + def test_purchase_with_shipping_fields + options = @transaction_options.merge({ shipping_address: shipping_address }) + + response = stub_comms do + @gateway.purchase(@amount, @credit_card, options) + end.check_request do |_endpoint, data, _headers| + test_transaction_options(data) + + assert_match(/shipping_firstname=Jon/, data) + assert_match(/shipping_lastname=Smith/, data) + assert_match(/shipping_address1=123\+Your\+Street/, data) + assert_match(/shipping_address2=Apt\+2/, data) + assert_match(/shipping_city=Toronto/, data) + assert_match(/shipping_state=ON/, data) + assert_match(/shipping_country=CA/, data) + assert_match(/shipping_zip=K2C3N7/, data) + end.respond_with(successful_purchase_response) + + assert_success response + end + + def test_purchase_with_shipping_fields_omits_blank_name + options = @transaction_options.merge({ shipping_address: shipping_address(name: nil) }) + + response = stub_comms do + @gateway.purchase(@amount, @credit_card, options) + end.check_request do |_endpoint, data, _headers| + test_transaction_options(data) + + refute_match(/shipping_firstname/, data) + refute_match(/shipping_lastname/, data) + end.respond_with(successful_purchase_response) + + assert_success response + end + + def test_purchase_with_shipping_email + options = @transaction_options.merge({ shipping_address: shipping_address, shipping_email: 'test@example.com' }) + + response = stub_comms do + @gateway.purchase(@amount, @credit_card, options) + end.check_request do |_endpoint, data, _headers| + test_transaction_options(data) + + assert_match(/shipping_email=test%40example\.com/, data) + end.respond_with(successful_purchase_response) + + assert_success response + end + def test_failed_purchase response = stub_comms do @gateway.purchase(@amount, @credit_card)