55import stripe
66from braintree .exceptions .request_timeout_error import RequestTimeoutError
77from braintree .exceptions .service_unavailable_error import ServiceUnavailableError
8-
98from threescale_api .resources import InvoiceState
109
1110
1211class Stripe :
1312 """API for Stripe"""
1413
15- def __init__ (self , api_key ):
14+ def __init__ (self , api_key , provider_account_id ):
1615 # Due to fact that we can set up only one Stripe per 3scale and we use same api_key everytime this
1716 # is not disruptive even if it looks like it is.
1817 stripe .api_key = api_key
18+ self .provider_account_id = provider_account_id
1919
2020 @staticmethod
2121 @backoff .on_predicate (backoff .fibo , lambda x : x == [], max_tries = 10 , jitter = None )
2222 def read_charge (customer ):
2323 """Retrieves the details of the charge"""
24- return stripe .Charge .search (query = f"customer:'{ customer ['id' ]} '" ).get ( " data" )
24+ return stripe .Charge .search (query = f"customer:'{ customer ['id' ]} '" ).data
2525
26- @staticmethod
2726 @backoff .on_exception (backoff .expo , IndexError , max_tries = 4 , jitter = None )
28- def read_customer_by_account (account ):
27+ def read_customer_by_account (self , account ):
2928 """
3029 Read Stripe customer.
3130 Different 3scale deployments can have customers with the same id, which is reflected to the
3231 `3scale_account_reference` Stripe Customer variable. This method reads just the last one.
3332 """
3433 return stripe .Customer .search (
35- query = f"metadata['3scale_account_reference']:'3scale-2 -{ str (account .entity_id )} '"
36- ).get ( " data" ) [0 ]
34+ query = f"metadata['3scale_account_reference']:'3scale-{ self . provider_account_id } -{ str (account .entity_id )} '"
35+ ).data [0 ]
3736
3837 def assert_payment (self , invoice , account ):
3938 """Compare 3scale and Stripe invoices"""
@@ -51,7 +50,7 @@ def assert_payment(self, invoice, account):
5150class Braintree :
5251 """API for braintree"""
5352
54- def __init__ (self , merchant_id , public_key , private_key ):
53+ def __init__ (self , merchant_id , public_key , private_key , provider_account_id ):
5554 self .gateway = braintree .BraintreeGateway (
5655 braintree .Configuration (
5756 environment = braintree .Environment .Sandbox ,
@@ -60,6 +59,7 @@ def __init__(self, merchant_id, public_key, private_key):
6059 private_key = private_key ,
6160 )
6261 )
62+ self .provider_account_id = provider_account_id
6363
6464 @backoff .on_exception (backoff .fibo , (ServiceUnavailableError , RequestTimeoutError ), max_tries = 8 , jitter = None )
6565 def get_customer_transactions (self , account ):
@@ -76,10 +76,9 @@ def merchant_currency(self):
7676 merchant_accounts = list (self .gateway .merchant_account .all ().merchant_accounts .items )
7777 return [x for x in merchant_accounts if x .default is True ][0 ].currency_iso_code
7878
79- @staticmethod
80- def customer_id (account ):
81- """Returns Braintree customer id. It is in a form `3scale-2-{account_id}-1`"""
82- return f"3scale-2-{ account .entity_id } -1"
79+ def customer_id (self , account ):
80+ """Returns Braintree customer id. It is in a form `3scale-{provider_id}-{account_id}-1`"""
81+ return f"3scale-{ self .provider_account_id } -{ account .entity_id } -1"
8382
8483 @staticmethod
8584 def _assert_transaction (invoice , transaction ):
0 commit comments