Passing only one product to Stripe checkout from Django

Viewed 308

I've integrated Django with Stripe and seems that works ok. However i have a problem by showing all products in stripe checkout page. I need to tell stripe which products we're going to buy. If i have 2 products in cart i see only one product in stripe checkout.

views.py

def payments_stripe(request):
    YOUR_DOMAIN = "http://127.0.0.1:8000/orders"
    print(request.body)
    body = json.loads(request.body)
    cart = Cart.objects.get(cart_id=_cart_id(request))
    cart_items = cartItem.objects.filter(cart=cart, is_active=True)
    print(cart_items)
    order = Order.objects.get(cart=cart, is_ordered=False, order_number=body['orderID'])
    print(order)

    for cart_item in cart_items:
        quantity = cart_item.quantity
        print(quantity)
        name = cart_item.product.name
        print(name)
        
    try:
        checkout_session = stripe.checkout.Session.create(
            customer_email=order.email,
            billing_address_collection='auto',
            payment_method_types=['card'],
            line_items=[
                {
                    'price_data': {
                        'currency': 'eur',
                        'unit_amount': int(order.order_total * 100),
                        'product_data': {
                            'name': name,
                            'images': ['https://i.imgur.com/EHyR2nP.png'],
                        },
                    },
                    'quantity': quantity,
                },
            ],
            metadata={
                'order_number': body['orderID'], 
                'payment_method': body['payment_method'], 
                'cart_id': cart,
            },
            mode='payment',
            success_url=YOUR_DOMAIN + '/success/',
            cancel_url=YOUR_DOMAIN + '/cancel/',
        )

        return JsonResponse({'id': checkout_session.id})
    except Exception as e:
        return JsonResponse(error=str(e)), 403

I iterate over cart items and Iteration gives: 1 Golden Arrow 1 Silver Heart

However in Stripe dashboard i see only one product 1 Golden Arrow.

I suppose i need to create a new list. Is that right? How can i do that in my case?

Thank you

1 Answers

You need to actually add each item to the line_items array, so you want to do something more like this:

line_items = []

for cart_item in cart_items:
    # I presume you have something like cart_item.amount, right?
    unit_amount = int(cart_item.amount * 100)

    line_items.add({
        'price_data': {
            'currency': 'eur',
            'unit_amount': unit_amount,
            'product_data': {
                'name': cart_item.product.name,
                'images': ['https://i.imgur.com/EHyR2nP.png'],
            },
        },
        'quantity': cart_item.quantity,
    })
    
try:
    checkout_session = stripe.checkout.Session.create(
        customer_email=order.email,
        billing_address_collection='auto',
        payment_method_types=['card'],
        line_items=line_items,
        metadata={
            'order_number': body['orderID'], 
            'payment_method': body['payment_method'], 
            'cart_id': cart,
        },
        mode='payment',
        success_url=YOUR_DOMAIN + '/success/',
        cancel_url=YOUR_DOMAIN + '/cancel/',
    )
Related