Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions testsuite/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@
class Stripe:
"""API for Stripe"""

def __init__(self, api_key):
def __init__(self, api_key, provider_account_id):
# Due to fact that we can set up only one Stripe per 3scale and we use same api_key everytime this
# is not disruptive even if it looks like it is.
stripe.api_key = api_key
self.provider_account_id = provider_account_id

@staticmethod
@backoff.on_predicate(backoff.fibo, lambda x: x == [], max_tries=10, jitter=None)
def read_charge(customer):
"""Retrieves the details of the charge"""
return stripe.Charge.search(query=f"customer:'{customer['id']}'").get("data")

@staticmethod
@backoff.on_exception(backoff.expo, IndexError, max_tries=4, jitter=None)
def read_customer_by_account(account):
def read_customer_by_account(self, account):
"""
Read Stripe customer.
Different 3scale deployments can have customers with the same id, which is reflected to the
`3scale_account_reference` Stripe Customer variable. This method reads just the last one.
"""
return stripe.Customer.search(
query=f"metadata['3scale_account_reference']:'3scale-2-{str(account.entity_id)}'"
query=f"metadata['3scale_account_reference']:'3scale-{self.provider_account_id}-{account.entity_id}'"
).get("data")[0]

def assert_payment(self, invoice, account):
Expand All @@ -51,7 +51,7 @@ def assert_payment(self, invoice, account):
class Braintree:
"""API for braintree"""

def __init__(self, merchant_id, public_key, private_key):
def __init__(self, merchant_id, public_key, private_key, provider_account_id):
self.gateway = braintree.BraintreeGateway(
braintree.Configuration(
environment=braintree.Environment.Sandbox,
Expand All @@ -60,6 +60,7 @@ def __init__(self, merchant_id, public_key, private_key):
private_key=private_key,
)
)
self.provider_account_id = provider_account_id

@backoff.on_exception(backoff.fibo, (ServiceUnavailableError, RequestTimeoutError), max_tries=8, jitter=None)
def get_customer_transactions(self, account):
Expand All @@ -76,10 +77,9 @@ def merchant_currency(self):
merchant_accounts = list(self.gateway.merchant_account.all().merchant_accounts.items)
return [x for x in merchant_accounts if x.default is True][0].currency_iso_code

@staticmethod
def customer_id(account):
"""Returns Braintree customer id. It is in a form `3scale-2-{account_id}-1`"""
return f"3scale-2-{account.entity_id}-1"
def customer_id(self, account):
"""Returns Braintree customer id. It is in a form `3scale-{provider_id}-{account_id}-1`"""
return f"3scale-{self.provider_account_id}-{account.entity_id}-1"

@staticmethod
def _assert_transaction(invoice, transaction):
Expand Down
5 changes: 3 additions & 2 deletions testsuite/tests/ui/billing/braintree/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def require_braintree_patch(openshift):


@pytest.fixture(scope="session")
def braintree(testconfig):
def braintree(testconfig, threescale):
"""Braintree API"""
braintree_credentials = testconfig["braintree"]
merchant_id = braintree_credentials["merchant_id"]
public_key = braintree_credentials["public_key"]
private_key = braintree_credentials["private_key"]
return Braintree(merchant_id, public_key, private_key)
provider_account_id = threescale.provider_accounts.fetch().entity_id
return Braintree(merchant_id, public_key, private_key, provider_account_id)


@pytest.fixture(scope="module", autouse=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def card_setup(custom_card):

def normalize_url(url):
"""Invoice url need to be changed from internal to external form"""
for rep in (("3scale-admin", "3scale"), ("/api/", "/admin/account/")):
for rep in (("-admin.", "."), ("/api/", "/admin/account/")):
url = url.replace(*rep)
return url

Expand All @@ -28,11 +28,11 @@ def test_no_sca_ui_invoice(braintree, ui_invoice, account):
assert invoice_view.state_field.text == "State Paid"


def test_mail_completed_payment(invoice, mailhog_client):
def test_mail_completed_payment(provider_account, invoice, mailhog_client):
"""Tests mail notification about successful payment"""
invoice.charge()
mailhog_client.assert_message_received(
subject="Provider Name API - Payment completed",
subject=f"{provider_account['org_name']} API - Payment completed",
content=f"successfully completed your monthly payment for our service of USD {invoice.entity['cost']}0.\r\n\r\n"
f"Your invoice is available online at:\r\n\r\n{normalize_url(invoice.url)}",
)
5 changes: 3 additions & 2 deletions testsuite/tests/ui/billing/stripe/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ def gateway_setup(custom_admin_login, navigator, testconfig):


@pytest.fixture(scope="session")
def stripe(testconfig):
def stripe(testconfig, threescale):
"""Stripe API"""
return Stripe(testconfig["stripe"]["api_key"])
provider_account_id = threescale.provider_accounts.fetch().entity_id
return Stripe(testconfig["stripe"]["api_key"], provider_account_id)


@pytest.fixture(scope="module")
Expand Down
Loading