Need to create checkout session GraphQL + Django

Need to create checkout session GraphQL + Django


0

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!

3

  • In this particular case, using GraphQL likely won't work for you. GraphQL is a tool that's useful for updating/mutating objects in your database. In this case, you simply need an API endpoint that creates a Checkout Session as shown in the example in your post. There would be no queries or mutations to be done so the true purpose/need for GraphQL isn't there. My personal recommendation here would be to maintain one graphql endpoint for managing the state of your app-specific objects/resources, and a separate REST endpoint for creating the Checkout Session.

    – ttmarek

    Oct 14, 2020 at 21:44

  • Thank you so much for that explanation! I did try creating a separate REST endpoint but it kept saying "get" not allowed. I will try it again now. Quick question, can REST endpoint be in the same Django project where I created my GraphQL endpoint?

    – BigSteppaJC

    Oct 14, 2020 at 21:48


  • 1

    Yes it can. You'll need to setup the routing so that the URLs used for both endpoints are different.

    – Mark Chackerian

    Oct 15, 2020 at 3:18

2 Answers
2


0

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.


0

Here is something that might help you – https://www.getlazy.ai/templates/stripe-checkout-session-api-server-template-0b4c5889-4952-4a0a-9390-d766fb96dcf6.
This is an AI app which create an API server that includes an endpoint for creating Stripe checkout session for processing payments, another endpoint for retrieving the status of a session, allows all CORS, includes logs with info level, and logs sent requests.
I’ve done it by using Lazy, it’s an AI tool that helps in such situations.



Leave a Reply

Your email address will not be published. Required fields are marked *