#hassan_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1377021507863314452
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- hassan_code, 6 hours ago, 28 messages
def post(self, request):
try:
customer_id = request.data.get("customerId")
interval = request.data.get("interval", "year")
email = request.user.email # Extracted from token
if not customer_id:
return Response(
{"error": "Missing customer ID"}, status=status.HTTP_400_BAD_REQUEST
)
# 1. Retrieve the customer
customer = stripe.Customer.retrieve(customer_id)
default_pm = customer.get("invoice_settings", {}).get(
"default_payment_method"
)
# 2. If no default payment method, fetch saved ones and set the first as default
if not default_pm:
payment_methods = stripe.PaymentMethod.list(
customer=customer_id, type="card"
)
if not payment_methods["data"]:
return Response(
{"error": "No saved payment methods found"},
status=status.HTTP_400_BAD_REQUEST,
)
default_pm = payment_methods["data"][0]["id"]
# Set it as default on the customer
stripe.Customer.modify(
customer_id, invoice_settings={"default_payment_method": default_pm}
)
# 3. Get active price from your DB
price = StripePrice.objects.filter(interval=interval, active=True).first()
if not price:
return Response(
{"error": "No active price found"}, status=status.HTTP_404_NOT_FOUND
)
4. Calculate future billing cycle anchor
billing_cycle_anchor = datetime.now()
if interval == "month":
billing_cycle_anchor += timedelta(days=30)
elif interval == "year":
billing_cycle_anchor += timedelta(days=365)
billing_cycle_anchor_ts = int(billing_cycle_anchor.timestamp())
# 5. Create subscription with automatic tax calculation
subscription = stripe.Subscription.create(
customer=customer_id,
items=[{"price": price.stripe_price_id}],
default_payment_method=default_pm,
billing_cycle_anchor=billing_cycle_anchor_ts,
proration_behavior="none",
automatic_tax={"enabled": True},
)
# 6. Store subscription in DB
customer_details = CustomerDetails.objects.get(user__email=email)
subscription_item = subscription["items"]["data"][0]
current_period_end = subscription_item.get("current_period_end")
subscription_instance = Subscription.objects.create(
customer=customer_details,
stripe_customer_id=customer_id,
stripe_subscription_id=subscription.id,
status=subscription["status"],
subscription_type=interval,
subscription_expiry=(
datetime.utcfromtimestamp(current_period_end)
if current_period_end
else None
),
)
return Response(
{"subscriptionId": subscription.id}, status=status.HTTP_200_OK
)
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
Hi there ๐ I don't see automatic_tax in your Subscription creation request based on the code you shared.
https://docs.stripe.com/api/subscriptions/create#create_subscription-automatic_tax
I'd suggest starting here and ensuring you've setup the basics of Stripe Tax:
https://docs.stripe.com/tax/set-up
make sure you've registered to collect tax in relevant jurisdictions:
https://docs.stripe.com/tax/registering
make sure your Products and Prices are set up correctly:
https://docs.stripe.com/tax/products-prices-tax-codes-tax-behavior
then finally make sure you're using those objects correctly to combine Stripe Tax and Stripe Billing features:
https://docs.stripe.com/tax/subscriptions?estimate=before
5. Create subscription with automatic tax calculation
subscription = stripe.Subscription.create(
customer=customer_id,
items=[{"price": price.stripe_price_id}],
default_payment_method=default_pm,
billing_cycle_anchor=billing_cycle_anchor_ts,
proration_behavior="none",
automatic_tax={"enabled": True},
)
its there
product = stripe.Product.create(
name=name,
description=description,
tax_code="txcd_10103000", # General - Tangible Goods
)
# Step 2: Create monthly price
price_month = stripe.Price.create(
unit_amount=int(price_monthly * 100), # Amount in cents
currency="usd",
recurring={"interval": "month"},
product=product.id,
tax_behavior="exclusive", # Tax is included in the price
)
i create products and prices like this
Ah, yup, guess I was looking at a Sub creation that creates a non-Stripe subscription via Subscription.objects.create, my bad
is my setup correct?
I don't know, there's more to it than that. Like making sure you have an origin address set up in your Stripe dashboard, and making sure you're registered in the relevant jurisdictions. But if you aren't seeing tax calculated, it doesn't sound like it.
in terms of api, setup haha?
Are you providing addresses for your Customers?
the issue is resolved thank you for your help guys
Hi hi! I didn't do anything, but you're welcome. ๐ I'm taking over for my colleague, so let me know if anything else comes up. ๐