Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic for ApplePay #2

Merged
merged 4 commits into from
Jul 6, 2023
Merged
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 @@ -10,6 +10,7 @@
require 'spreedly/payment_methods/payment_method'
require 'spreedly/payment_methods/credit_card'
require 'spreedly/payment_methods/google_pay'
require 'spreedly/payment_methods/apple_pay'
require 'spreedly/payment_methods/paypal'
require 'spreedly/payment_methods/sprel'
require 'spreedly/payment_methods/bank_account'
Expand Down
7 changes: 7 additions & 0 deletions lib/spreedly/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ def add_payment_token(doc, payment_method_token, options = {})
#{'<test_card_number>4111111111111111</test_card_number>' if options[:test_mode]}
</google_pay>
XML
elsif options[:payment_method] == :apple_pay
doc << <<~XML
<apple_pay>
<payment_data><![CDATA[#{payment_method_token}]]></payment_data>
#{'<test_card_number>4111111111111111</test_card_number>' if options[:test_mode]}
</apple_pay>
XML
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably do something fancier here in which we use the value of the options[:payment_method] as the value for <apple_pay> or <google_pay>

Example:

doc << <<~XML
  <#{options[:paymet_method]}
    ....
  </#{options[:paymet_method]}

else # if credit card
doc.payment_method_token(payment_method_token)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/spreedly/payment_methods/apple_pay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Spreedly

class ApplePay < PaymentMethod

end

Comment on lines +2 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the extra space?

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 @@ -28,6 +28,8 @@ def self.new_from(xml_doc)
return ThirdPartyToken.new(xml_doc)
when 'google_pay'
return GooglePay.new(xml_doc)
when 'apple_pay'
return ApplePay.new(xml_doc)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an issue, just a Ruby newbie question about performance. When ordering a "case" statement, is any thought given to ordering the "when"s so that the most likely hits are at the top, and the least likely are at the bottom?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably up to the individual engineer, and whether there is a real world difference between the options. So yes, in some cases, but there is nothing dogmatic that we follow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never really thought about it, I guess is you have a very large case statement it could have an effect but in general i would say the gains would be minimal.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree, in this case the gain would be marginal since each option is a simple string.

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.28"
VERSION = "2.0.29"
end
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 @@ -117,6 +117,18 @@ def test_successfully_find_google_pay
assert_equal("cached", google_pay.storage_state)
end

def test_successfully_find_apple_pay
apple_pay = find_using(successful_get_apple_pay_response)

assert_kind_of(Spreedly::ApplePay, apple_pay)
assert_equal "RgGkf6vbV2QOuibYDq5F2hBjQy8", apple_pay.token
assert_equal(1501177975, apple_pay.created_at.to_i)
assert_equal(1501177975, apple_pay.updated_at.to_i)
assert_equal("", apple_pay.data)
assert_equal("apple_pay", apple_pay.payment_method_type)
assert_equal("cached", apple_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/apple_payment_token_stubs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ApplePaymentTokenStubs
def apple_pay_token
token = <<~TEXT
{\"signature\":\"MEYCIQD/JoCQ2AMHS0o1bXH4BhfosEsndtXU0jBhXrlFRXqHVgIhAMbYV8JOR1ysS6rrV4vwF+S8/I0bBDytRMEdItlHkXEQ\",\"protocolVersion\":\"ECv1\",\"signedMessage\":\"{\\\"encryptedMessage\\\":\\\"tJUnNLW8HBaDS3w6Q959I0WdTbo3IE9P1TMAFb/OhAQoRhV+qaiwQ0GTOMI7Dmzl1bfaMnYBFh8yAbCw6RMnzK71AZLwwXkDknUpxwtU1QJds2k2SINjiuBrQyt4VpgBFjVAzV9SWlCW+Mm9qfqAzN4N4mFgsvNw0GDlbzgHpjHyjKev/HAH4Gk98bLjXh+cmG9pL9hk1NIooLZPJ4I5q34Ri1dChKIXoQRScBYYFXzt3tk4u8ykiqCqPydjAls3vujNceKKj0SIRaixlpJ+jRJgK04XL3OOe4EzHd476ElKSwVfg96t5Y/ScJ1lPav2FNiCIekHevyhyIIiN9Vun3OsZ6wSRkSWaIkeflsK2potCpb2MzAdAgc1qQIdqBc3x9x1aLS/jEKgPY/gA1gnAyrbb8Ol/RF8W0FCaCE/A/DqPY+YdH92sUHBuOgmarPPU4KhZkshF09cD6hFMKMZBezYgFikXQ\\\\u003d\\\\u003d\\\",\\\"ephemeralPublicKey\\\":\\\"BKX7sb9PXjbdL8GsfEzcAiVYkM+QBJXt/L2yGXHjyJM7+NO7SlyPyXe3wK6+ky16ujphhwRy10c73LPANrwPTXY\\\\u003d\\\",\\\"tag\\\":\\\"e7DeYqVtIKIEZWmXAvA5OTZ2HwPSxyfCyrAAn7XEbm4\\\\u003d\\\"}\"}
TEXT
end
end
23 changes: 23 additions & 0 deletions test/unit/purchase_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'test_helper'
require 'unit/response_stubs/purchase_stubs'
require 'unit/payment_token_stubs/google_payment_token_stubs'
require 'unit/payment_token_stubs/apple_payment_token_stubs'

class PurchaseTest < Test::Unit::TestCase

include PurchaseStubs
include GooglePaymentTokenStubs
include ApplePaymentTokenStubs

def setup
@environment = Spreedly::Environment.new("key", "secret")
Expand Down Expand Up @@ -176,6 +178,27 @@ def test_request_body_params_for_google_pay
assert body.to_s.include?(google_pay_token)
end

def test_request_body_params_for_apple_pay
body = get_request_body(successful_purchase_response) do
@environment.purchase_on_gateway("TheGatewayToken", apple_pay_token, 2001, all_possible_options.merge(payment_method: :apple_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?(apple_pay_token)
end

private
def purchase_using(response)
@environment.stubs(:raw_ssl_request).returns(response)
Expand Down
40 changes: 40 additions & 0 deletions test/unit/response_stubs/find_payment_method_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,44 @@ def successful_get_google_pay_response
XML
end

def successful_get_apple_pay_response
StubResponse.succeeded <<-XML
<payment_method>
<token>RgGkf6vbV2QOuibYDq5F2hBjQy8</token>
<created_at type="dateTime">2017-07-27T17:52:55Z</created_at>
<updated_at type="dateTime">2017-07-27T17:52:55Z</updated_at>
<email nil="true"/>
<data nil="true"/>
<storage_state>cached</storage_state>
<test type="boolean">true</test>
<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">3</month>
<year type="integer">2018</year>
<full_name></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"/>
<payment_method_type>apple_pay</payment_method_type>
<errors>
</errors>
</payment_method>
XML
end

end