Skip to content

Commit

Permalink
Worldpay: Add customStringFields
Browse files Browse the repository at this point in the history
These fields are included as part of FraudSightData in auth/purchase requests
https://docs.worldpay.com/apis/wpg/fraudsightglobal/fraudsightdirect

CER-1766

LOCAL
6040 tests, 80501 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

801 files inspected, no offenses detected

UNIT
125 tests, 705 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

REMOTE
112 tests, 473 assertions, 3 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
97.3214% passed
  • Loading branch information
jcreiff committed Oct 4, 2024
1 parent 2981c50 commit b964d73
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* Cybersource Rest: Add support for recurring Apple Pay [bdcano] #5270
* Cybersource Rest: Update message and error_code [almalee24] #5276
* Paysafe: Add support for `external_initial_transaction_id` [rachelkirk] #5291
* Worldpay: Add customStringFields [jcreiff] #5284

== Version 1.137.0 (August 2, 2024)
* Unlock dependency on `rexml` to allow fixing a CVE (#5181).
Expand Down
15 changes: 15 additions & 0 deletions lib/active_merchant/billing/gateways/worldpay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def build_authorization_request(money, payment_method, options)
add_order_content(xml, options)
add_payment_method(xml, money, payment_method, options)
add_shopper(xml, options)
add_fraud_sight_data(xml, options)
add_statement_narrative(xml, options)
add_risk_data(xml, options[:risk_data]) if options[:risk_data]
add_sub_merchant_data(xml, options[:sub_merchant_data]) if options[:sub_merchant_data]
Expand Down Expand Up @@ -792,6 +793,20 @@ def add_shopper(xml, options)
end
end

def add_fraud_sight_data(xml, options)
return unless options[:custom_string_fields].is_a?(Hash)

xml.tag! 'FraudSightData' do
xml.tag! 'customStringFields' do
options[:custom_string_fields].each do |key, value|
# transform custom_string_field_1 into customStringField1, etc.
formatted_key = key.to_s.camelize(:lower).to_sym
xml.tag! formatted_key, value
end
end
end
end

def add_statement_narrative(xml, options)
xml.statementNarrative truncate(options[:statement_narrative], 50) if options[:statement_narrative]
end
Expand Down
14 changes: 14 additions & 0 deletions test/remote/gateways/remote_worldpay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,20 @@ def test_successful_purchase_with_level_two_and_three_fields
assert_equal 'SUCCESS', response.message
end

def test_successful_purchase_with_custom_string_fields
assert response = @gateway.purchase(@amount, @credit_card, @options.merge(custom_string_fields: { custom_string_field_1: 'testvalue1', custom_string_field_2: 'testvalue2' }))
assert_success response
assert_equal true, response.params['ok']
assert_equal 'SUCCESS', response.message
end

def test_failed_purchase_with_blank_custom_string_field
assert response = @gateway.purchase(@amount, @credit_card, @options.merge(custom_string_fields: { custom_string_field_1: '' }))
assert_failure response

assert_equal "The tag 'customStringField1' cannot be empty", response.message
end

# Fails currently because the sandbox doesn't actually validate the stored_credential options
# def test_failed_authorize_with_bad_stored_cred_options
# assert auth = @gateway.authorize(@amount, @credit_card, @options.merge(stored_credential_usage: 'FIRST'))
Expand Down
13 changes: 13 additions & 0 deletions test/unit/gateways/worldpay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,19 @@ def test_transaction_with_level_three_data
assert_success response
end

def test_successful_purchase_with_custom_string_fields
options = @options.merge(custom_string_fields: { custom_string_field_1: 'testvalue1', custom_string_field_2: 'testvalue2' })
response = stub_comms do
@gateway.authorize(@amount, @credit_card, options)
end.check_request do |_endpoint, data, _headers|
assert_match %r(<FraudSightData>\n), data
assert_match %r(<customStringFields>\n), data
assert_match %r(<customStringField1>testvalue1</customStringField1>), data
assert_match %r(<customStringField2>testvalue2</customStringField2>), data
end.respond_with(successful_authorize_response)
assert_success response
end

def test_successful_purchase_with_sub_merchant_data
options = @options.merge(@sub_merchant_options)
response = stub_comms do
Expand Down

0 comments on commit b964d73

Please sign in to comment.