From 103366ea97fd25f7bd768d6ead393e5a75dbf44c Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Fri, 20 Nov 2020 14:36:46 -0800 Subject: [PATCH] add request limits to credit card cloner --- lib/stripe/credit_card_cloner.rb | 36 +++++++++++++++++--------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/stripe/credit_card_cloner.rb b/lib/stripe/credit_card_cloner.rb index 7d90960bf5..d21bc2986c 100644 --- a/lib/stripe/credit_card_cloner.rb +++ b/lib/stripe/credit_card_cloner.rb @@ -18,7 +18,7 @@ module Stripe class CreditCardCloner def find_or_clone(card, connected_account_id) - if cloned_card = find_cloned_card(card, connected_account_id) + if card.user && cloned_card = find_cloned_card(card, connected_account_id) cloned_card else clone(card, connected_account_id) @@ -32,7 +32,7 @@ module Stripe customer_id, _payment_method_id = find_cloned_card(card, stripe_account) next unless customer_id - customer = Stripe::Customer.retrieve(customer_id, { stripe_account: stripe_account }) + customer = Stripe::Customer.retrieve(customer_id, stripe_account: stripe_account) customer&.delete unless customer.deleted? end end @@ -57,18 +57,16 @@ module Stripe end def find_cloned_card(card, connected_account_id) - matches = [] - return matches unless fingerprint = fingerprint_for_card(card) + return nil unless fingerprint = fingerprint_for_card(card) find_customers(card.user.email, connected_account_id).each do |customer| find_payment_methods(customer.id, connected_account_id).each do |payment_method| if payment_method_is_clone?(payment_method, fingerprint) - matches << [customer.id, payment_method.id] + return [customer.id, payment_method.id] end end end - - matches.first + nil end def payment_method_is_clone?(payment_method, fingerprint) @@ -80,35 +78,39 @@ module Stripe end def find_customers(email, connected_account_id) - starting_after = nil - customers = [] + start_after, customers = nil, [] - loop do - response = Stripe::Customer.list({ email: email, starting_after: starting_after }, + (1..request_limit = 100).each do |request_number| + response = Stripe::Customer.list({ email: email, starting_after: start_after, limit: 100 }, stripe_account: connected_account_id) customers += response.data break unless response.has_more - starting_after = response.data.last.id + start_after = response.data.last.id + notify_limit(request_number, "customers") if request_limit == request_number end customers end def find_payment_methods(customer_id, connected_account_id) - starting_after = nil - payment_methods = [] + start_after, payment_methods = nil, [] - loop do - options = { customer: customer_id, type: 'card', starting_after: starting_after } + (1..request_limit = 10).each do |request_number| + options = { customer: customer_id, type: 'card', starting_after: start_after, limit: 100 } response = Stripe::PaymentMethod.list(options, stripe_account: connected_account_id) payment_methods += response.data break unless response.has_more - starting_after = response.data.last.id + start_after = response.data.last.id + notify_limit(request_number, "payment methods") if request_limit == request_number end payment_methods end + def notify_limit(request_number, retrieving) + Bugsnag.notify("Reached limit of #{request_number} requests retrieving #{retrieving}.") + end + def clone_payment_method(credit_card, connected_account_id) platform_acct_payment_method_id = credit_card.gateway_payment_profile_id customer_id = credit_card.gateway_customer_profile_id