#adomarc-django-checkout
1 messages · Page 1 of 1 (latest)
mindful-django-checkout
adomarc-django-checkout
@drifting oasis there isn't really anything in our API that is "framework specific". Embedded Checkout works fine on Django or Flask as we don't have any knowledge of your framework
So I think the first step is to ignore the framework part and figure out what's not working exactly
This is my code. Adapted from the flask code provided in Stripe's guide. I'm struggling to get everything to work together.
#views.py
@login_required
@email_verified_required
def create_subscription(request, tier_name):
if request.method == 'GET':
try:
tier = SubscriptionTier.objects.get(name=tier_name)
checkout_session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[{
'price': tier.stripe_price_id,
'quantity': 1,
}],
mode='subscription',
ui_mode='embedded',
return_url=request.build_absolute_uri(reverse('session_status')) + '?session_id={CHECKOUT_SESSION_ID}',
# Ensure the session is for an embedded form
client_reference_id=request.user.id, # Optional: for tracking
)
# Update tier limits
# Remove the user from all subscription groups
for group_name in ['Free', 'Basic', 'Regular', 'Premium']:
group = Group.objects.get(name=group_name)
group.user_set.remove(request.user)
# Add the user to the new group based on their subscription
new_group = Group.objects.get(name=tier_name.capitalize())
new_group.user_set.add(request.user)
# Instead of redirecting, pass session ID to your template
context={
"sessionId": checkout_session.id,
"clientSecret": checkout_session.client_secret,
'STRIPE_PUBLIC_KEY': settings.STRIPE_PUBLIC_KEY}
return render(request, 'create_subscription.html', context)
except SubscriptionTier.DoesNotExist:
pass # Handle error
Let's pause
sorry you just dumped a ton of code on me. You're a developer, you can add detailed logging to your code to figure out which part is failing first
I'm a mechanical engineer by profession not a software engineer. So I apologize if this seems really simple
All good and totally reasonable. I'm not saying it's simple yet, just that it's important to debug your own code first. I won't be able to guess what's up without actionable information unfortunately
So the first step is for you to add clear logs to your code. There are multiple separate pieces to your code from creating a Checkout Session server-side to rendering it client-side. Any of those steps could be failing and would return a clear error as to why, so that's what you need to do first. Narrow down the part of the code failing so tat you get a clear error and I can help you fix it
Well I am getting several error messages in the console. I'm not sure if these are the root cause of the Checkout Session embedded form not displaying but may be a good start.
Request to access cookie or storage on “https://js.stripe.com/v3/” was blocked because it came from a tracker and content blocking is enabled.
You may test your Stripe.js integration over HTTP. However, live Stripe.js integrations must use HTTPS. v3:1:432439
Loading failed for the <script> with source “http://127.0.0.1:8000/subscribe/dashboard.js”. basic:298:221
Request to access cookie or storage on “<URL>” was blocked because it came from a tracker and content blocking is enabled. 2
Partitioned cookie or storage access was provided to “https://m.stripe.network/inner.html#url=http%3A%2F%2F127.0.0.1%3A8000%2Fsubscribe%2Fbasic&title=Mindful Recruit&referrer=&muid=695b6c10-6f66-4015-96e2-a6ed91133cf81eebef&sid=NA&version=6&preview=false” because it is loaded in the third-party context and dynamic state partitioning is enabled.
A resource is blocked by OpaqueResponseBlocking, please check browser console for details. 3 csp-report
Request to access cookie or storage on “<URL>” was blocked because it came from a tracker and content blocking is enabled. 3
Request to access cookie or storage on “https://m.stripe.com/6” was blocked because it came from a tracker and content blocking is enabled.
Request to access cookie or storage on “https://m.stripe.com/6” was blocked because it came from a tracker and content blocking is enabled. 2
Request to access cookie or storage on “https://m.stripe.com/6” was blocked because it came from a tracker and content blocking is enabled.
A resource is blocked by OpaqueResponseBlocking, please check browser console for details. 2 csp-report
nothing in there seems relevant right now and you can ignore
okay thanks for confirming.
I am getting a message "Not Found: /create_subscription" in the terminal. This seems to indicate that something is wrong with my pointing to the url.
My urls.py has a line of code for the url:
path('subscribe/slug:tier_name', views.create_subscription, name='create_subscription'),
I am passing the tier_name to the url here.
The javascript in my html file seems to be pointing to the /create_subscription url. Not 100% sure how they interact but possibly it's not getting the dynamic tier_name.
const response = await fetch("/create_subscription", {
method: "POST",
});
Not sure if this is the issue
Okay so your JS code is calling your server's endpoint. That part is fine. The problem is that your server is not working or responding on that URL, that's what you need to debug
Okay I update the javascript to:
const response = await fetch("/subscribe/"+'{{tier_name}}', {
method: "POST",
});
my tier name is called "basic" in this case. so the url is "/subscribe/basic"
I now get this error in my terminal:
Forbidden (CSRF token missing.): /subscribe/basic
Forbidden (CSRF token missing.): /subscribe/basic
would the missing CSRF token cause the Stripe Checkout embedded form from displaying?
yes and no
Checkout has zero info about any of this so not really. The problem is that for Checkout to show you have to create the Checkout Session (in Python) and then send the client secret back.
for that to work, the request to your server has to work. And it's not working at all right now because of the CSRF. That is entirely unrelated to Checkout though, it's more something specific to Django that you need to learn and debug first
I think I solved the CSRF issue but the issue with the embedded form not displaying still persists. It would be great if Stripe added a python django option to their documentation instead of just a python flask option.
Any chance of that happening?
Hi, stepping in here. Can you attach the document you're rerreing to here while I catch up?
Are you asking if we can add python django example here: https://stripe.com/docs/checkout/embedded/quickstart ?
yes. I think that would be extremely helpful!
I think it's more on developers to be learn the specifics of Django but I'll definitely share this sentiment with the team.
It doesn't seem like I will get any specific help here that will help me get Stripe going for my business so that would be great
may have to look for alternatives to stripe
but thanks anyways
You can also look at outside resources like https://testdriven.io/blog/django-stripe-tutorial/ is it's helpful.
thanks but I have yet to find anything online for the embedded form Checkout Session django implementation. doing a search on google shows how scant this info is