Strapi BadRequestError

System Information
  • Strapi Version: 4.10.5
  • Operating System: All, tested on windows, linux and macos
  • Database: Postgres
  • Node Version: v18.16.0
  • NPM Version: v9.5.1
  • Yarn Version: 1.22.19

I am running into an issue with the following:

Stripe sends an order confirmed request to my NextJS app whenever an order is confirmed. I then parse the products and create an order like so:

const order = {
	user_name: checkoutSession.shipping_details.name,
	email: checkoutSession.customer_details.email,
	order_number: checkoutSession.metadata.order_number,
	payment_success: true,
	paid_at: new Date(),
	payment_method: paymentIntent.payment_method_types[0],
	shipped: false,
	shipping_price: checkoutSession.shipping_cost.amount_total / 100,
	subtotal_price: checkoutSession.amount_subtotal / 100,
	total_price: checkoutSession.amount_total / 100,
	payment_intent: paymentIntent.id,
	ordered_products: orderedProducts.map((product) => {
		return {
			jewel: {
				connect: [product.id],
			},
			quantity: product.quantity,
			size: product.size ? product.size : "One size",
		}
	}),
	shipping: {
		line1: checkoutSession.shipping_details.address.line1,
		line2: checkoutSession.shipping_details.address.line2,
		city: checkoutSession.shipping_details.address.city,
		state: checkoutSession.shipping_details.address.state,
		postal_code: checkoutSession.shipping_details.address.postal_code,
		country: checkoutSession.shipping_details.address.country,
	},
	invoice: {
		line1: checkoutSession.shipping_details.address.line1,
		line2: checkoutSession.shipping_details.address.line2,
		city: checkoutSession.shipping_details.address.city,
		state: checkoutSession.shipping_details.address.state,
		postal_code: checkoutSession.shipping_details.address.postal_code,
		country: checkoutSession.shipping_details.address.country,
	},
}

So this still works fine. After the order creation, whenever I try to post it to Strapi, like so:

const orderInsert = await axios
	.post(
		`${strapiUrl}/api/orders`,
		{
			data: order,
		},
		axiosConfig
	)
	.then((res) => res.data)
	.catch((err) => {
		console.log(
			"❌ Error creating order in Strapi: ",
			err.response.data
		)
	})

This returns the error:

❌ Error creating order in Strapi:  {
  data: null,
  error: {
    status: 400,
    name: 'BadRequestError',
    message: 'Bad Request',
    details: {}
  }
}

Interestingly enough it does actually create the order, but it does so twice! I really don’t understand this behaviour. Every other post I do works just fine. My guess is that the BadRequestError gets returned because it tried adding a order which already exists. But why does it create two? Nowhere else in the app am I calling this route.

What did I try to fix this?

  • Change the order object, this returns normal errors, like:
❌ Error creating order in Strapi:  {
  data: null,
  error: {
    status: 400,
    name: 'ValidationError',
    message: 'payment_method must be defined.',
    details: { errors: [Array] }
  }
}
  • Playing with the permissions. I set all order permissions to public, still did not work (also don’t see why this would work, but okay).
  • Check the swagger documentation to see if I’m doing this correctly (I am as far as I could see).
  • Compare other (working) post request to check for differences, there were none.

If anyone knows more about this issue, please let me know!