@@ -2,6 +2,32 @@ module Travis::API::V3
22 class Queries ::V2Subscription < Query
33 params :enabled , :threshold , :amount
44
5+ def update_payment_details ( user_id )
6+ recaptcha_redis_key = "recaptcha_attempts_v2_#{ params [ 'subscription.id' ] } "
7+ count = Travis . redis . get ( recaptcha_redis_key ) &.to_i
8+ count = count . nil? ? 0 : count
9+ if count > captcha_max_failed_attempts
10+ raise ClientError , 'Error verifying reCAPTCHA, you have exausted your attempts, please wait.'
11+ end
12+
13+ result = recaptcha_client . verify ( params [ 'captcha_token' ] )
14+ unless result
15+ if count == 0
16+ Travis . redis . setex ( recaptcha_redis_key , captcha_block_duration , count + 1 )
17+ else
18+ ttl = Travis . redis . ttl ( recaptcha_redis_key )
19+ Travis . redis . setex ( recaptcha_redis_key , ttl , count + 1 )
20+ end
21+ raise ClientError , 'Error verifying reCAPTCHA, please try again.'
22+ end
23+
24+ address_data = params . dup . tap { |h | h . delete ( 'subscription.id' ) }
25+ address_data = address_data . tap { |h | h . delete ( 'token' ) }
26+ client = BillingClient . new ( user_id )
27+ client . update_v2_address ( params [ 'subscription.id' ] , address_data ) unless address_data . empty?
28+ client . update_v2_creditcard ( params [ 'subscription.id' ] , params [ 'token' ] , params [ 'fingerprint' ] ) if params . key? ( 'token' )
29+ end
30+
531 def update_address ( user_id )
632 address_data = params . dup . tap { |h | h . delete ( 'subscription.id' ) }
733 client = BillingClient . new ( user_id )
@@ -60,5 +86,19 @@ def update_auto_refill(user_id, addon_id)
6086 client = BillingClient . new ( user_id )
6187 client . update_auto_refill ( addon_id , threshold , amount )
6288 end
89+
90+ private
91+
92+ def recaptcha_client
93+ @recaptcha_client ||= RecaptchaClient . new
94+ end
95+
96+ def captcha_block_duration
97+ Travis . config . antifraud . captcha_block_duration
98+ end
99+
100+ def captcha_max_failed_attempts
101+ Travis . config . antifraud . captcha_max_failed_attempts
102+ end
63103 end
64104end
0 commit comments