From a19392ef1be251a7c5b611b59ece490e7e30f7bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9Luis=20Torres?= Date: Fri, 23 Jun 2023 09:24:42 -0500 Subject: [PATCH] Adding the ability to handle Google Pay transactions --- lib/spreedly.rb | 1 + lib/spreedly/environment.rb | 15 ++++++- lib/spreedly/payment_methods/google_pay.rb | 6 +++ .../payment_methods/payment_method.rb | 2 + lib/spreedly/version.rb | 2 +- test/test_helper.rb | 2 +- test/unit/find_payment_method_test.rb | 12 ++++++ .../google_payment_token_stubs.rb | 7 ++++ test/unit/purchase_test.rb | 22 ++++++++++ .../find_payment_method_stubs.rb | 42 +++++++++++++++++++ 10 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 lib/spreedly/payment_methods/google_pay.rb create mode 100644 test/unit/payment_token_stubs/google_payment_token_stubs.rb diff --git a/lib/spreedly.rb b/lib/spreedly.rb index baf2cff..fa90cc8 100644 --- a/lib/spreedly.rb +++ b/lib/spreedly.rb @@ -9,6 +9,7 @@ require 'spreedly/credential' require 'spreedly/payment_methods/payment_method' require 'spreedly/payment_methods/credit_card' +require 'spreedly/payment_methods/google_pay' require 'spreedly/payment_methods/paypal' require 'spreedly/payment_methods/sprel' require 'spreedly/payment_methods/bank_account' diff --git a/lib/spreedly/environment.rb b/lib/spreedly/environment.rb index 76208e7..06d4a8a 100644 --- a/lib/spreedly/environment.rb +++ b/lib/spreedly/environment.rb @@ -175,7 +175,7 @@ def auth_purchase_body(amount, payment_method_token, options) build_xml_request('transaction') do |doc| doc.amount amount doc.currency_code(options[:currency_code] || currency_code) - doc.payment_method_token(payment_method_token) + add_payment_token(doc, payment_method_token, options) add_to_doc(doc, options, :retain_on_success) add_to_doc(doc, options, :stored_credential_initiator) add_to_doc(doc, options, :stored_credential_reason_type) @@ -183,6 +183,19 @@ def auth_purchase_body(amount, payment_method_token, options) end end + def add_payment_token(doc, payment_method_token, options = {}) + if options[:payment_method] == :google_pay + doc << <<~XML + + + #{'4111111111111111' if options[:test_mode]} + + XML + else # if credit card + doc.payment_method_token(payment_method_token) + end + end + def verify_body(payment_method_token, options) build_xml_request('transaction') do |doc| doc.payment_method_token(payment_method_token) diff --git a/lib/spreedly/payment_methods/google_pay.rb b/lib/spreedly/payment_methods/google_pay.rb new file mode 100644 index 0000000..ccabf77 --- /dev/null +++ b/lib/spreedly/payment_methods/google_pay.rb @@ -0,0 +1,6 @@ +module Spreedly + + class GooglePay < PaymentMethod + end + +end diff --git a/lib/spreedly/payment_methods/payment_method.rb b/lib/spreedly/payment_methods/payment_method.rb index 1e82c02..95f150f 100644 --- a/lib/spreedly/payment_methods/payment_method.rb +++ b/lib/spreedly/payment_methods/payment_method.rb @@ -26,6 +26,8 @@ def self.new_from(xml_doc) return BankAccount.new(xml_doc) when 'third_party_token' return ThirdPartyToken.new(xml_doc) + when 'google_pay' + return GooglePay.new(xml_doc) end end diff --git a/lib/spreedly/version.rb b/lib/spreedly/version.rb index 09c9459..307a7d3 100644 --- a/lib/spreedly/version.rb +++ b/lib/spreedly/version.rb @@ -1,3 +1,3 @@ module Spreedly - VERSION = "2.0.27" + VERSION = "2.0.28" end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8c269ba..df6cf37 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,5 @@ require 'test/unit' -require 'mocha/setup' +require 'mocha/test_unit' require 'awesome_print' require 'pry' require 'logger' diff --git a/test/unit/find_payment_method_test.rb b/test/unit/find_payment_method_test.rb index 2d57abb..c131cdc 100644 --- a/test/unit/find_payment_method_test.rb +++ b/test/unit/find_payment_method_test.rb @@ -105,6 +105,18 @@ def test_successfully_find_bank_account assert_equal([], b.errors) end + def test_successfully_find_google_pay + google_pay = find_using(successful_get_google_pay_response) + + assert_kind_of(Spreedly::GooglePay, google_pay) + assert_equal "5OHW2sAiQQMTNB06A3sOZ8UVduo", google_pay.token + assert_equal(1682445021, google_pay.created_at.to_i) + assert_equal(1682445021, google_pay.updated_at.to_i) + assert_equal("", google_pay.data) + assert_equal("google_pay", google_pay.payment_method_type) + assert_equal("cached", google_pay.storage_state) + end + private def find_using(response) @environment.stubs(:raw_ssl_request).returns(response) diff --git a/test/unit/payment_token_stubs/google_payment_token_stubs.rb b/test/unit/payment_token_stubs/google_payment_token_stubs.rb new file mode 100644 index 0000000..5e845ea --- /dev/null +++ b/test/unit/payment_token_stubs/google_payment_token_stubs.rb @@ -0,0 +1,7 @@ +module GooglePaymentTokenStubs + def google_pay_token + token = <<~TEXT + {"signature":"MEUCIA6SGVRwhOyeYRkeDUUNwB/kGtyfQAlOsg7NZydT17u/AiEA48BhWGQEF1EbEU0J+m8eSK3rTfhok9QqpiFVbME+Ky0\u003d","protocolVersion":"ECv1","signedMessage":"{\"encryptedMessage\":\"3v4IcT/eovIDP2WF8iRUy4qWQnE9Cx0vQxIZ5f9i3Emv3Tzs1AzvB7cxXhxrjp9FVIzdOwsZAPAsm03gvoYq8Xdr70XvrVRd2MFwQhMC7IV/uEsthw4JsR8oCkbI5v/zqhu2B+JodFgavNliHcpKBgijy2D6bpx7jXEkM39M/L4oBObFxFrhVSLA1GjOV6A5gLAXNXt0ffkCYekihqAyJrWlk3sCBDCF5SUiAKEIOIZtzZLgusxjVp6ufZHOHm/53uhAi6JWSJ1E6G5aaYGtULYdwgURHtxN5OIzQPYlEGctaQd5tgfCsBFfGkYyN1GRNgclbaLzAfk/Jn7/6IVKuV0ol3xubTcnjGTZXwtTjiEyYDoz1yVqB9ViMmJa55L6nBtbbAkcNEgAi7dPnrbvBGEP7QWsNT9D71g8SWrlRTCYUAOyuamaQhofG4ul1IVjmltdAy2BHBWpqgJnR9kczydQyE7uDiqhSC1/0eG8GCGIqoi8XfOioGXfMyLZ1p2ZcNK9ECjzUrH/edrwgtShxgWuWMwQTM4DQlVTAA/R4DVs192YWZcc7jm5wLqZ0+XEaPuighJM1Ps1Egeccg\\u003d\\u003d\",\"ephemeralPublicKey\":\"BA2SvF9BdCX7Tl1wwRkyLzTfqhctobhZgSugC9Cz77XNUCBOBMfFyJQt506PUs89D6IJZZfOkZopy0shRF9Uph4\\u003d\",\"tag\":\"Uhin1BE7KAuuiam7eEQFimRUDd9Xn6tZc2fClTpfrXQ\\u003d\"}"} + TEXT + end +end diff --git a/test/unit/purchase_test.rb b/test/unit/purchase_test.rb index f01cf57..a8e32a7 100644 --- a/test/unit/purchase_test.rb +++ b/test/unit/purchase_test.rb @@ -1,9 +1,11 @@ require 'test_helper' require 'unit/response_stubs/purchase_stubs' +require 'unit/payment_token_stubs/google_payment_token_stubs' class PurchaseTest < Test::Unit::TestCase include PurchaseStubs + include GooglePaymentTokenStubs def setup @environment = Spreedly::Environment.new("key", "secret") @@ -153,6 +155,26 @@ def test_request_body_params [ './continue_caching', 'true'] end + def test_request_body_params_for_google_pay + body = get_request_body(successful_purchase_response) do + @environment.purchase_on_gateway("TheGatewayToken", google_pay_token, 2001, all_possible_options.merge(payment_method: :google_pay)) + end + + transaction = body.xpath('./transaction') + assert_xpaths_in transaction, + [ './amount', '2001' ], + [ './currency_code', 'EUR' ], + [ './payment_method_token', '' ], + [ './order_id', '8675' ], + [ './description', 'SuperDuper' ], + [ './ip', '183.128.100.103' ], + [ './merchant_name_descriptor', 'Real Stuff' ], + [ './merchant_location_descriptor', 'Raleigh' ], + [ './retain_on_success', 'true' ], + [ './continue_caching', 'true'] + + assert body.to_s.include?(google_pay_token) + end private def purchase_using(response) diff --git a/test/unit/response_stubs/find_payment_method_stubs.rb b/test/unit/response_stubs/find_payment_method_stubs.rb index a70afbd..0df730b 100644 --- a/test/unit/response_stubs/find_payment_method_stubs.rb +++ b/test/unit/response_stubs/find_payment_method_stubs.rb @@ -139,4 +139,46 @@ def successful_get_bank_account_response XML end + def successful_get_google_pay_response + StubResponse.succeeded <<-XML + + 5OHW2sAiQQMTNB06A3sOZ8UVduo + 2023-04-25T17:50:21Z + 2023-04-25T17:50:21Z + + + cached + true + + + 1111 + 411111 + visa + + + 6 + 2027 + + + + + + + + + + + + + + + + + false + google_pay + + + XML + end + end