#phaniophrero

1 messages ยท Page 1 of 1 (latest)

fading thicketBOT
acoustic stream
#

@grizzled ginkgo please use the thread here

grizzled ginkgo
#

I've kind of tried that before but it didn't got removed

#

can I show you my code and tell me if is the right place to do it

#

so this is my form component

#

this is the checkout component

#

`import { useState, useEffect } from "react";
import { loadStripe } from "@stripe/stripe-js";
import { Elements } from "@stripe/react-stripe-js";
import { useRouter } from "next/router";
import Link from "next/link";
import PaymentForm from "./PaymentForm";
import CheckoutForm from "./CheckoutForm";

const stripePromise = loadStripe(
process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
);

const Checkout = ({ course_id }) => {
const [clientSecret, setClientSecret] = useState("");

const router = useRouter();

console.log("course_id: ", course_id);

useEffect(() => {
fetch("http://localhost:8000/api/create-payment-intent/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(course_id),
})
.then((res) => res.json())
.then((data) => setClientSecret(data.clientSecret));
}, []);

const options = {
clientSecret: clientSecret,
// just added this right now
fields: {
billingDetails: {
address: {
country: "never",
postalCode: "never",
},
},
},
};

console.log("clientSecret: ", clientSecret);
// const handleCheckout = () => {
// router.push(/checkout/${course_id});
// };

return (
<div className="btn-checkout-wrapper">
{clientSecret && (
<Elements stripe={stripePromise} options={options}>
<PaymentForm />
{/* <CheckoutForm /> /}
</Elements>
)}
{/
<button
style={{
background: "green",
border: "none",
padding: ".5rem",
borderRadius: ".5rem",
}}
onClick={handleCheckout}
className="btn-checkout"
>
Checkout
</button> */}
</div>
);
};

export default Checkout;`

acoustic stream
#

In <PaymentElement id="payment-element" />, I don't see you setting options field and disable the billing address country

grizzled ginkgo
#

so in the <PaymentElement/> i should pass the options ?

acoustic stream
#

Yup! It should be set as <PaymentElement />, not <Elements />

grizzled ginkgo
#

const options = { clientSecret: clientSecret, // just added this right now fields: { billingDetails: { address: { country: "never", postalCode: "never", }, }, }, };

#

this ones ?

#

should I leave the clientSecret there or not ?

acoustic stream
#

There are two options to be set. One at <Elements /> and another one at <PaymentElement />

#

The one with client secret is the options field at <Elements /> and client secret should be set

grizzled ginkgo
#

ok

acoustic stream
#

Billing address country should be set at the options of <PaymentElement />

grizzled ginkgo
#

ok , I'll change that now

#

and come back with an update

#

ok

#

so

#

it didn't worked..

#

`import { useState, useEffect } from "react";
import { loadStripe } from "@stripe/stripe-js";
import { Elements } from "@stripe/react-stripe-js";
import { useRouter } from "next/router";
import Link from "next/link";
import PaymentForm from "./PaymentForm";
import CheckoutForm from "./CheckoutForm";

const stripePromise = loadStripe(
process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
);

const Checkout = ({ course_id }) => {
const [clientSecret, setClientSecret] = useState("");

const router = useRouter();

console.log("course_id: ", course_id);

useEffect(() => {
fetch("http://localhost:8000/api/create-payment-intent/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(course_id),
})
.then((res) => res.json())
.then((data) => setClientSecret(data.clientSecret));
}, []);

const options = {
clientSecret: clientSecret,
};

const optionsPaymentElement = {
fields: {
billingDetails: {
address: {
country: "never",
postalCode: "never",
},
},
},
};

console.log("clientSecret: ", clientSecret);
// const handleCheckout = () => {
// router.push(/checkout/${course_id});
// };

return (
<div className="btn-checkout-wrapper">
{clientSecret && (
<Elements stripe={stripePromise} options={options}>
<PaymentForm options={optionsPaymentElement} />
{/* <CheckoutForm /> /}
</Elements>
)}
{/
<button
style={{
background: "green",
border: "none",
padding: ".5rem",
borderRadius: ".5rem",
}}
onClick={handleCheckout}
className="btn-checkout"
>
Checkout
</button> */}
</div>
);
};

export default Checkout;`

#

it still shows the default country and gets my country france

#

is there something I did wrong ?

#

I also have some code wrote in Django in order to create the payment intent and all of that...

#

do I need to set it from there ?

acoustic stream
#

I don't see optionsPaymentElement set in <PaymentElement />

grizzled ginkgo
#

<PaymentForm options={optionsPaymentElement} />

#

is there

acoustic stream
#

It's not setting at <PaymentElement />

grizzled ginkgo
#

oh wait

#

sorry

#

my bad

acoustic stream
#

PaymentForm is your own react component

grizzled ginkgo
#

i just pass it down to the payment form and use the prosp to serve the options to PaymentElement

#

done

#

thank you

acoustic stream
#

Great to see that it's working now

grizzled ginkgo
#

so basically now is this the wright way I'm passing the billing details data

#

?

#

in this component

#

here

#

and for example if I'd like to add some extra fields like customer ID or Invoice ID or Business name , this like that , that usually goes in a invoice ?

acoustic stream
#

Yup! That looks right to me. Alternatively, you can set optionsPaymentElement in the PaymentForm component and set it directly to <PaymentElement/> instead of passing through props

grizzled ginkgo
#

brb in 5 minutes

acoustic stream
#

and for example if I'd like to add some extra fields like customer ID or Invoice ID or Business name , this like that , that usually goes in a invoice ?
Where are you going to add?

grizzled ginkgo
#

well

#

in the same component I've send above

#

I mean where I've already added those fields address , country , city , zipcode..

#

I'm trying go get all this data to be able to create an invoice

#

do you have any suggestions ? ๐Ÿ™‚

acoustic stream
#

Those fields are collected automatically when necessary. These information might not be collected if the payment doesn't require it. If you wish to collect them all the time, you may disable them in Payment Element and collect with your own form

grizzled ginkgo
#

how can I do that ? I mean I thought that I'm already doing that by adding those fileds I have already added. But you are saying that what I did there is not actually collecting those fileds with my own form ?

acoustic stream
#

Oh sorry! I didn't look clearly. If you have added those fields in your own form, you can submit the information in confirmPayment (which you already did), so that it'll reflect in the Payment Intent which you can retrieve it later

grizzled ginkgo
#

I'm getting this error now when I try to submit the payment

#

why is that ?

#

and that is the console

acoustic stream
grizzled ginkgo
#

you mean, to go to the dashboard and look for the payment that dind't went through ?

#

it says

#

invalid_request_error - payment_method_data[billing_details][address][country]
Country 'France' is unknown. Try using a 2-character alphanumeric country code instead, such as 'US', 'EG', or 'GB'. A full list of country codes is available at https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements

Was this useful?

Yes

No

ISO 3166-1 alpha-2 codes are two-letter country codes defined in ISO 3166-1, part of the ISO 3166 standard published by the International Organization for Standardization (ISO), to represent countries, dependent territories, and special areas of geographical interest. They are the most widely used of the country codes published by ISO (the other...

#

I guess

acoustic stream
#

Any API failure (like the screenshot) will be reflected under request history in the Dashboard. I'd need the request ID (req_xxx) of this particular request failures to check the reason

grizzled ginkgo
#

the country

#

is not right ?

acoustic stream
#

Can you share the request ID (req_xxx)?

grizzled ginkgo
#

yes

#

req_ogKKxr6M1NbvG8

#

this one ?

acoustic stream
#

In your case, France should be FR

grizzled ginkgo
#

so I sould do a conversion behing the scene , for example I should let the user type the word "france" and behind the scene to convert it to "fr" ?

acoustic stream
#

Yup, correct

grizzled ginkgo
#

if I'm not asking to much already

#

do you have something i mind

#

of how can I do that in a right way ?

acoustic stream
grizzled ginkgo
#

so do you think if I check for the first two letters of each country words like France first two words are FR to set its value to FR ?

#

in the state

acoustic stream
#

No, that's not correct. Two letters is not always the first two character. For example, United Kingdom is GB which is irrelevant to the first two character

#

You will check the full name of the country and map it to its own 2-character alphanumeric country code

grizzled ginkgo
#

right

#

ok thank you so much river

#

what are you specialized on ? react js / next js or other framework ?

#

do you also do backend too ?

#

just a courious question

#

if you don't mind

#

๐Ÿ™‚

acoustic stream
#

Engineers supporting this channel are mainly full stack

#

We work with both frontend and backend

grizzled ginkgo
#

right

#

well thank you again for your support

#

and quick respose