npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-payment-inputs

v0.2.0

Published

A set of Angular Directives to validate and format credit card inputs.

Downloads

75

Readme

npm npm bundle size npm

Install

Before install the library, check if your Angular version are >=10.0.0

NPM

npm i --save ngx-payment-inputs

Yarn

yarn add ngx-payment-inputs

Usage

Import the main module into your feature module (example.: app.module.ts):

import { PaymentInputsModule } from 'ngx-payment-inputs';

// rest of code...

@NgModule({
  declarations: [AppComponent],
  imports: [
      BrowserModule,
      ReactiveFormsModule,
+     PaymentInputsModule,
    ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Now, go to your formGroup (or FormControl), and create all controls necessaries for usage (card number, card expiry date, card cvc).

// your feature component
// rest of code...

  ngOnInit(): void {
    this.form = this.fb.group({
      cardNumber: [null],
      cardCvc: [null],
      cardValidate: [null],
    });
  }

  // rest of code...

Into your HTML, add jstCardNumber, jstCardExpireDate and jstCardCvv for respectives inputs.

<form [formGroup]="form">
  <div>
    <label for="card-number">Card Number</label>
    <input id="card-number" formControlName="cardNumber" jstCardNumber />
  </div>

  <div>
    <label for="card-date">Card Date</label>
    <input id="card-date" formControlName="cardValidate" jstCardExpireDate />
  </div>

  <div>
    <label for="card-cvv">Card CVV</label>
    <input id="card-cvv" formControlName="cardCvc" jstCardCvv />
  </div>
</form>

Using custom validators

ngx-payment-inputs provides three custom validators to help to show errors form all credit card inputs.

  • PaymentInputValidators.cardNumber()
  • PaymentInputValidators.expiryDate()
  • PaymentInputValidators.CVC()

Example of usage:

// rest of code
import { CardTypeService, PaymentInputValidators } from 'ngx-payment-inputs';
// ...

  ngOnInit(): void {
    this.form = this.fb.group({
      cardNumber: [null, [PaymentInputValidators.cardNumber()],
      cardCvc: [null, [PaymentInputValidators.expiryDate()],
      cardValidate: [null, [PaymentInputValidators.CVC()],
    });
  }

Hacking your forms with ngx-payment-inputs service

This library not provide a component (yet 😏) with all credit card inputs, instead of this, provide three Angular Directives that made all job for you, and let the developer free to customize the input, but you can improve the user experience showing the credit card brand, for example.

To help the developer, we provide a service that exposes the card type as observable, see the below example:

// rest of code
import { CardTypeService } from 'ngx-payment-inputs';

@Component({
  selector: 'jst-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  type: string;

  constructor(private cardTypeService: CardTypeService) {}

  ngOnInit(): void {
    this.cardTypeService.cardType.subscribe((type) => (this.type = type.type));
  }
}

With this, you can show a credit card brand into your HTML based into type returned from our service.

See all credit card types (all values are lowercase):

  • visa
  • mastercard
  • amex
  • dinersclub
  • discover
  • jcb
  • unionpay
  • maestro
  • elo
  • hipercard

Public APIs

List with all public api exposed to use or extends:

| API | Type | Description | | ---------------------- | ------------------------- | -------------------------------------------------------------------------------------- | | PaymentInputsModule | Module | Module for Payment Inputs Directives and services | | jstCardNumber | Directive | Formatter for card. number | | jstCardExpireDate | Directive | Formatter for card expiry date . | | jstCardCvv | Directive | Formatter for card cvc. | | CardTypeService | Service | Service with all card input refs and card type. | | CardTypesModel | Interface/Type definition | Type definition for Card Type list (internal list used for identify a card by number). | | PaymentInputValidators | Class | Class with validators for: Card number, card expiry date and card CVC. |

Contributing

Fork the project and help us to improve the project.

Install dependencies with NPM: npm i. The project are into packages folder.

To build the project, run npm run build:payment-inputs, the artifact goes to dist/packages folder.

Running unit tests

WIP 🚧

Run nx test payment-inputs to execute the unit tests.

License

MIT @ Rai Siqueira