Skip to content

Commit

Permalink
TNS: Support asia_pacific endpoint
Browse files Browse the repository at this point in the history
The remote test for this feature will fail by default.  See the comment
on test_successful_purchase_with_region describing how to make it work.

Closes activemerchant#2140
  • Loading branch information
curiousepic authored and duff committed Jun 20, 2016
1 parent 5da28b1 commit 5ca4241
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* MaxiPago: Add verify and scrub [shasum]
* CyberSource: Assign default with override for billing address and email [shasum]
* CyberSource: Assign default order_id [duff]
* TNS: Support asia_pacific endpoint [curiousepic]

== Version 1.59.0 (May 18, 2016)
* Orbital: Allow AVS parts to be sent sans country [duff]
Expand Down
11 changes: 9 additions & 2 deletions lib/active_merchant/billing/gateways/tns.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class TnsGateway < Gateway
class_attribute :live_na_url, :live_ap_url

self.display_name = 'TNS'
self.homepage_url = 'http://www.tnsi.com/'

# Testing is partitioned by account.
self.live_url = 'https://secure.na.tnspayments.com/api/rest/version/22/'
self.live_na_url = 'https://secure.na.tnspayments.com/api/rest/version/22/'
self.live_ap_url = 'https://secure.ap.tnspayments.com/api/rest/version/22/'

self.supported_countries = %w(AR AU BR FR DE HK MX NZ SG GB US)

Expand All @@ -14,6 +17,9 @@ class TnsGateway < Gateway

def initialize(options={})
requires!(options, :userid, :password)

options[:region] = 'north_america' unless options[:region]

super
end

Expand Down Expand Up @@ -174,7 +180,8 @@ def commit(action, post)
end

def build_url(orderid, transactionid)
"#{live_url}merchant/#{@options[:userid]}/order/#{orderid}/transaction/#{transactionid}"
base_url = @options[:region] == 'asia_pacific' ? live_ap_url : live_na_url
"#{base_url}merchant/#{@options[:userid]}/order/#{orderid}/transaction/#{transactionid}"
end

def build_request(post = {})
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,10 @@ tns:
userid: TESTSPREEDLY01
password: 3f34fe50334fbe6cbe04c283411a5860

tns_ap:
userid: TESTUNISOLMAA01
password: b7f8119fda3bd27c17656badb52c95bb

trans_first:
login: 45567
password: TNYYKYMFZ59HSN7Q
Expand Down
10 changes: 10 additions & 0 deletions test/remote/gateways/remote_tns_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def setup

@amount = 100
@credit_card = credit_card('5123456789012346')
@ap_credit_card = credit_card('5424180279791732', month: 05, year: 2017, verification_value: 222)
@declined_card = credit_card('4000300011112220')

@options = {
Expand Down Expand Up @@ -38,6 +39,15 @@ def test_successful_purchase_with_more_options
assert_equal "Succeeded", response.message
end

# The region-specific credentials will fail OpenSSL certificate verification
# To test, in connection.rb, configure_ssl's verify_mode must be VERIFY_NONE
def test_successful_purchase_with_region
@gateway = TnsGateway.new(fixtures(:tns_ap).merge(region: 'asia_pacific'))

assert response = @gateway.purchase(@amount, @ap_credit_card, @options.merge(currency: "AUD"))
assert_success response
assert_equal "Succeeded", response.message
end

def test_failed_purchase
assert response = @gateway.purchase(@amount, @declined_card, @options)
Expand Down
32 changes: 32 additions & 0 deletions test/unit/gateways/tns_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,38 @@ def test_unsuccessful_verify
assert_equal "FAILURE - DECLINED", response.message
end

def test_north_america_region_url
@gateway = TnsGateway.new(
userid: 'userid',
password: 'password',
region: 'north_america'
)

response = stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, @credit_card, @options)
end.check_request do |method, endpoint, data, headers|
assert_match(/secure.na.tnspayments.com/, endpoint)
end.respond_with(successful_capture_response)

assert_success response
end

def test_asia_pacific_region_url
@gateway = TnsGateway.new(
userid: 'userid',
password: 'password',
region: 'asia_pacific'
)

response = stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, @credit_card, @options)
end.check_request do |method, endpoint, data, headers|
assert_match(/secure.ap.tnspayments.com/, endpoint)
end.respond_with(successful_capture_response)

assert_success response
end

private

def successful_authorize_response
Expand Down

0 comments on commit 5ca4241

Please sign in to comment.