richnologies / ngx-stripe

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

Card Info Valid output #11

Closed simonaberry closed 6 years ago

simonaberry commented 6 years ago

is there a way to determine if the card information is valid ? I wish to disable the buy button until such time.

picosam commented 6 years ago

I think there's a CSS class that's automatically set when you're typing the card info.

simonaberry commented 6 years ago

but the button that I want to disable is outside the stripe iframe... are you saying the ngx-stripe component will emit a class ?

picosam commented 6 years ago

No, in that case I take my words back!

richnologies commented 6 years ago

If you use the elements.create method like in the first example in the readme, then the card object has a: card.on('change', cb) method, so you disable the button. If you are using the ngx-stripe-card component, you can't access that by now, but I will soon add an output to allow to subscribe to that event stream.

card.on('change', function(event) { if (event.error) { errorElement.textContent = event.error.message; errorElement.classList.add('visible'); } });

ericuldall commented 6 years ago

@richnologies Your example seems to fail with the following error:

 Argument of type '(event: any) => void' is not assignable to parameter of type '() => void'.

Any insight on how to resolve this?

lukaVarga commented 6 years ago

Any news regarding this @richnologies ? TS won't compile and, as far as I know, there is no other way to catch errors before the user tries to submit the form.

smlee commented 6 years ago

For the eventlisteners you can just create a function and use bind to pass in this

example:

this.cardNumber.on('change', this.onChange.bind(this));

onChange(event) {
    if (event && !!event.error) {
      this.errorMessage = event.error.message;
    } else {
      this.errorMessage = '';
    }
    this.cdr.detectChanges();
  }
richnologies commented 6 years ago

Hi, sorry for the delay, I believe his error is solved now, if anyone have any more problems about this, can always reopen

jorgeartigas commented 6 years ago

What about if we are using the ngx-stripe-card component, is there any way to determine if the information is valid, before clicking buy? I want to disable the button until is valid

evaletolab commented 6 years ago

this one,

        this.card = this.elements.create(...);
        this.card.addEventListener('change', (event)=> {
          // use it in template [disabled]="!isValid"
          this.isValid=event.complete;
        });
DwieDima commented 3 years ago

I also had to strugge with this to set my button disabled. This is my workaround:

  @ViewChild(StripeCardNumberComponent) number: StripeCardNumberComponent;
  @ViewChild(StripeCardExpiryComponent) expiry: StripeCardExpiryComponent;
  @ViewChild(StripeCardCvcComponent) cvc: StripeCardCvcComponent;
  public isValid = false;

...
ngOnInit() {
    const number$ = fromEvent(this.number.element as any, 'change');
    const expiry$ = fromEvent(this.expiry.element as any, 'change');
    const cvc$ = fromEvent(this.cvc.element as any, 'change');

    combineLatest([number$, expiry$, cvc$])
      .pipe(
        map(([number, expiry, cvc]: any) => {
          return (
            !!!number.error &&
            number.complete &&
            !!!expiry.error &&
            expiry.complete &&
            !!!cvc.error &&
            cvc.complete
          );
        }),
        tap((isValid) => (this.isValid = isValid))
      )
      .subscribe();
}
    <button [disabled]="!isValid" (click)="onAddCreditCard()"
      >save</button