简体   繁体   中英

stripe js create a customer

I need to create a new customer on stripe using their JS client.

I don't seem to be able to find the method for creating it,

here's my code:

<script src="https://js.stripe.com/v3/"></script>

to load the script in the page

then in my js file:

I have:

const stripe = Stripe(App.Config.stripeKey);

function createCustomer(){
    return Stripe.customers.create({email: $('#payment-form').data('email')}).then(function(result) {
        if (result.error) {
            const errorElement = document.getElementById('card-errors');
            errorElement.textContent = result.error.message;
            return new Promise((resolve, reject) => {
                reject(result.error.message);
            });
        } else {
            stripeCustomerTokenHandler(result.token);
            return new Promise((resolve, reject) => {
                resolve(result.token);
            });
        }
    })
}

I tried also with stripe.customers.create({...} (ie using the Stripe object) but in both cases I get Uncaught TypeError: Cannot read property 'create' of undefined

any ideas?

Stripe.js has many feature and functions including mounting card Elements and confirming payments using a publishable key, but it is not used for addressing the API directly.

Calls to the API to do things like create Customers or PaymentIntents, must be done from the backend with your secret key. Your frontend can interact with your backend server using RESTful calls which in turn can make calls to the API to create Customers from the server. For example in node your backend could use this call [1].

const stripe = require('stripe')('sk_test_xxx');

const customer = await stripe.customers.create({
  description: 'My First Test Customer (created for API docs)',
});

An example of an integration that illustrates both the use of backend code and Stripe.js to confirm a payment can be seen here [2].

[1] https://stripe.com/docs/api/customers/create?lang=node

[2] https://stripe.com/docs/payments/accept-a-payment?integration=elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM