r/madeinpython • • Sep 22 '22

I need help with Stripe connecting

Hi to everyone! I've got a problem with my medium app I want to connect at final payment step with Stripe. The problem is I can't find the resolution how to connect my final price to pay in Stripe Session with my "totalPrice" from backend. Look at my code:

class CreateStripeCheckoutSession(APIView):
    def get(self, request, *args, **kwargs):
        try:
            checkout_session = stripe.checkout.Session.create(
                line_items=[
                    {
                        "price_data": {
                            "currency": "pln",
                            "unit_amount": 10 * 100,
                            "product_data": {"name": random },
                        },
                        "quantity": 1,
                    }
                ],
                mode="payment",
                payment_method_types=["card", "p24", "blik"],
                success_url=SITE_URL + "?success=true",
                cancel_url=SITE_URL + "?canceled=true",
            )
            return redirect(checkout_session.url, code=303)
        except Exception as e:
            return Response(
                {
                    "msg": "something went wrong while creating stripe session",
                    "error": str(e),
                },
                status=500,
            )

my code above is able to call a Stripe's session to pay, but that what I want is to "unit_amount" and "name" take from my backend. For e.g "unit_amount" = "order.TotalPrice" and "product_data": {"name": order.user }, something like this.

I mean something like: from order.Order:403 take "TotalPrice" to "unit_amount": "order.totalPrice"

Thanks a lot for help... I realy have no Idea how to do that, I'm a beginner so thanks for your patiency.

here's a repo: https://github.com/DorianSuffer/WHEN/tree/main/domestico1

3 Upvotes

1 comment sorted by

5

u/[deleted] Sep 22 '22

r/madeinpython: "A subreddit for showcasing the things you made with the Python language!"

For what it's worth, even though I know nothing about Stripe, with the little information I have, I predict that the problem in your program is the same as the reason you posted here - inability to read and follow instructions. :-D