Need to create checkout session GraphQL + Django

492 views Asked by At

I need to create a session for Stripe Checkout. According to Stripe Checkout Docs:

Add an endpoint on your server that creates a Checkout Session. A Checkout Session controls what your customer sees in the Stripe-hosted payment page such as line items, the order amount and currency, and acceptable payment methods. Return the Checkout Session's ID in the response to reference the Session on the client.

I am struggling with creating this service in Graphene (GraphQL wrapper for Django implementation).

This is what the docs show as an example (Flask):

[email protected]('/create-session', methods=['POST'])
22def create_checkout_session():
23    try:
24        checkout_session = stripe.checkout.Session.create(
25            payment_method_types=['card'],
26            line_items=[
27                {
28                    'price_data': {
29                        'currency': 'usd',
30                        'unit_amount': 2000,
31                        'product_data': {
32                            'name': 'Stubborn Attachments',
33                            'images': ['https://i.imgur.com/EHyR2nP.png'],
34                        },
35                    },
36                    'quantity': 1,
37                },
38            ],
39            mode='payment',
40            success_url=YOUR_DOMAIN + '?success=true',
41            cancel_url=YOUR_DOMAIN + '?canceled=true',
42        )
43        return jsonify({'id': checkout_session.id})
44    except Exception as e:
45        return jsonify(error=str(e)), 403

I know how to get the frontend(react) working and know how to request this create session id every time a user wants to purchase something but I am stuck on the GraphQl server implementation for the create session service. Essentially I want to convert the above code into GraphQL equivalent. I would like to know if I need to create a model for this service or not? Anything to get me started would be helpful!

1

There are 1 answers

0
Umair Anwar On

Session require cookies. A better approach is to use Auth Bearer token with requests to mimic session behavior in your request via GraphL POST queries and mutations.

If you want to use cookie based session, then create session at graphql.