最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stripe checkout - Remove or make email field read only - Stack Overflow

programmeradmin2浏览0评论

I'm using stripe checkout as described in

On the server side, I create a new stripe customer (if there isn't one already), and use that customer id to create a checkout session. and then redirect to checkout using stripe api

stripe.redirectToCheckout({
  // Make the id field from the Checkout Session creation API response
  // available to this file, so you can provide it as parameter here
  // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
  sessionId: '{{CHECKOUT_SESSION_ID}}'
}).then(function (result) {
  // If `redirectToCheckout` fails due to a browser or network
  // error, display the localized error message to your customer
  // using `result.error.message`.
});

in the checkout page (which is stripe hosted), there is a email field which is pre populated, but is editable. I have had customers changing that email address. Is there a way I can make the email address on stripe checkout readonly field?

I'm using stripe checkout as described in https://stripe./docs/payments/checkout

On the server side, I create a new stripe customer (if there isn't one already), and use that customer id to create a checkout session. and then redirect to checkout using stripe api

stripe.redirectToCheckout({
  // Make the id field from the Checkout Session creation API response
  // available to this file, so you can provide it as parameter here
  // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
  sessionId: '{{CHECKOUT_SESSION_ID}}'
}).then(function (result) {
  // If `redirectToCheckout` fails due to a browser or network
  // error, display the localized error message to your customer
  // using `result.error.message`.
});

in the checkout page (which is stripe hosted), there is a email field which is pre populated, but is editable. I have had customers changing that email address. Is there a way I can make the email address on stripe checkout readonly field?

Share Improve this question edited Jul 12, 2022 at 21:05 Super Kai - Kazuya Ito 1 asked May 11, 2020 at 9:28 rragrrag 6577 silver badges15 bronze badges 3
  • 2 Unfortunately, that's currently not possible. Providing the customer id allows Stripe to keep Customer and PaymentMethods as up to date as possible. This includes allowing the Customer to change their email address. While not perfect, you could detect those changes in your code using webhooks, and have the user reconcile or verify what should be on the customer object. stripe./docs/api/customers/update#update_customer-email – v3nkman Commented May 11, 2020 at 12:45
  • @v3nkman Do you know if Stripe validates the new email before allowing the payment to go through? I went through the trouble of implementing my own validation before passing the email to the Stripe checkout form, and if Stripe allows them to change the email without validation, this would undermine all that work. At the same time I don't want to just pass customer_email per MatiasG's solution, because that can create duplicate customers. I am trying to follow best practices and it's almost like Stripe's API isn't allowing me to do so. – cazort Commented Oct 22, 2021 at 19:46
  • 1 @cazort This is now resolved by stripe. See my answer below – rrag Commented Nov 3, 2021 at 18:54
Add a ment  | 

5 Answers 5

Reset to default 4

I got an email from stripe today

We are disabling customers’ ability to overwrite their email address Starting October 26, customers can no longer edit their email address after you pass it into a Checkout Session. We’ve made this change to prevent duplicative subscriptions from being created by accident for existing customers and prevent unexpected updates to the Customer object during Checkout.

And here it is in all its glory

A possible work around:

You can use customer_email instead customer parameter when you create a session. In this case users can not change the value of email field.

Then, you can get the created customer and update it if you need added some data using the API: https://stripe./docs/api/customers/update

But, the problem is that stripe will create a new customer for each checkout.

Use "customer_email" parameter as shown below and this is the example in Python:

checkout_session = stripe.checkout.Session.create(
    customer_email='[email protected]', # Here
    line_items=[
        {
            'price_data': {
                'currency': 'jpy',
                'unit_amount': 1000,
                'product_data': {
                    'name': 'Honey',
                    'description': 'Good honey',
                },
            },
            'quantity': 1,
        },
    ],
    mode='payment',
    success_url= "https://example./success.html",
    cancel_url='https://example./cancel',
)

And because you use "customer_email" parameter so the email field is readonly as shown below:

Now, according to the Docs:

If the customer changes their email on the Checkout page, the Customer object will be updated with the new email.

Given that Stripe allows "customers" to change the email id. we pass to the Checkout form, an idea that could work for you is to add your "internal" identifier (be it an email or other internal user id) as Metatadata to the customer you create, like so (below code is in c#, but you get the idea):

Dictionary<string, string> customerMetaData = new Dictionary<string, string>();
customerMetaData.Add("myappuserid", "{useyourinternalidhere}");

var customer = await cs.CreateAsync(new CustomerCreateOptions { Email = "{emailthat youwanttouseforcustomer}", Metadata = customerMetaData });

//Now you can pass this customer when you create the session for checkout

The point being whatever email the user ends up using, you can always get the corresponding internal user id as well from the customer object's metadata.

In the "success" call back, you can then retrieve the customer data and then save the returned customer details - including the stripe customer id / email that end user used in check out form / your own internal id that you can now retrieve from the customer metadata -- that way you can maintain a mapping of stripe's details to your own internal customer details.

//This code is on the "success" call back (when the session id. is passed)
var sessionService = new SessionService();
var session = await sessionService.GetAsync(session_id, new SessionGetOptions { Expand = new List<string> { "customer" , "line_items", "subscription" } });
var sessionCustomer = session.Customer;

//This sessionCustomer now has the metadata that you passed while creating the customer
//Save the Stripe ids / email id along with data retrieved from the Metadata;
//Now you have all the info you required..

Of course this response is delayed - hopefully, it helps others...

发布评论

评论列表(0)

  1. 暂无评论