Gathering detailed insights and metrics for ngx-stripe-sca
Gathering detailed insights and metrics for ngx-stripe-sca
Gathering detailed insights and metrics for ngx-stripe-sca
Gathering detailed insights and metrics for ngx-stripe-sca
npm install ngx-stripe-sca
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
2
Add SCA PaymentIntent => waiting for ngx-stripe original project update Add these 3 methods : handleCardPayment(clientSecret: string, el: Element, data?: any): any; confirmPaymentIntent(clientSecret: string, el: Element, data: any): any; retrievePaymentIntent(clientSecret: string): any;
Need testing ...
To install this library, run:
1$ npm install ngx-stripe-sca
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
1import { BrowserModule } from '@angular/platform-browser'; 2import { NgModule } from '@angular/core'; 3 4import { AppComponent } from './app.component'; 5 6// Import your library 7import { NgxStripeModule } from 'ngx-stripe-sca'; 8 9@NgModule({ 10 declarations: [ 11 AppComponent 12 ], 13 imports: [ 14 BrowserModule, 15 NgxStripeModule.forRoot('***your-stripe-publishable key***'), 16 LibraryModule 17 ], 18 providers: [], 19 bootstrap: [AppComponent] 20}) 21export class AppModule { }
Once imported, you can inject the StripeService anywhere you need. The stripe script will be loaded the first time the service is injected.
The stripe service exposes the same methods as the StripeJS instance but with typescript types. The API is based on Observables so it can be combined with other actions.
In the example below, the component mounts the card in the OnInit lifecycle. The buy button creates a Stripe token the could be sent to the server for further actions. In this example we just log that token to the console:
Example component (more HTML and CSS examples can be found at the Stripe Elements Examples):
1<form novalidate (ngSubmit)="buy()" [formGroup]="stripeTest"> 2 <input type="text" formControlName="name" placeholder="Jane Doe"> 3 <div id="card-element" class="field"></div> 4 <button type="submit"> 5 BUY 6 </button> 7</form>
1import { Component, OnInit, ViewChild } from '@angular/core'; 2import { FormGroup, FormBuilder, Validators } from "@angular/forms"; 3 4import { StripeService, Elements, Element as StripeElement, ElementsOptions } from "ngx-stripe-sca"; 5 6@Component({ 7 selector: 'app-stripe-test', 8 templateUrl: 'stripe.html' 9}) 10export class StripeTestComponent implements OnInit { 11 elements: Elements; 12 card: StripeElement; 13 14 // optional parameters 15 elementsOptions: ElementsOptions = { 16 locale: 'es' 17 }; 18 19 stripeTest: FormGroup; 20 21 constructor( 22 private fb: FormBuilder, 23 private stripeService: StripeService) {} 24 25 ngOnInit() { 26 this.stripeTest = this.fb.group({ 27 name: ['', [Validators.required]] 28 }); 29 this.stripeService.elements(this.elementsOptions) 30 .subscribe(elements => { 31 this.elements = elements; 32 // Only mount the element the first time 33 if (!this.card) { 34 this.card = this.elements.create('card', { 35 style: { 36 base: { 37 iconColor: '#666EE8', 38 color: '#31325F', 39 lineHeight: '40px', 40 fontWeight: 300, 41 fontFamily: '"Helvetica Neue", Helvetica, sans-serif', 42 fontSize: '18px', 43 '::placeholder': { 44 color: '#CFD7E0' 45 } 46 } 47 } 48 }); 49 this.card.mount('#card-element'); 50 } 51 }); 52 } 53 54 buy() { 55 const name = this.stripeTest.get('name').value; 56 this.stripeService 57 .createToken(this.card, { name }) 58 .subscribe(result => { 59 if (result.token) { 60 // Use the token to create a charge or a customer 61 // https://stripe.com/docs/charges 62 console.log(result.token); 63 } else if (result.error) { 64 // Error creating the token 65 console.log(result.error.message); 66 } 67 }); 68 } 69}
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 (card) output, or, by using a ViewChild, the public method getCard()
//stripe.html
1<form novalidate (ngSubmit)="buy()" [formGroup]="stripeTest"> 2 <input type="text" formControlName="name" placeholder="Jane Doe"> 3 <ngx-stripe-card [options]="cardOptions" [elementsOptions]="elementsOptions"></ngx-stripe-card> 4 <button type="submit"> 5 BUY 6 </button> 7</form>
1import { Component, OnInit, ViewChild } from '@angular/core'; 2import { FormGroup, FormBuilder, Validators } from "@angular/forms"; 3 4import { StripeService, StripeCardComponent, ElementOptions, ElementsOptions } from "ngx-stripe-sca"; 5 6@Component({ 7 selector: 'app-stripe-test', 8 templateUrl: 'stripe.html' 9}) 10export class StripeTestComponent implements OnInit { 11 @ViewChild(StripeCardComponent) card: StripeCardComponent; 12 13 cardOptions: ElementOptions = { 14 style: { 15 base: { 16 iconColor: '#666EE8', 17 color: '#31325F', 18 lineHeight: '40px', 19 fontWeight: 300, 20 fontFamily: '"Helvetica Neue", Helvetica, sans-serif', 21 fontSize: '18px', 22 '::placeholder': { 23 color: '#CFD7E0' 24 } 25 } 26 } 27 }; 28 29 elementsOptions: ElementsOptions = { 30 locale: 'es' 31 }; 32 33 stripeTest: FormGroup; 34 35 constructor( 36 private fb: FormBuilder, 37 private stripeService: StripeService) {} 38 39 ngOnInit() { 40 this.stripeTest = this.fb.group({ 41 name: ['', [Validators.required]] 42 }); 43 } 44 45 buy() { 46 const name = this.stripeTest.get('name').value; 47 this.stripeService 48 .createToken(this.card.getCard(), { name }) 49 .subscribe(result => { 50 if (result.token) { 51 // Use the token to create a charge or a customer 52 // https://stripe.com/docs/charges 53 console.log(result.token.id); 54 } else if (result.error) { 55 // Error creating the token 56 console.log(result.error.message); 57 } 58 }); 59 } 60}
If you get your stripe key in a lazy way, or if you need to work with more than one key, you can use this service.
If you don't know the key just call the forRoot method with no params:
1import { BrowserModule } from '@angular/platform-browser'; 2import { NgModule } from '@angular/core'; 3 4import { AppComponent } from './app.component'; 5 6// Import your library 7import { NgxStripeModule } from 'ngx-stripe-sca'; 8 9@NgModule({ 10 declarations: [ 11 AppComponent 12 ], 13 imports: [ 14 BrowserModule, 15 NgxStripeModule.forRoot(), 16 LibraryModule 17 ], 18 providers: [], 19 bootstrap: [AppComponent] 20}) 21export class AppModule { }
Then you can use the factory service to create stripe instances. The stripe instance has the same methods of old StripeService.
1import { Component, OnInit, ViewChild } from '@angular/core'; 2import { FormGroup, FormBuilder, Validators } from "@angular/forms"; 3 4import { StripeInstance, StripeFactoryService } from "ngx-stripe-sca"; 5 6@Component({ 7 selector: 'app-stripe-test', 8 templateUrl: 'stripe.html' 9}) 10export class StripeTestComponent implements OnInit { 11 stripe: StripeInstance; 12 13 constructor( 14 private fb: FormBuilder, 15 private stripeFactory: StripeFactoryService 16 ) {} 17 18 ngOnInit() { 19 this.stripe = this.stripeFactory.create('***your-stripe-publishable key***'); 20 } 21}
If you prefer to work the old StripeService, you can also update the key:
1import { Component, OnInit } from '@angular/core'; 2 3import { StripeService } from 'ngx-stripe-sca'; 4 5@Component({ 6 selector: 'app-stripe-test', 7 templateUrl: 'stripe.html' 8}) 9export class StripeTestComponent implements OnInit { 10 11 constructor( 12 private fb: FormBuilder, 13 private stripeSerivce: StripeService 14 ) {} 15 16 ngOnInit() { 17 this.stripeService.setKey('***your-stripe-publishable key***'); 18 } 19}
For situations where the module has any mistakes or missing methods (I'm aware it has) you can now access both your elements instance and a reference to Stripe:
1import { Component, OnInit } from '@angular/core'; 2 3import { StripeService } from 'ngx-stripe-sca'; 4 5@Component({ 6 selector: 'app-stripe-test', 7 templateUrl: 'stripe.html' 8}) 9export class StripeTestComponent implements OnInit { 10 11 constructor( 12 private fb: FormBuilder, 13 private stripeSerivce: StripeService 14 ) {} 15 16 ngOnInit() { 17 // this is equivalent to window.Stipe, but it make sure the module is loaded 18 const Stripe$: Observable<any> = this.stripeService.getStripeReference(); 19 20 // this is a reference to the stripe elements object 21 const stripe = this.stripeService.getInstance(); 22 } 23}
MIT © Ricardo Sánchez Gregorio
No vulnerabilities found.
No security vulnerabilities found.