-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from GooeyAI/autorecharge-after-oneoff
Store Stripe billing info + auto recharge without subscription + cancel button for subscriptions
- Loading branch information
Showing
14 changed files
with
573 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import stripe | ||
from loguru import logger | ||
|
||
from app_users.models import PaymentProvider, TransactionReason | ||
from celeryapp.celeryconfig import app | ||
from payments.models import Subscription | ||
from payments.plans import PricingPlan | ||
from payments.webhooks import set_user_subscription | ||
|
||
|
||
@app.task | ||
def save_stripe_default_payment_method( | ||
*, | ||
payment_intent_id: str, | ||
uid: str, | ||
amount: int, | ||
charged_amount: int, | ||
reason: TransactionReason, | ||
): | ||
pi = stripe.PaymentIntent.retrieve(payment_intent_id, expand=["payment_method"]) | ||
pm = pi.payment_method | ||
if not (pm and pm.customer): | ||
logger.error( | ||
f"Failed to retrieve payment method for payment intent {payment_intent_id}" | ||
) | ||
return | ||
|
||
# update customer's defualt payment method | ||
# note: if a customer has an active subscription, the payment method attached there will be preferred | ||
# see `stripe_get_default_payment_method` in payments/models.py module | ||
logger.info( | ||
f"Updating default payment method for customer {pm.customer} to {pm.id}" | ||
) | ||
stripe.Customer.modify( | ||
pm.customer, | ||
invoice_settings=dict(default_payment_method=pm), | ||
) | ||
|
||
# if user doesn't already have a active billing/autorecharge info, so we don't need to do anything | ||
# set user's subscription to the free plan | ||
if ( | ||
reason == TransactionReason.ADDON | ||
and not Subscription.objects.filter( | ||
user__uid=uid, payment_provider__isnull=False | ||
).exists() | ||
): | ||
set_user_subscription( | ||
uid=uid, | ||
plan=PricingPlan.STARTER, | ||
provider=PaymentProvider.STRIPE, | ||
external_id=None, | ||
amount=amount, | ||
charged_amount=charged_amount, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.