How to access checkout session metadata in Stripe webhook for payment methods in subscription mode

I'm integrating Stripe Checkout in a Django application and handling webhooks to update user information based on payment events. However, I'm encountering issues accessing metadata associated with a Checkout Session when dealing with payment_method objects.

Context:

I have the following setup for Stripe Checkout:

  • StripeCheckoutMonthlyView and StripeCheckoutYearlyView: Both create a Checkout Session with metadata (e.g., user_id, plan_type).
  • Webhook Handler (stripe_webhook): Processes different event types from Stripe.

Problem:

In the payment_method.attached event, I need to access metadata that was included in the Checkout Session. However, the payment_method object does not include metadata and does not directly reference the Checkout Session.

Here’s how my webhook handler looks:

@csrf_exempt
def stripe_webhook(request):
    payload = request.body
    event = None

    print('Stripe Webhook Received!')

    try:
        event = stripe.Event.construct_from(
            json.loads(payload), stripe.api_key
        )
    except ValueError as e:
        # Invalid payload
        return HttpResponse(status=400)

    if event.type == 'payment_intent.succeeded':
        # Handle payment_intent.succeeded event
        pass
    elif event.type == 'payment_method.attached':
        payment_method = event.data.object
        # Issue: Payment method does not include metadata or session_id
        pass
    elif event.type == 'checkout.session.completed':
        session = event.data.object
        # Retrieve session metadata here
        pass
    else:
        print('Unhandled event type {}'.format(event.type))

    return HttpResponse(status=200)

What I Need:

I need to update user information based on metadata that was included in the Checkout Session. Specifically:

  1. Access metadata in payment_method.attached event.
  2. Retrieve metadata from the Checkout Session when handling payment_method events.

Solution Attempted:

I tried to use payment_intent_data:

class StripeCheckoutMonthlyView(APIView):
   def post(self, request, *args, **kwargs):
        # try:
        checkout_session = stripe.checkout.Session.create(
            line_items=[
                {
                    'price': settings.STRIPE_PRICE_ID_MONTHLY,
                    'quantity': 1,
                },
            ],
            payment_method_types=['card'],
            mode='subscription',
            success_url=settings.SITE_URL + '/pagamento/?success=true&session_id={CHECKOUT_SESSION_ID}',
            cancel_url=settings.SITE_URL + '/?canceled=true',
            metadata={'user_id': request.user.id,
                        'plan_type': 'monthly'},
            payment_intent_data={
                'metadata': {
                    'user_id': request.user.id,
                }
            }
        )
        return Response({'url': checkout_session.url,
                            'id': checkout_session.id,
                            }, status=status.HTTP_200_OK)

and then use it in the appropriate function:

def add_info_card(payment_method):
    """
    Update the user's card details and payment date based on the payment intent.
    
    Args:
        payment_method: The payment intent object from Stripe containing card and charge details.
        user: The user object retrieved from the database.
    """
    print('Payment Method: ', payment_method)
    user = get_object_or_404(User, id=payment_method.metadata.user_id)

    last4 = payment_method['card']['last4']
    payment_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    brand_card = payment_method['card']['brand']

    print('Last 4: ', last4)
    print('Payment Date: ', payment_date)
    
    # Update the user with card details and payment date
    user.card_last4 = last4
    user.brand_card = brand_card
    user.payment_date = payment_date
    user.save()
    print(f"User {user.id} updated with card details and payment date.")

but received the error:

stripe._error.InvalidRequestError: Request req_td3acLmE4ziQqi: You can not pass `payment_intent_data` in `subscription` mode.

Questions:

  1. How can I access Checkout Session metadata when handling payment_method events?
  2. What is the best way to link payment_method to Checkout Session metadata in the webhook?

For Checkout Session with subscription mode, the metadata should be set under subscription_data.metadata.

The metadata will be available in following places:

Metadata won't be available on Payment Method or Payment Intent objects.

Back to Top