Skip to content

Commit

Permalink
Stripe: Remove StripePaymenToken & ApplePayPaymentToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Alma Malambo committed Oct 10, 2024
1 parent f974b4c commit 43a62c6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 291 deletions.
59 changes: 9 additions & 50 deletions lib/active_merchant/billing/gateways/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,10 @@ def authorize(money, payment, options = {})
return Response.new(false, direct_bank_error)
end

MultiResponse.run do |r|
if payment.is_a?(ApplePayPaymentToken)
r.process { tokenize_apple_pay_token(payment) }
payment = StripePaymentToken.new(r.params['token']) if r.success?
end
r.process do
post = create_post_for_auth_or_purchase(money, payment, options)
add_application_fee(post, options) if emv_payment?(payment)
post[:capture] = 'false'
commit(:post, 'charges', post, options)
end
end.responses.last
post = create_post_for_auth_or_purchase(money, payment, options)
add_application_fee(post, options) if emv_payment?(payment)
post[:capture] = 'false'
commit(:post, 'charges', post, options)
end

# To create a charge on a card or a token, call
Expand All @@ -115,17 +107,9 @@ def purchase(money, payment, options = {})
return Response.new(false, direct_bank_error)
end

MultiResponse.run do |r|
if payment.is_a?(ApplePayPaymentToken)
r.process { tokenize_apple_pay_token(payment) }
payment = StripePaymentToken.new(r.params['token']) if r.success?
end
r.process do
post = create_post_for_auth_or_purchase(money, payment, options)
post[:card][:processing_method] = 'quick_chip' if quickchip_payment?(payment)
commit(:post, 'charges', post, options)
end
end.responses.last
post = create_post_for_auth_or_purchase(money, payment, options)
post[:card][:processing_method] = 'quick_chip' if quickchip_payment?(payment)
commit(:post, 'charges', post, options)
end

def capture(money, authorization, options = {})
Expand Down Expand Up @@ -199,12 +183,7 @@ def store(payment, options = {})
params = {}
post = {}

if payment.is_a?(ApplePayPaymentToken)
token_exchange_response = tokenize_apple_pay_token(payment)
params = { card: token_exchange_response.params['token']['id'] } if token_exchange_response.success?
elsif payment.is_a?(StripePaymentToken)
add_payment_token(params, payment, options)
elsif payment.is_a?(Check)
if payment.is_a?(Check)
bank_token_response = tokenize_bank_account(payment)
return bank_token_response unless bank_token_response.success?

Expand Down Expand Up @@ -255,17 +234,6 @@ def unstore(identification, options = {}, deprecated_options = {})
commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", nil, options)
end

def tokenize_apple_pay_token(apple_pay_payment_token, options = {})
token_response = api_request(:post, "tokens?pk_token=#{CGI.escape(apple_pay_payment_token.payment_data.to_json)}")
success = !token_response.key?('error')

if success && token_response.key?('id')
Response.new(success, nil, token: token_response)
else
Response.new(success, token_response['error']['message'])
end
end

def verify_credentials
begin
ssl_get(live_url + 'charges/nonexistent', headers)
Expand Down Expand Up @@ -368,12 +336,7 @@ def list_webhook_endpoints(options)
def create_post_for_auth_or_purchase(money, payment, options)
post = {}

if payment.is_a?(StripePaymentToken)
add_payment_token(post, payment, options)
else
add_creditcard(post, payment, options)
end

add_creditcard(post, payment, options)
add_charge_details(post, money, payment, options)
post
end
Expand Down Expand Up @@ -537,10 +500,6 @@ def add_emv_creditcard(post, icc_data, options = {})
post[:card] = { emv_auth_data: icc_data }
end

def add_payment_token(post, token, options = {})
post[:card] = token.payment_data['id']
end

def add_customer(post, payment, options)
post[:customer] = options[:customer] if options[:customer] && !payment.respond_to?(:number)
end
Expand Down
12 changes: 2 additions & 10 deletions lib/active_merchant/billing/gateways/stripe_payment_intents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def store(payment_method, options = {})
if new_apple_google_pay_flow(payment_method, options)
options[:customer] = customer(payment_method, options).params['id'] unless options[:customer]
verify(payment_method, options.merge!(action: :store))
elsif payment_method.is_a?(StripePaymentToken) || payment_method.is_a?(ActiveMerchant::Billing::CreditCard)
elsif payment_method.is_a?(ActiveMerchant::Billing::CreditCard)
result = add_payment_method_token(params, payment_method, options)
return result if result.is_a?(ActiveMerchant::Billing::Response)

Expand Down Expand Up @@ -405,14 +405,6 @@ def add_return_url(post, options)

def add_payment_method_token(post, payment_method, options, responses = [])
case payment_method
when StripePaymentToken
post[:payment_method_data] = {
type: 'card',
card: {
token: payment_method.payment_data['id'] || payment_method.payment_data
}
}
post[:payment_method] = payment_method.payment_data['id'] || payment_method.payment_data
when String
extract_token_from_string_and_maybe_add_customer_id(post, payment_method)
when ActiveMerchant::Billing::CreditCard
Expand Down Expand Up @@ -691,7 +683,7 @@ def setup_future_usage(post, options = {})
end

def add_billing_address(post, payment_method, options = {})
return if payment_method.nil? || payment_method.is_a?(StripePaymentToken) || payment_method.is_a?(String)
return if payment_method.nil? || payment_method.is_a?(String)

post[:payment_method_data] ||= {}
if billing = options[:billing_address] || options[:address]
Expand Down
86 changes: 0 additions & 86 deletions test/remote/gateways/remote_stripe_apple_pay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,92 +12,6 @@ def setup
description: 'ActiveMerchant Test Purchase',
email: '[email protected]'
}
@apple_pay_payment_token = apple_pay_payment_token
end

def test_successful_purchase_with_apple_pay_payment_token
assert response = @gateway.purchase(@amount, @apple_pay_payment_token, @options)
assert_success response
assert_equal 'charge', response.params['object']
assert response.params['paid']
assert_equal 'ActiveMerchant Test Purchase', response.params['description']
assert_equal '[email protected]', response.params['metadata']['email']
assert_match CHARGE_ID_REGEX, response.authorization
end

def test_authorization_and_capture_with_apple_pay_payment_token
assert authorization = @gateway.authorize(@amount, @apple_pay_payment_token, @options)
assert_success authorization
refute authorization.params['captured']
assert_equal 'ActiveMerchant Test Purchase', authorization.params['description']
assert_equal '[email protected]', authorization.params['metadata']['email']

assert capture = @gateway.capture(@amount, authorization.authorization)
assert_success capture
end

def test_authorization_and_void_with_apple_pay_payment_token
assert authorization = @gateway.authorize(@amount, @apple_pay_payment_token, @options)
assert_success authorization
refute authorization.params['captured']

assert void = @gateway.void(authorization.authorization)
assert_success void
end

def test_successful_void_with_apple_pay_payment_token
assert response = @gateway.purchase(@amount, @apple_pay_payment_token, @options)
assert_success response
assert response.authorization
assert void = @gateway.void(response.authorization)
assert_success void
end

def test_successful_store_with_apple_pay_payment_token
assert response = @gateway.store(@apple_pay_payment_token, { description: 'Active Merchant Test Customer', email: '[email protected]' })
assert_success response
assert_equal 'customer', response.params['object']
assert_equal 'Active Merchant Test Customer', response.params['description']
assert_equal '[email protected]', response.params['email']
first_card = response.params['cards']['data'].first
assert_equal response.params['default_card'], first_card['id']
assert_equal '4242', first_card['dynamic_last4'] # when stripe is in test mode, token exchanged will return a test card with dynamic_last4 4242
assert_equal '0000', first_card['last4'] # last4 is 0000 when using an apple pay token
end

def test_successful_store_with_existing_customer_and_apple_pay_payment_token
assert response = @gateway.store(@credit_card, { description: 'Active Merchant Test Customer' })
assert_success response

assert response = @gateway.store(@apple_pay_payment_token, { customer: response.params['id'], description: 'Active Merchant Test Customer', email: '[email protected]' })
assert_success response
assert_equal 2, response.responses.size

card_response = response.responses[0]
assert_equal 'card', card_response.params['object']
assert_equal '4242', card_response.params['dynamic_last4'] # when stripe is in test mode, token exchanged will return a test card with dynamic_last4 4242
assert_equal '0000', card_response.params['last4'] # last4 is 0000 when using an apple pay token

customer_response = response.responses[1]
assert_equal 'customer', customer_response.params['object']
assert_equal 'Active Merchant Test Customer', customer_response.params['description']
assert_equal '[email protected]', customer_response.params['email']
assert_equal 2, customer_response.params['cards']['count']
end

def test_successful_recurring_with_apple_pay_payment_token
assert response = @gateway.store(@apple_pay_payment_token, { description: 'Active Merchant Test Customer', email: '[email protected]' })
assert_success response
assert recharge_options = @options.merge(customer: response.params['id'])
assert response = @gateway.purchase(@amount, nil, recharge_options)
assert_success response
assert_equal 'charge', response.params['object']
assert response.params['paid']
end

def test_purchase_with_unsuccessful_apple_pay_token_exchange
assert response = @gateway.purchase(@amount, ApplePayPaymentToken.new('garbage'), @options)
assert_failure response
end

def test_successful_purchase_with_apple_pay_raw_cryptogram_with_eci
Expand Down
Loading

0 comments on commit 43a62c6

Please sign in to comment.