Continue Discussion 10 replies
November 2021

Saint_Everest

Thanks so much for this wonderful tutorial on strapi, nextjs, stripe and graphql.
it is very educative.
i am in part 6 now and the stripe integration works fine.
Sir, when I click the confirm order button the payment will be successful but there is no feedback on the client-side and no redirect to another page
thanks

October 2023

Xochitl_Mora

I made it this far in the tutorial; however, in my Content Manager, orders post as drafts (not published) and do not include the relation (the currently logged in user placing the order). Help!

1 reply
October 2023 ▶ Xochitl_Mora

Paul_Brats Strapi Developer Advocate

Can you provide the code example that handles the order action. The reason why you might not have the user relation set is that you are not passing the userID. And order show up as draft because you are most likely not passing the the publishedAt with a date.

1 reply
October 2023 ▶ Paul_Brats

Xochitl_Mora

Hi, this is straight from tutorial:
//& don’t change this

“use strict”;
const stripe = require(“stripe”)(“sk_test_key_goes_here”);
const { createCoreController } = require(“@strapi/strapi”).factories;

// Create the order controller
module.exports = createCoreController(“api::order.order”, ({ strapi }) => ({
async create(ctx) {
// Get the authenticated user from the context
const user = ctx.state.user;

// Check if a user is authenticated
if (!user) {
  return ctx.unauthorized("You are not authorized!");
}

// Destructure the necessary properties from the request body
const { address, amount, dishes, token, city, state } = ctx.request.body.data;

try {
  // Charge the customer using Stripe
  await stripe.charges.create({
    amount: amount,
    currency: "usd",
    description: `Order ${new Date()} by ${ctx.state.user.id}`,
    source: token
  });

  // Create the order and associate it with the authenticated user
  const order = await strapi.service("api::order.order").create({
    data: {
      amount,
      address,
      dishes,
      city,
      state,
      token,
      user: ctx.state.user.id // Set the user field to the ID of the authenticated user
    },
  });

  // Return the created order
  return order;
} catch (err) {
  // Handle errors, return a 500 error for simplicity in this example
  console.log("err", err);
  ctx.response.status = 500;
  return {
    error: { message: "There was a problem creating the charge" },
    address,
    amount,
    dishes,
    token,
    city,
    state,
  };
}

},
}));

1 reply
October 2023

Xochitl_Mora

Also observed that the user and the relation updated posted twice after dozens of tries after I clicked “user” in the displayed fields…