Skip to content

Commit

Permalink
StripePI: Update version to 2022-11-15
Browse files Browse the repository at this point in the history
Update version to 2022-11-15 and charges to be
latest_charge. latest_charge is no longer
automatically expanded andwill only be expanded
if the repsonse is succesful.

Remote
99 tests, 463 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
98.9899% passed
  • Loading branch information
Alma Malambo committed Dec 4, 2024
1 parent 80f1d1c commit 320b2ec
Show file tree
Hide file tree
Showing 5 changed files with 841 additions and 584 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
* Decidir and DecicirPlus: Add the wallet_id field [yunnydang] #5354
* Worldpay: Update where to pass shopperIPAddress [almalee24] #5348
* Braintree: Account for BraintreeError [almalee24] #5346
* Worldpay: Fix stored credentials unscheduled reason type [Buitragox] #5352
* Worldpay: Fix stored credentials unscheduled reason type [Buitragox] #5352
* StripePI: Update version to 2022-11-15 [almalee24] #5314

== Version 1.137.0 (August 2, 2024)
* Unlock dependency on `rexml` to allow fixing a CVE (#5181).
Expand Down
9 changes: 3 additions & 6 deletions lib/active_merchant/billing/gateways/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def add_level_three(post, options)
post[:level3] = level_three unless level_three.empty?
end

def add_expand_parameters(post, options)
def add_expand_parameters(method, url, post, options)
post[:expand] ||= []
post[:expand].concat(Array.wrap(options[:expand]).map(&:to_sym)).uniq!
end
Expand Down Expand Up @@ -681,7 +681,7 @@ def api_request(method, endpoint, parameters = nil, options = {})
end

def commit(method, url, parameters = nil, options = {})
add_expand_parameters(parameters, options) if parameters
add_expand_parameters(method, url, parameters, options) if parameters
return Response.new(false, 'Invalid API Key provided') unless key_valid?(options)

response = api_request(method, url, parameters, options)
Expand Down Expand Up @@ -786,10 +786,7 @@ def quickchip_payment?(payment)
end

def card_from_response(response)
# StripePI puts the AVS and CVC check significantly deeper into the response object
response['card'] || response['active_card'] || response['source'] ||
response.dig('charges', 'data', 0, 'payment_method_details', 'card', 'checks') ||
response.dig('latest_attempt', 'payment_method_details', 'card', 'checks') || {}
response['card'] || response['active_card'] || response['source'] || {}
end

def emv_authorization_from_response(response)
Expand Down
34 changes: 28 additions & 6 deletions lib/active_merchant/billing/gateways/stripe_payment_intents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StripePaymentIntentsGateway < StripeGateway
CREATE_INTENT_ATTRIBUTES = %i[description statement_descriptor_suffix statement_descriptor receipt_email save_payment_method]
CONFIRM_INTENT_ATTRIBUTES = %i[receipt_email return_url save_payment_method setup_future_usage off_session]
UPDATE_INTENT_ATTRIBUTES = %i[description statement_descriptor_suffix statement_descriptor receipt_email setup_future_usage]
DEFAULT_API_VERSION = '2020-08-27'
DEFAULT_API_VERSION = '2022-11-15'
DIGITAL_WALLETS = {
apple_pay: 'apple_pay',
google_pay: 'google_pay_dpan'
Expand Down Expand Up @@ -54,7 +54,6 @@ def create_intent(money, payment_method, options = {})
request_three_d_secure(post, options)
add_level_three(post, options)
add_card_brand(post, options)
post[:expand] = ['charges.data.balance_transaction']

CREATE_INTENT_ATTRIBUTES.each do |attribute|
add_whitelisted_attribute(post, options, attribute)
Expand All @@ -65,7 +64,7 @@ def create_intent(money, payment_method, options = {})
end

def show_intent(intent_id, options)
commit(:get, "payment_intents/#{intent_id}", nil, options)
commit(:get, "payment_intents/#{intent_id}?expand[]=latest_charge.balance_transaction", nil, options)
end

def create_test_customer
Expand Down Expand Up @@ -183,7 +182,6 @@ def create_setup_intent(payment_method, options = {})
post[:on_behalf_of] = options[:on_behalf_of] if options[:on_behalf_of]
post[:usage] = options[:usage] if %w(on_session off_session).include?(options[:usage])
post[:description] = options[:description] if options[:description]
post[:expand] = ['latest_attempt']

commit(:post, 'setup_intents', post, options)
end
Expand Down Expand Up @@ -228,11 +226,11 @@ def void(intent_id, options = {})

def refund(money, intent_id, options = {})
if intent_id.include?('pi_')
intent = api_request(:get, "payment_intents/#{intent_id}", nil, options)
intent = api_request(:get, "payment_intents/#{intent_id}?expand[]=latest_charge", nil, options)

return Response.new(false, intent['error']['message'], intent) if intent['error']

charge_id = intent.try(:[], 'charges').try(:[], 'data').try(:[], 0).try(:[], 'id')
charge_id = intent.try(:[], 'latest_charge').try(:[], 'id')

if charge_id.nil?
error_message = "No associated charge for #{intent['id']}"
Expand Down Expand Up @@ -317,6 +315,30 @@ def supports_network_tokenization?

private

def card_from_response(response)
extract_payment_intent_details(response) ||
response.dig('latest_attempt', 'payment_method_details', 'card', 'checks') || super
end

def extract_payment_intent_details(response)
return nil if response['latest_charge'].nil? || response['latest_charge']&.is_a?(String)

response.dig('latest_charge', 'payment_method_details', 'card', 'checks')
end

def add_expand_parameters(method, url, post, options)
post[:expand] ||= []
post[:expand].concat(Array.wrap(options[:expand]).map(&:to_sym)).uniq!

return if method == :get

if url.include?('payment_intents')
post[:expand].concat(['latest_charge', 'latest_charge.balance_transaction'])
elsif url.include?('setup_intents')
post[:expand] << 'latest_attempt'
end
end

def error_id(response, url)
if url.end_with?('payment_intents')
response.dig('error', 'payment_intent', 'id') || super
Expand Down
Loading

0 comments on commit 320b2ec

Please sign in to comment.