mirror of https://gitlab.com/litecord/litecord.git
user.billing: use db instead of app instance in create_payment
This commit is contained in:
parent
0ec615f3bd
commit
a50cf8a17c
|
|
@ -220,15 +220,18 @@ async def get_payment(payment_id: int, db=None):
|
||||||
return drow
|
return drow
|
||||||
|
|
||||||
|
|
||||||
async def create_payment(subscription_id, app):
|
async def create_payment(subscription_id, db=None):
|
||||||
"""Create a payment."""
|
"""Create a payment."""
|
||||||
sub = await get_subscription(subscription_id, app.db)
|
if not db:
|
||||||
|
db = app.db
|
||||||
|
|
||||||
|
sub = await get_subscription(subscription_id, db)
|
||||||
|
|
||||||
new_id = get_snowflake()
|
new_id = get_snowflake()
|
||||||
|
|
||||||
amount = AMOUNTS[sub['payment_gateway_plan_id']]
|
amount = AMOUNTS[sub['payment_gateway_plan_id']]
|
||||||
|
|
||||||
await app.db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO user_payments (
|
INSERT INTO user_payments (
|
||||||
id, source_id, subscription_id, user_id,
|
id, source_id, subscription_id, user_id,
|
||||||
|
|
|
||||||
|
|
@ -54,18 +54,24 @@ async def _process_user_payments(app, user_id: int):
|
||||||
subscription = await get_subscription(
|
subscription = await get_subscription(
|
||||||
sub_id, app.db)
|
sub_id, app.db)
|
||||||
|
|
||||||
|
# if the max payment is X days old, we create another.
|
||||||
|
# X is 30 for monthly subscriptions of nitro,
|
||||||
|
# X is 365 for yearly subscriptions of nitro
|
||||||
threshold = THRESHOLDS[subscription['payment_gateway_plan_id']]
|
threshold = THRESHOLDS[subscription['payment_gateway_plan_id']]
|
||||||
|
|
||||||
log.debug('delta {} delta days {} threshold {}',
|
log.debug('delta {} delta days {} threshold {}',
|
||||||
delta, delta.days, threshold)
|
delta, delta.days, threshold)
|
||||||
|
|
||||||
if delta.days > threshold:
|
if delta.days > threshold:
|
||||||
# insert new payment, for free !!!!!!
|
|
||||||
log.info('creating payment for sid={}',
|
log.info('creating payment for sid={}',
|
||||||
sub_id)
|
sub_id)
|
||||||
|
|
||||||
|
# create_payment does not call any Stripe
|
||||||
|
# or BrainTree APIs at all, since we'll just
|
||||||
|
# give it as free.
|
||||||
await create_payment(sub_id, app)
|
await create_payment(sub_id, app)
|
||||||
else:
|
else:
|
||||||
log.debug('not there yet for sid={}', sub_id)
|
log.debug('sid={}, missing {threshold - delta.days} days', sub_id)
|
||||||
|
|
||||||
|
|
||||||
async def payment_job(app):
|
async def payment_job(app):
|
||||||
|
|
@ -74,7 +80,7 @@ async def payment_job(app):
|
||||||
This function will check through users' payments
|
This function will check through users' payments
|
||||||
and add a new one once a month / year.
|
and add a new one once a month / year.
|
||||||
"""
|
"""
|
||||||
log.info('payment job start!')
|
log.debug('payment job start!')
|
||||||
|
|
||||||
user_ids = await app.db.fetch("""
|
user_ids = await app.db.fetch("""
|
||||||
SELECT DISTINCT user_id
|
SELECT DISTINCT user_id
|
||||||
|
|
@ -82,7 +88,6 @@ async def payment_job(app):
|
||||||
""")
|
""")
|
||||||
|
|
||||||
log.debug('working {} users', len(user_ids))
|
log.debug('working {} users', len(user_ids))
|
||||||
print(user_ids)
|
|
||||||
|
|
||||||
# go through each user's payments
|
# go through each user's payments
|
||||||
for row in user_ids:
|
for row in user_ids:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue