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);
        }
      });
  }
}

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>

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>

Last updated