#djk3600

1 messages · Page 1 of 1 (latest)

sudden bobcatBOT
heavy stag
#

What's your Q?

tall nest
#

hey snufkin

#

thanks for taking it on so fast

#

okay so

#

first question

#

How do I send an order confirmation to a user after they have ordered my product?

heavy stag
#

Well the most straightforward would be a Receipt Email. But how are your customers ordering your product?

tall nest
#

So they are ordering by adding it to basket and purchasing via stripe

#

Very classic e-commerce

#

I just want the customer to get a confirmation email once they have ordered

#

something like

#

'
Hi Joe Bloggs,

Thanks so much for your order.
Which comes to a total of £15.99'

heavy stag
#

You can customize the email receipt and read about it here: https://stripe.com/docs/receipts
I still think that sounds like the best fit for what you are trying to do

tall nest
#

oh wow! you can automatically enable reciepts

#

like so

heavy stag
#

Yup

tall nest
#

(I just enabled) with this send a confirmation to a customer then 🙂

heavy stag
#

You can test sending a receipt to yourself first

tall nest
#

damn it didnt come through

#

I definitely saved it as well

heavy stag
#

Can you try updating the Payment Intent with a receipt_email parameter?

tall nest
#

okay brilliant this is what I was about to ask about!

#
const paymentIntent = await stripe.paymentIntents.create({
    amount: calculateOrderAmount(items),
    currency: "gbp",
    automatic_payment_methods: {
      enabled: true,
    },
    description,
    shipping: {
      address: {
        line1: shipping.line1,
        line2: shipping.line2,
        city: shipping.city,
        country: shipping.country,
        postal_code: shipping.postal_code,
      },
      name: shipping.name,
      phone: shipping.phone,
    },
    // receipt_email:
  });
#

what does in reciept_email ?

heavy stag
#

We explain this in our docs

When creating a PaymentIntent, include your customer’s email address as a value for the receipt_email parameter. Receipts are only sent when a successful payment has been made—no receipt is sent if the payment fails or is declined.

tall nest
#

oooooh I see!

#

it needs to be added into the checkout form

#

the same way,

line1
line2
city
country
postal_code

#

have been??

heavy stag
#

No, you add it when you create (or update) the Payment Intent via the API

tall nest
#

okay I dont understand at all

#

lol

#
useEffect(() => {
    // Create PaymentIntent as soon as the page loads
    fetch("http://localhost:4242/create-payment-intent", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        items: cartItems,
        userEmail: customerEmail,
        shipping: shippingAddress,
        billing: billingAddress,
        description,
      }),
    })
      .then((res) => {
        if (res.ok) {
          return res.json();
        }
        return res.json().then((json) => Promise.reject(json));
      })
      .then((data) => {
        setClientSecret(data.clientSecret);
      })
      .catch(() => {
        setMessage("Failed to initialize checkout");
        toast.error("Something went wrong!!!");
      });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
#

This payment intent?

#

okay so to explain

#

This is the payment intent on the server

app.post("/create-payment-intent", async (req, res) => {
  const { items, shipping, description } = req.body;

  // Create a PaymentIntent with the order amount and currency
  const paymentIntent = await stripe.paymentIntents.create({
    amount: calculateOrderAmount(items),
    currency: "gbp",
    automatic_payment_methods: {
      enabled: true,
    },
    description,
    shipping: {
      address: {
        line1: shipping.line1,
        line2: shipping.line2,
        city: shipping.city,
        country: shipping.country,
        postal_code: shipping.postal_code,
      },
      name: shipping.name,
      phone: shipping.phone,
    },
    // receipt_email: customerEmail
  });
#

This is the payment intent in the front end

useEffect(() => {
    // Create PaymentIntent as soon as the page loads
    fetch("http://localhost:4242/create-payment-intent", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        items: cartItems,
        userEmail: customerEmail,
        shipping: shippingAddress,
        billing: billingAddress,
        description,
      }),
    })
      .then((res) => {
        if (res.ok) {
          return res.json();
        }
        return res.json().then((json) => Promise.reject(json));
      })
      .then((data) => {
        setClientSecret(data.clientSecret);
      })
      .catch(() => {
        setMessage("Failed to initialize checkout");
        toast.error("Something went wrong!!!");
      });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
#

you still there?

heavy stag
#

Sorry it's really busy right. now

tall nest
#

Yeah I just noticed, you are quite literally the only person answering everyones queries

#

no worries, take your time! 🙂

heavy stag
#

So what I'm saying is you provide the Customer Email when you create the Payment Intent on the server in the receipt_email parameter

tall nest
#

okay awesome

#

but where am I grabbing the customer email

#

because currently when the customer is ordering a product, I am not taking their email in

heavy stag
#

Ah. Okay what front-end are you using to collect payment?

tall nest
#

I am using, react and of course stripe

#

so what I am doing is

#

taking this

const initialAddressState = {
  name: "",
  line1: "",
  line2: "",
  postal_code: "",
  country: "",
  phone: "",
};
#

saving in redux

  const handleSubmit = (e) => {
    e.preventDefault();
    dispatch(SAVE_SHIPPING_ADDRESS(shippingAddress));
    dispatch(SAVE_BILLING_ADDRESS(billingAddress));
    navigate("/checkout");
  };
#

then in checkout selecting this

#
  const billingAddress = useSelector(selectBillingAddress);

  useEffect(() => {
    // Create PaymentIntent as soon as the page loads
    fetch("http://localhost:4242/create-payment-intent", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        items: cartItems,
        userEmail: customerEmail,
        shipping: shippingAddress,
        billing: billingAddress,
        description,
      }),
    })
      .then((res) => {
        if (res.ok) {
          return res.json();
        }
        return res.json().then((json) => Promise.reject(json));
      })
      .then((data) => {
        setClientSecret(data.clientSecret);
      })
      .catch(() => {
        setMessage("Failed to initialize checkout");
        toast.error("Something went wrong!!!");
      });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
#

but naturally I need the order email, is it a good idea to have them add it say here

#

and that way I can send it to my server.js much like, line1, line2 etc?

heavy stag
#

Okay so you are using the Address Element also?

tall nest
#

This is my payment intent on my server.js

app.post("/create-payment-intent", async (req, res) => {
  const { items, shipping, description } = req.body;

  // Create a PaymentIntent with the order amount and currency
  const paymentIntent = await stripe.paymentIntents.create({
    amount: calculateOrderAmount(items),
    currency: "gbp",
    automatic_payment_methods: {
      enabled: true,
    },
    description,
    shipping: {
      address: {
        line1: shipping.line1,
        line2: shipping.line2,
        city: shipping.city,
        country: shipping.country,
        postal_code: shipping.postal_code,
      },
      name: shipping.name,
      phone: shipping.phone,
    },
    receipt_email: customerEmail
  });

  res.send({
    clientSecret: paymentIntent.client_secret,
  });
});
heavy stag
#

The Payment Element will collect the customer's email when it is required by the payment method chosen. So if you want to be able to send receipt emails you'll need to collect it yourself to be certain it's always included.

tall nest
#

ahhhh

#

so collect it here

#

also is there a specific way it needs to be formatted?

#

or is something like this okay

shipping: {
      address: {
        line1: shipping.line1,
        line2: shipping.line2,
        city: shipping.city,
        country: shipping.country,
        postal_code: shipping.postal_code,
      },
      name: shipping.name,
      phone: shipping.phone,
    },
    receipt_email: customerEmail.information
heavy stag
#

As long as that is a valid email address and a String value it should be fine

tall nest
#

woooo!! okay I am gonna give it a go now my man

#

am I okay to message if I run into any issues??

#

thanks a ton for your help 🙂

heavy stag
#

That's why we're here 🙂