Element Components
Element components provide a flexible way to securely collect payment information in your Angular app.
You can mount individual Element components inside of your Elements tree. Note that you can only mount one of each type of Element.
Create Token
In this example the Card Element is used to collection payment information and create a token.
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { StripeService, StripeCardComponent } from 'ngx-stripe';
import {
StripeCardElementOptions,
StripeElementsOptions
} from '@stripe/stripe-js';
@Component({
selector: 'app-create-token',
templateUrl: './create-token.component.html',
})
export class CreateTokenComponent implements OnInit {
@ViewChild(StripeCardComponent) card: StripeCardComponent;
cardOptions: StripeCardElementOptions = {
style: {
base: {
iconColor: '#666EE8',
color: '#31325F',
fontWeight: '300',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSize: '18px',
'::placeholder': {
color: '#CFD7E0'
}
}
}
};
elementsOptions: StripeElementsOptions = {
locale: 'en'
};
stripeTest: FormGroup;
constructor(private fb: FormBuilder, private stripeService: StripeService) {}
ngOnInit(): void {
this.stripeTest = this.fb.group({
name: ['', [Validators.required]]
});
}
createToken(): void {
const name = this.stripeTest.get('name').value;
this.stripeService
.createToken(this.card.element, { name })
.subscribe((result) => {
if (result.token) {
// Use the token
console.log(result.token.id);
} else if (result.error) {
// Error creating the token
console.log(result.error.message);
}
});
}
}<h2>Create Token Example</h2>
<ngx-stripe-card
[options]="cardOptions"
[elementsOptions]="elementsOptions"
></ngx-stripe-card>
<button type="submit" (click)="createToken()">
CREATE TOKEN
</button>
The full list of components available:
Component
Selector
Card Element
ngx-stripe-card
Card Number Element
ngx-stripe-card-number
Card Expiry Element
ngx-stripe-card-expiry
Card CVC Element
ngx-stripe-card-cvc
Payment Request Button Element
ngx-stripe-payment-request-button
Au Bank Account Element
ngx-stripe-au-bank-account
Iban Element
ngx-stripe-iban
Ideal Bank Element
ngx-stripe-ideal-bank
Fpx Bank Element
ngx-stripe-fpx-bank
Card Group Directive
If you want to use the card number, expiry and cvc as separated elements you need to put them inside the card group directive.
This is necessary to ensure the three elements shared the same stripe elements objects, otherwise it won't work. As you can see in the example element options is not available for any of the three child components, but only for the group directive.
Here is an example using the Card Group as a Directive:
<div
ngxStripeCardGroup
[formGroup]="stripeTest"
[elementsOptions]="elementsOptions"
>
<input formControlName="name" />
<input formControlName="amount" />
<ngx-stripe-card-number [options]="cardOptions"></ngx-stripe-card-number>
<ngx-stripe-card-expiry [options]="cardOptions"></ngx-stripe-card-expiry>
<ngx-stripe-card-cvc [options]="cardOptions"></ngx-stripe-card-cvc>
<button type="submit" (click)="pay()">
PAY
</button>
</div>import { Component, OnInit, ViewChild, ɵConsole } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { StripeService, StripeCardNumberComponent } from 'ngx-stripe';
import {
StripeCardElementOptions,
StripeElementsOptions,
PaymentIntent,
} from '@stripe/stripe-js';
import { environment as env } from '../../environments/environment';
@Component({
selector: 'app-simple-payment-intent',
templateUrl: './simple-payment-intent.component.html',
styleUrls: ['./simple-payment-intent.component.css'],
})
export class SimplePaymentIntentComponent implements OnInit {
@ViewChild(StripeCardNumberComponent) card: StripeCardNumberComponent;
cardOptions: StripeCardElementOptions = {
style: {
base: {
iconColor: '#666EE8',
color: '#31325F',
fontWeight: '300',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSize: '18px',
'::placeholder': {
color: '#CFD7E0',
},
},
},
};
elementsOptions: StripeElementsOptions = {
locale: 'es',
};
stripeTest: FormGroup;
constructor(
private http: HttpClient,
private fb: FormBuilder,
private stripeService: StripeService
) {}
ngOnInit(): void {
this.stripeTest = this.fb.group({
name: ['Angular v10', [Validators.required]],
amount: [1001, [Validators.required, Validators.pattern(/\d+/)]],
});
}
pay(): void {
if (this.stripeTest.valid) {
this.createPaymentIntent(this.stripeTest.get('amount').value)
.pipe(
switchMap((pi) =>
this.stripeService.confirmCardPayment(pi.client_secret, {
payment_method: {
card: this.card.element,
billing_details: {
name: this.stripeTest.get('name').value,
},
},
})
)
)
.subscribe((result) => {
if (result.error) {
// Show error to your customer (e.g., insufficient funds)
console.log(result.error.message);
} else {
// The payment has been processed!
if (result.paymentIntent.status === 'succeeded') {
// Show a success message to your customer
}
}
});
} else {
console.log(this.stripeTest);
}
}
createPaymentIntent(amount: number): Observable<PaymentIntent> {
return this.http.post<PaymentIntent>(
`${env.apiUrl}/create-payment-intent`,
{ amount }
);
}
}
We also make available the component selector, in case you prefer to work that way, here is an example using the Card Group as a Component.
<ngx-stripe-card-group
[formGroup]="stripeTest"
[elementsOptions]="elementsOptions"
>
<input formControlName="name" />
<input formControlName="amount" />
<ngx-stripe-card-number [options]="cardOptions"></ngx-stripe-card-number>
<ngx-stripe-card-expiry [options]="cardOptions"></ngx-stripe-card-expiry>
<ngx-stripe-card-cvc [options]="cardOptions"></ngx-stripe-card-cvc>
<button type="submit" (click)="pay()">
PAY
</button>
</ngx-stripe-card-group>import { Component, OnInit, ViewChild, ɵConsole } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { StripeService, StripeCardNumberComponent } from 'ngx-stripe';
import {
StripeCardElementOptions,
StripeElementsOptions,
PaymentIntent,
} from '@stripe/stripe-js';
import { environment as env } from '../../environments/environment';
@Component({
selector: 'app-simple-payment-intent',
templateUrl: './simple-payment-intent.component.html',
styleUrls: ['./simple-payment-intent.component.css'],
})
export class SimplePaymentIntentComponent implements OnInit {
@ViewChild(StripeCardNumberComponent) card: StripeCardNumberComponent;
cardOptions: StripeCardElementOptions = {
style: {
base: {
iconColor: '#666EE8',
color: '#31325F',
fontWeight: '300',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSize: '18px',
'::placeholder': {
color: '#CFD7E0',
},
},
},
};
elementsOptions: StripeElementsOptions = {
locale: 'es',
};
stripeTest: FormGroup;
constructor(
private http: HttpClient,
private fb: FormBuilder,
private stripeService: StripeService
) {}
ngOnInit(): void {
this.stripeTest = this.fb.group({
name: ['Angular v10', [Validators.required]],
amount: [1001, [Validators.required, Validators.pattern(/\d+/)]],
});
}
pay(): void {
if (this.stripeTest.valid) {
this.createPaymentIntent(this.stripeTest.get('amount').value)
.pipe(
switchMap((pi) =>
this.stripeService.confirmCardPayment(pi.client_secret, {
payment_method: {
card: this.card.element,
billing_details: {
name: this.stripeTest.get('name').value,
},
},
})
)
)
.subscribe((result) => {
if (result.error) {
// Show error to your customer (e.g., insufficient funds)
console.log(result.error.message);
} else {
// The payment has been processed!
if (result.paymentIntent.status === 'succeeded') {
// Show a success message to your customer
}
}
});
} else {
console.log(this.stripeTest);
}
}
createPaymentIntent(amount: number): Observable<PaymentIntent> {
return this.http.post<PaymentIntent>(
`${env.apiUrl}/create-payment-intent`,
{ amount }
);
}
}Last updated
Was this helpful?