Stripe subscriptions checkout.session.completed resource missing error of invalid_request_error type

Viewed 754

I am trying to understand why webhook for checkout.session.completed is crashing when testing it locally with the Stripe CLI. I am using djstripe. My webhook for customer.subscription.created is successful. With the CLI, I am getting the following error with the stripe trigger checkout.session.completed command:

Request failed, status=404, body={
  "error": {
    "code": "resource_missing",
    "doc_url": "https://stripe.com/docs/error-codes/resource-missing",
    "message": "No such payment_page: 'ppage_1Il33eAyVjQjzOsRERzYQSbK'",
    "param": "payment_page",
    "type": "invalid_request_error"
  }

Is the problem with the success url routing? views.py

def create_checkout_session(request):
    """Create a checkout session for Stripe."""
    data = json.loads(request.body)
    priceId = data.get("priceId")
    if not Price.objects.filter(id=priceId).exists():
        messages.add_message(request, messages.ERROR,
                             "That plan price is not available. Please contact support for help.", )
        return HttpResponseRedirect(reverse("users:index"))
    request.account = OrgMembership.objects.filter(my_identity=request.user).first()
    sessionId = stripegateway.create_checkout_session(priceId, request)
    return JsonResponse({"sessionId": sessionId})

stripegateway.py

class StripeGateway:
    """Gateway interacts with the APIs required by Stripe. Credit: Matt Layman"""
    def create_checkout_session(self, priceId, request):
        """Create a checkout session based on the subscription price."""
        site = Site.objects.get_current()
        subscription_success = reverse("users:success")
        stripe_cancel = reverse("users:stripe_cancel")
        request.account = OrgMembership.objects.get(my_identity=request.user.id)
        request.org = request.account.my_org
        # session_parameters = {
        #     "customer_email": request.org.billing_email,
        #     "success_url": f"http://{site}{subscription_success}",
        #     "cancel_url": f"https://{site}{stripe_cancel}",
        #     "payment_method_types": ["card"],
        #     "mode": "subscription",
        #     "line_items": [{"price": priceId, "quantity": 1}],
        #     "client_reference_id": str(request.account.id),
        # }
        # checkout_session = stripe.checkout.Session.create(**session_parameters)
        # return checkout_session["id"]
        checkout_session = stripe.checkout.Session.create(
            customer_email = request.org.billing_email,
            client_reference_id = str(request.account.id),
            payment_method_types=['card'],
            line_items=[{
                'price': priceId,
                'quantity': 1,
            }],
            mode='subscription',
            success_url=request.build_absolute_uri(reverse('users:success')) + '?session_id={CHECKOUT_SESSION_ID}',
            cancel_url=request.build_absolute_uri(reverse('users:index')),
        )
        return checkout_session["id"]

The commented out section is original code used and then tried alternative in efforts to try to fix the problem. Any guidance is much appreciated.

2 Answers

I had this same issue. stripe trigger checkout.session.completed would return the following:

Trigger failed: Request failed, status=404, body={
  "error": {
    "code": "resource_missing",
    "doc_url": "https://stripe.com/docs/error-codes/resource-missing",
    "message": "No such payment_page: 'ppage_1IryQdFE8p8XoGx4OJLXOv4p'",
    "param": "payment_page",
    "type": "invalid_request_error"
  }
}

I fixed the error by updating my stripe CLI version from v1.5.4 to v1.5.14.

Make sure to forward the webhook events to a correct and working endpoint.

Use the forward-to command line parameter to point the stripe-cli to your development server:

stripe listen \
--skip-verify \
--forward-to https://localhost:8000/payments/webhook/ \
--log-level debug

The skip-verify is needed for self-signed or otherwise invalid certificates when using HTTPS endpoint. Django is also picky about having a slash at the end of URLs.

Related