Adding payment_id column into the account_membership table, and removing the previous subscription from stripe when a user changes his membership

pull/77/head
Matheus Lima 2019-03-15 11:58:00 -03:00
parent d88d58525c
commit ff16ce4477
8 changed files with 29 additions and 13 deletions

View File

@ -64,7 +64,8 @@ def _add_account(context, db):
type='Monthly Membership',
start_date=date.today(),
payment_method='Stripe',
payment_account_id='foo'
payment_account_id='foo',
payment_id='bar'
),
agreements=[
AccountAgreement(type=PRIVACY_POLICY, accept_date=date.today())

View File

@ -8,6 +8,7 @@ CREATE TABLE account.account_membership (
membership_ts_range tsrange NOT NULL,
payment_method payment_method_enum NOT NULL,
payment_account_id text NOT NULL,
payment_id text NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP,
EXCLUDE USING gist (account_id WITH =, membership_ts_range with &&),

View File

@ -207,12 +207,19 @@ class AccountEndpoint(SeleneEndpoint):
payment_token = self.request_data['support']['paymentToken']
email = self.request_data['login']['userEnteredEmail']
plan = self._get_plan(membership_type).stripe_plan
payment_account_id, start = self._create_stripe_subscription(None, payment_token, email, plan)
payment_account_id, start, subscription_id = self._create_stripe_subscription(
None,
payment_token,
email,
plan
)
membership = AccountMembership(
type=membership_type,
start_date=date.today(),
payment_method=self.request_data['support']['paymentMethod'],
payment_account_id=payment_account_id
payment_account_id=payment_account_id,
payment_id=subscription_id
)
account = Account(
email_address=email_address,
@ -236,10 +243,9 @@ class AccountEndpoint(SeleneEndpoint):
customer_id = customer.id
subscription = stripe.Subscription.create(customer=customer_id, items=[{'plan': plan}])
# TODO: store subscription.id
start = subscription.current_period_start
start = date.fromtimestamp(start)
return customer_id, start
return customer_id, start, subscription.id
def _get_plan(self, plan):
with get_db_connection(self.config['DB_CONNECTION_POOL']) as db:
@ -251,14 +257,15 @@ class AccountEndpoint(SeleneEndpoint):
active_membership = membership_repository.get_active_membership_by_account_id(self.account.id)
if active_membership:
active_membership.end_date = datetime.utcnow()
# TODO: use the subscription id to delete the membership on stripe
active_stripe_subscription = stripe.Subscription.retrieve(active_membership.payment_id)
active_stripe_subscription.delete()
membership_repository.finish_membership(active_membership)
add_membership = UpdateMembership(self.request_data.get('support'))
add_membership.validate()
support = self.request_data['support']
membership_type = support['membership']
membership = self._get_plan(membership_type)
stripe_id, start_date = self._create_stripe_subscription(
stripe_id, start_date, subscription_stripe_id = self._create_stripe_subscription(
active_membership.payment_account_id,
None,
self.account.email_address,
@ -271,7 +278,7 @@ class AccountEndpoint(SeleneEndpoint):
membership_type = support['membership']
token = support['paymentToken']
membership = self._get_plan(membership_type)
stripe_id, start_date = self._create_stripe_subscription(
stripe_id, start_date, subscription_stripe_id = self._create_stripe_subscription(
None,
token,
self.account.email_address,
@ -282,6 +289,7 @@ class AccountEndpoint(SeleneEndpoint):
start_date=start_date,
payment_method=STRIPE_PAYMENT,
payment_account_id=stripe_id,
payment_id=subscription_stripe_id,
type=membership_type
)
AccountRepository(db).add_membership(self.account.id, new_membership)

View File

@ -18,6 +18,7 @@ class AccountMembership(object):
start_date: date
payment_method: str
payment_account_id: str
payment_id: str
id: str = None
end_date: date = None

View File

@ -83,7 +83,8 @@ class AccountRepository(object):
account_id=acct_id,
membership_type=membership.type,
payment_method=membership.payment_method,
payment_account_id=membership.payment_account_id
payment_account_id=membership.payment_account_id,
payment_id=membership.payment_id
)
)
self.cursor.insert(request)

View File

@ -4,7 +4,8 @@ INSERT INTO
membership_id,
membership_ts_range,
payment_method,
payment_account_id
payment_account_id,
payment_id
)
VALUES
(
@ -19,5 +20,6 @@ VALUES
),
'[now,]',
%(payment_method)s,
%(payment_account_id)s
%(payment_account_id)s,
%(payment_id)s
)

View File

@ -29,7 +29,8 @@ WITH
'type', m.type,
'start_date', lower(am.membership_ts_range)::DATE,
'payment_method', am.payment_method,
'payment_account_id', am.payment_account_id
'payment_account_id', am.payment_account_id,
'payment_id', am.payment_id
)
FROM
account.account_membership am

View File

@ -3,7 +3,8 @@ SELECT
mem.type,
LOWER(acc_mem.membership_ts_range) start_date,
acc_mem.payment_method,
payment_account_id
payment_account_id,
payment_id
FROM
account.account_membership acc_mem
INNER JOIN