Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Adding the ability to handle Google Pay transactions #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/spreedly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 14 additions & 1 deletion lib/spreedly/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,27 @@ 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)
add_extra_options_for_basic_ops(doc, options)
end
end

def add_payment_token(doc, payment_method_token, options = {})
if options[:payment_method] == :google_pay
doc << <<~XML
<google_pay>
<payment_data><![CDATA[#{payment_method_token}]]></payment_data>
#{'<test_card_number>4111111111111111</test_card_number>' if options[:test_mode]}
</google_pay>
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)
Expand Down
6 changes: 6 additions & 0 deletions lib/spreedly/payment_methods/google_pay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Spreedly

class GooglePay < PaymentMethod
end

end
2 changes: 2 additions & 0 deletions lib/spreedly/payment_methods/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/spreedly/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Spreedly
VERSION = "2.0.27"
VERSION = "2.0.28"
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'test/unit'
require 'mocha/setup'
require 'mocha/test_unit'
require 'awesome_print'
require 'pry'
require 'logger'
Expand Down
12 changes: 12 additions & 0 deletions test/unit/find_payment_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/unit/payment_token_stubs/google_payment_token_stubs.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions test/unit/purchase_test.rb
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions test/unit/response_stubs/find_payment_method_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,46 @@ def successful_get_bank_account_response
XML
end

def successful_get_google_pay_response
StubResponse.succeeded <<-XML
<payment_method>
<token>5OHW2sAiQQMTNB06A3sOZ8UVduo</token>
<created_at type=\"dateTime\">2023-04-25T17:50:21Z</created_at>
<updated_at type=\"dateTime\">2023-04-25T17:50:21Z</updated_at>
<email nil=\"true\"/>
<data nil=\"true\"/>
<storage_state>cached</storage_state>
<test type=\"boolean\">true</test>
<metadata nil=\"true\"/>
<callback_url nil=\"true\"/>
<last_four_digits>1111</last_four_digits>
<first_six_digits>411111</first_six_digits>
<card_type>visa</card_type>
<first_name nil=\"true\"/>
<last_name nil=\"true\"/>
<month type=\"integer\">6</month>
<year type=\"integer\">2027</year>
<full_name/>
<address1 nil=\"true\"/>
<address2 nil=\"true\"/>
<city nil=\"true\"/>
<state nil=\"true\"/>
<zip nil=\"true\"/>
<country nil=\"true\"/>
<phone_number nil=\"true\"/>
<company nil=\"true\"/>
<shipping_address1 nil=\"true\"/>
<shipping_address2 nil=\"true\"/>
<shipping_city nil=\"true\"/>
<shipping_state nil=\"true\"/>
<shipping_zip nil=\"true\"/>
<shipping_country nil=\"true\"/>
<shipping_phone_number nil=\"true\"/>
<eligible_for_card_updater type=\"boolean\">false</eligible_for_card_updater>
<payment_method_type>google_pay</payment_method_type>
<errors></errors>
</payment_method>
XML
end

end