Table of Contents
@angular wrapper for StripeJS based on module by Ricardo Sánchez Gregorio at https://github.com/richnologies/ngx-stripe
- Stripe Service
- Lazy script loading
- AoT compliant
- SSR fallback
To install this library, run:
$ npm install @nomadreservations/ngx-stripe --save
or
$ yarn add @nomadreservations/ngx-stripe
Import the NgxStripeModule
into the application
The module takes the same parameters as the global Stripe object. The APIKey and the optional options to use Stripe connect
- apiKey: string
- options: { stripeAccount?: string; }
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// Import your library
import { NgxStripeModule } from '@nomadreservations/ngx-stripe';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgxStripeModule.forRoot('***your-stripe-publishable key***'),
LibraryModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Note: If you leave the publishable key blank you must set on using StripeSerive.changeKey prior to creating token's or sources
As an alternative to the previous example, you could use the StripeCardComponent.
It will make a little bit easier to mount the card.
To fetch the Stripe Element, you could you use either the (change) output, or, by using a ViewChild, the public method getCard()
//stripe.compnent.html
<ngx-stripe-card [options]="cardOptions" [elementsOptions]="elementsOptions" (change)="cardUpdated($event)" (error)="error = $event"></ngx-stripe-card>
<div class="error">
{{error?.message}}
</div>
<button (click)="getCardToken()" [disabled]="!complete">Get Card Token</button>
//stripe.component.ts
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { StripeService, StripeCardComponent, ElementOptions, ElementsOptions } from "@nomadreservations/ngx-stripe";
@Component({
selector: 'app-stripe-test',
templateUrl: 'stripe.component.html'
})
export class StripeTestComponent implements OnInit {
stripeKey = '';
error: any;
complete = false;
element: StripeElement;
cardOptions: ElementOptions = {
style: {
base: {
iconColor: '#276fd3',
color: '#31325F',
lineHeight: '40px',
fontWeight: 300,
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSize: '18px',
'::placeholder': {
color: '#CFD7E0'
}
}
}
};
elementsOptions: ElementsOptions = {
locale: 'en'
};
constructor(
private _stripe: StripeService
) {}
cardUpdated(result) {
this.element = result.element;
this.complete = result.card.complete;
this.error = undefined;
}
keyUpdated() {
this._stripe.changeKey(this.stripeKey);
}
getCardToken() {
this._stripe.createToken(this.element, {
name: 'tested_ca',
address_line1: '123 A Place',
address_line2: 'Suite 100',
address_city: 'Irving',
address_state: 'BC',
address_zip: 'VOE 1H0',
address_country: 'CA'
}).subscribe(result => {
// Pass token to service for purchase.
console.log(result);
});
}
}
The following command runs unit & integration tests that are in the tests
folder, and unit tests that are in src
folder:
yarn test
The following command:
yarn build
- starts TSLint with Codelyzer
- starts AoT compilation using ngc compiler
- creates
dist
folder with all the files of distribution
To test the npm package locally, use the following command:
yarn publish:dev
You can then run the following to install it in an app to test it:
yalc link @nomadreservations/ngx-stripe
yarn release:patch
or
yarn release:minor
or
yarn release:major
To generate the documentation, this starter uses compodoc:
yarn compodoc
yarn compodoc:serve
MIT