Skip to content

Commit 49505e6

Browse files
committed
Add cybersource void
1 parent 5b56638 commit 49505e6

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

lib/active_merchant/billing/gateways/cyber_source.rb

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'nokogiri'
2+
13
module ActiveMerchant # :nodoc:
24
module Billing # :nodoc:
35
# Initial setup instructions can be found in
@@ -202,8 +204,8 @@ def purchase(money, payment_method, options = {})
202204
end
203205
end
204206

205-
def void(identification, options = {})
206-
commit(build_void_request(identification, options), :void, nil, options)
207+
def void(money, identification, options = {})
208+
commit(build_void_request(money, identification, options), :void, nil, options)
207209
end
208210

209211
def refund(money, identification, options = {})
@@ -466,11 +468,14 @@ def reference_is_a_check?(payment_method_or_reference)
466468
payment_method_or_reference.is_a?(String) && payment_method_or_reference.split(';')[7] == 'check'
467469
end
468470

469-
def build_void_request(identification, options)
470-
order_id, request_id, request_token, action, money, currency = identification.split(';')
471-
options[:order_id] = order_id
472-
471+
def build_void_request(money, identification, options)
473472
xml = Builder::XmlMarkup.new indent: 2
473+
if identification.nil?
474+
request_id = nil
475+
else
476+
order_id, request_id, request_token, action, money, currency = identification.split(';')
477+
options[:order_id] = order_id
478+
end
474479
case action
475480
when 'capture', 'purchase'
476481
add_mdd_fields(xml, options)
@@ -1016,7 +1021,7 @@ def add_void_service(xml, request_id, request_token)
10161021

10171022
def add_auth_reversal_service(xml, request_id, request_token)
10181023
xml.tag! 'ccAuthReversalService', { 'run' => 'true' } do
1019-
xml.tag! 'authRequestID', request_id
1024+
xml.tag! 'authRequestID', request_id unless request_id.nil?
10201025
xml.tag! 'authRequestToken', request_token
10211026
end
10221027
end
@@ -1053,6 +1058,7 @@ def add_subscription_retrieve_service(xml, options)
10531058
end
10541059

10551060
def add_merchant_category_code(xml, options)
1061+
xml.tag! 'merchantTransactionIdentifier', options[:merchant_identifier] if options[:merchant_identifier]
10561062
xml.tag! 'merchantCategoryCode', options[:merchant_category_code] if options[:merchant_category_code]
10571063
end
10581064

@@ -1196,6 +1202,7 @@ def add_partner_solution_id(xml)
11961202
# Where we actually build the full SOAP request using builder
11971203
def build_request(body, options)
11981204
xsd_version = test? ? TEST_XSD_VERSION : PRODUCTION_XSD_VERSION
1205+
validate_with_xsd(body, options)
11991206

12001207
xml = Builder::XmlMarkup.new indent: 2
12011208
xml.instruct!
@@ -1332,6 +1339,23 @@ def eligible_for_zero_auth?(payment_method, options = {})
13321339
def format_routing_number(routing_number, options)
13331340
options[:currency] == 'CAD' && routing_number.length > 8 ? routing_number[-8..-1] : routing_number
13341341
end
1342+
1343+
def validate_with_xsd(xml_body, options, xsd_path: '/Users/gsanmartin/Documents/Spreedly/active_merchant/test/schema/cyber_source/CyberSourceTransaction_1.201.xsd')
1344+
xsd = Nokogiri::XML::Schema(File.read(xsd_path))
1345+
xml = Builder::XmlMarkup.new indent: 2
1346+
1347+
xsd_version = test? ? TEST_XSD_VERSION : PRODUCTION_XSD_VERSION
1348+
1349+
xml.tag! 'requestMessage', { 'xmlns' => "urn:schemas-cybersource-com:transaction-data-#{xsd_version}" } do
1350+
add_merchant_data(xml, options)
1351+
xml << xml_body
1352+
end
1353+
xml_t = xml.target!
1354+
puts xml_t
1355+
doc = Nokogiri::XML(xml_t)
1356+
errors = xsd.validate(doc)
1357+
errors.empty? ? true : puts(errors.map(&:message))
1358+
end
13351359
end
13361360
end
13371361
end

test/remote/gateways/remote_cyber_source_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,14 @@ def test_purchase_and_void
411411
assert_successful_response(void)
412412
end
413413

414+
def test_void_with_no_authorization_value
415+
merchant_identifier = 'testTransaction131' + SecureRandom.hex(3)
416+
assert authorize = @gateway.authorize(@amount, @credit_card, @options.merge(merchant_identifier:))
417+
assert_successful_response(authorize)
418+
assert void = @gateway.void(@amount, nil, @options.merge({ merchant_identifier: }))
419+
assert_successful_response(void)
420+
end
421+
414422
def test_purchase_and_void_with_merchant_category_code
415423
assert purchase = @gateway.purchase(@amount, @credit_card, @options)
416424
assert_successful_response(purchase)

0 commit comments

Comments
 (0)