richnologies / ngx-stripe

Angular 6+ wrapper for StripeJS
MIT License
219 stars 77 forks source link

save a credit card for later payment #173

Closed Karman40 closed 2 years ago

Karman40 commented 2 years ago

I want to ask for help. How can I save a credit card without a transaction? Which module should I use?

I tried to use the ngx-stripe-card element, if you need to use this, is it possible for desing to look like Angular Material input fields? Is it possible to save Google Pay and Apple Pay here?

I got a token error when using the ngx-stripe-payment element.

Thanks in advance!

richnologies commented 2 years ago

Hi @Karman40,

Yes you can. It can be done both the Card and the Payment Element, but since you suggest you're interested too in Google Pay and Apple Pay I'm going to show how to do it using the Payment Element.

When we want to save a payment method without charging the customer, we need to use a Setup Intent instead of Payment Intent

The only other consideration is that you also need to create a Customer first. This makes since, we need to the create the customer so then we can use SetupIntents to attach the payment method to it.

In summary:

We create the customer in our server (check the docs for more info)

const stripe = require('stripe')('sk_test_XXXXXXX');
const customer = await stripe.customers.create({
  name: 'John Doe',
  email: 'support@ngx-stripe.dev',
  description: 'My First Test Customer (created for API docs at https://www.stripe.com/docs/api)',
});

We create the Setup Intent in our server to use with the Payment Element:

const stripe = require('stripe')('sk_test_XXXXXXX');
const setupIntent = await stripe.setupIntents.create({
  customer: customer.id
});

We confirm the setup in the client:

this.stripeService.confirmSetup({
  elements: this.paymentElement.elements,
  confirmParams: {
    payment_method_data: {
      billing_details: {
        name: this.checkoutForm.get('name').value,
        email: this.checkoutForm.get('email').value,
        address: {
          line1: this.checkoutForm.get('address').value,
          postal_code: this.checkoutForm.get('zipcode').value,
          city: this.checkoutForm.get('city').value,
        },
      },
    },
  },
  redirect: 'if_required',
})
.subscribe({
   // DO SOMETHING
});

Here is a link to a Stackblitz with a full example:

https://stackblitz.com/edit/ngx-stripe-issue-173?file=src/app/app.component.ts&view=editor

A few notes no the example:

I hope this helps you, if need anything else just let me know. Thanks for the issue, I'll add this example to the docs so it might help others too.

Kind regards

R

Karman40 commented 2 years ago

@richnologies Thank you so much for this detailed description so everything will go.

denes16 commented 2 years ago

can i use this on angular 11? Because theres no payment element component on that version

richnologies commented 2 years ago

Hi @denes16,

Version 11.2.0 should have the payment element component

Please let me know if that is not the case

Have a nice day

R