包详细信息

ngx-stripe

richnologies278.6kMIT20.7.0

Collect Payments with Stripe: The Angular Way

angular, stripe, payments

自述文件

Collect Payments with Stripe: The Angular Way

drawing version license

Ngx Stripe is a comprehensive library designed for seamless integration of Stripe Elements and payment processing capabilities into Angular applications. Leveraging the powerful features of StripeJS, Ngx Stripe simplifies building robust, secure, and scalable payment solutions.

Use Elements with any Stripe product to collect online payments. For the right integration path for your business, explore the Stripe Docs.

Learn how to use ngx-stripe on the new docs site 🤓

Notice (Jun 18th 2024)

We would like to inform you that we have updated the library to support Stripe V4 from version 18.1.0 onwards. This is a major version upgrade, but it's not a significant change and should not cause any issues.

We are keeping the library versioning in line with Angular majors, which upgrade more often than Stripe, and as a result, we are deviating from the semver standard. We believe this approach will provide a better experience in the long run.

We would like to apologize for any inconvenience this may cause you.

Features

  • Angular Components for Stripe Elements: Ngx Stripe provides a set of Angular components, each corresponding to a specific Stripe Web Element. These components are designed to simplify the integration of Stripe's UI elements, ensuring a smooth and consistent user experience.
  • Seamless Integration with StripeJS: Aligning closely with StripeJS, Ngx Stripe ensures that you have access to the latest features and updates from Stripe, directly within your Angular application.
  • Lazy Loading of StripeJS: Enhance your application's performance by lazy loading the StripeJS JavaScript. This feature ensures that the StripeJS library is loaded only when needed, optimizing loading times and improving the overall user experience.
  • Customizable and Flexible: Customize the look and feel of your payment forms to match your application's design. Ngx Stripe components are highly flexible, allowing for extensive customization and styling.
  • Strongly Typed for Angular Development: Benefit from TypeScript in your payment integration. Ngx Stripe is strongly typed, making

Installation

Active Versions

To install the last active version:

$ npm install ngx-stripe @stripe/stripe-js

To install a specific version for an older Angular major, use the LTS npm tags or check the table below to pick the right version. For example, for v8:

$ npm install ngx-stripe@v14-lts @stripe/stripe-js

Choose the version corresponding to your Angular version:

Angular ngx-stripe
19 19.x+
18 18.x+
17 17.x+
16 16.x+
15 15.x+
14 14.x+
13 13.x+
12 12.x+
11 11.x+
10 10.x+
9 v9-lts / 9.4.0
8 v8-lts / 8.2.0

Using the library

Most of the documentation has been moved to the new docs site. Only a very basic example is left here:

We start by adding the providers to our app:

import { provideNgxStripe } from 'ngx-stripe';

bootstrapApplication(AppComponent, {
  providers: [
    // ... rest of your providers
    provideNgxStripe()
  ]
});

Or if you're still using modules:

Import the NgxStripeModule into your application:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// Import the library
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// Import the library
import { NgxStripeModule } from 'ngx-stripe';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    // ... rest of your imports
    NgxStripeModule.forRoot(),
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

Payment Element Component

Once the module has been imported, you can collect credit card details using the ngx-stripe-card component.

Then you can use the Stripe Service, which is basically an Observable wrapper around the stripejs object, to use that information. In this example, we use it to create a token, but it can be used to confirm a Payment Intent, Setup Intent, etc...

Please check the docs site to see a complete set of Stripe Element Components available and the full API of the Stripe Service.

<div [formGroup]="paymentElementForm">
  <mat-form-field appearance="fill">
    <input matInput placeholder="name" formControlName="name" />
  </mat-form-field>
  <mat-form-field appearance="fill">
    <input matInput placeholder="Email" type="email" formControlName="email" />
  </mat-form-field>
  <mat-form-field appearance="fill">
    <input matInput placeholder="Address" formControlName="address" />
  </mat-form-field>
  <mat-form-field appearance="fill">
    <input matInput placeholder="ZIP Code" formControlName="zipcode" />
  </mat-form-field>
  <mat-form-field appearance="fill">
    <input matInput placeholder="city" formControlName="city" />
  </mat-form-field>
  @if (elementsOptions.clientSecret) {
    <ngx-stripe-elements
      [stripe]="stripe"
      [elementsOptions]="elementsOptions"
    >
      <ngx-stripe-payment [options]="paymentElementOptions" />
    </ngx-stripe-elements>
  }
  <button (click)="pay()">PAY</button>
</div>
import { Component, inject, signal, ViewChild } from '@angular/core';
import { UntypedFormBuilder, Validators } from '@angular/forms';

import { MatInputModule } from '@angular/material/input';

import {
  injectStripe,
  StripePaymentElementComponent
} from 'ngx-stripe';
import {
  StripeElementsOptions, 
  StripePaymentElementOptions
} from '@stripe/stripe-js';

@Component({
  selector: 'ngstr-checkout-form',
  templateUrl: './payment-element.component.html',
  standalone: true,
  imports: [
    ReactiveFormsModule,
    MatInputModule,
    StripePaymentElementComponent
  ]
})
export class CheckoutFormComponent {
  @ViewChild(StripePaymentElementComponent)
  paymentElement!: StripePaymentElementComponent;

  private readonly fb = inject(UntypedFormBuilder);

  paymentElementForm = this.fb.group({
    name: ['John Doe', [Validators.required]],
    email: ['support@ngx-stripe.dev', [Validators.required]],
    address: [''],
    zipcode: [''],
    city: [''],
    amount: [2500, [Validators.required, Validators.pattern(/\d+/)]]
  });

  elementsOptions: StripeElementsOptions = {
    locale: 'en',
    client: '{{YOUR_CLIENT_SECRET}}'
    appearance: {
      theme: 'flat'
    }
  };

  paymentElementOptions: StripePaymentElementOptions = {
    layout: {
      type: 'tabs',
      defaultCollapsed: false,
      radios: false,
      spacedAccordionItems: false
    }
  };

  // Replace with your own public key
  stripe = injectStripe({{YOUR_PUBLIC_KEY}});
  paying = signal(false);

  pay() {
    if (this.paying() || this.paymentElementForm.invalid) return;
    this.paying.set(true);

    const {
      name,
      email,
      address,
      zipcode,
      city
    } = this.checkoutForm.getRawValue();

    this.stripe
      .confirmPayment({
        elements: this.paymentElement.elements,
        confirmParams: {
          payment_method_data: {
            billing_details: {
              name: name as string,
              email: email as string,
              address: {
                line1: address as string,
                postal_code: zipcode as string,
                city: city as string
              }
            }
          }
        },
        redirect: 'if_required'
      })
      .subscribe(result => {
        this.paying.set(false);
        if (result.error) {
          // Show error to your customer (e.g., insufficient funds)
          alert({ success: false, error: result.error.message });
        } else {
          // The payment has been processed!
          if (result.paymentIntent.status === 'succeeded') {
            // Show a success message to your customer
            alert({ success: true });
          }
        }
      });
  }
}

Support us

ngx-stripe is an MIT-licensed open source project. You can now become a sponsor with GitHub Sponsors.

We've been bringing ngx-stripe to the world for over 6 years and are excited to be able to start dedicating some real resources to the project.

Your sponsorship helps us keep a team of maintainers actively working to improve ngx-stripe and ensure it stays up-to-date with the latest Stripe changes. If you're using ngx-stripe in a commercial capacity and have the ability to start a sponsorship, we'd greatly appreciate the contribution.

Principal Sponsors

License

MIT © Ricardo Sánchez Gregorio

更新日志

Changelog

20.7.0 - 2025-05-28

  • Add support for StripeJS V6 - basil

20.6.1 - 2025-05-28

  • Fix importing StripeJS

20.6.0 - 2025-05-28

  • Add support for StripeJS V6 - acacia

20.5.0 - 2025-05-28

  • Add support for Angular v20

19.7.0 - 2025-05-28

  • Add support for StripeJS V7 - basil

19.6.0 - 2025-05-28

  • Add support for StripeJS V6 - acacia

19.5.0 - 2025-05-28

  • #231 Fix an error with Hydration warnings with SSR
  • Add support for MultibancoPayment
  • Add support for TwintPayment

19.0.0 - 2024-11-27

  • Add support for Angular v19
  • Add support for StripeJS v5

18.1.0 - 2024-06-18

  • Add support for StripeJS v4
  • Add support for MobilePay
  • Add support for ConfirmationTokens
  • Update ConfirmSetup params type

18.0.0 - 2024-05-24

  • Add support for Angular v18

17.2.0 - 2024-04-10

  • Remove Card Element Types

17.1.1 - 2024-02-18

  • Fix NPM dependencies

17.1.0 - 2024-02-13

  • Add support for StripeJS v3

17.0.1 - 2023-11-18

  • Multiple updates to the docs site
  • Update the README file
  • to boldly go with your payments where no one has gone before

17.0.0 - 2023-11-08

  • Add support for Angular v17

16.3.1 - 2023-11-08

  • #231 Fix an error with Payment Request Button Element

16.3.0 - 2023-11-07

  • Update stripe peer dependency to 2.1.11. This is a major change, but the breaking change is minimum so we believe is better to stay aligned with Angular instead of bump a new major.
  • Add support for Express Checkout Element

16.2.0 - 2023-07-17

  • Adding provideNgxStripe method for Standalone Applications

16.1.2 - 2023-06-30

  • Update stripe peer dependency to 1.54.1

16.1.1 - 2023-06-02

  • Update stripe peer dependency to 1.54.0

16.1.0 - 2023-05-27

  • Payment Method Messaging Element support
  • Add support for Blik Payemnts
  • Add support for Cashapp Payments
  • Add new types for ConfirmPaymet
  • Add new types for createPaymentMethod
  • Add new types for confirmSetup
  • Add support for handleNextAction

16.0.0 - 2023-05-04

  • Add support for Angular v16

15.6.0 - 2023-03-31

  • Improve typing with the new StripeElementsOptions
  • Add support for StripeCardElementUpdateOptions new typing
  • Fix small issue with one docs example
  • Update peer dependencies on @stripe/stripe-js

15.5.1 - 2023-03-14

  • Removing use of Observable lastValueFrom to bring back support for RxJS@6

15.4.1 - 2023-02-05

  • Cleaner implementation for events in NgxStripeCardGroup

15.4.0 - 2023-02-05

  • Add events to NgxStripeCardGroup directive

15.3.0 - 2023-02-04

  • Add support for Issuing Elements

15.2.0 - 2023-01-18

  • Add support for Standalone Components

15.1.1 - 2023-01-17

  • Fix type error for paymentRequest method

15.1.0 - 2023-01-17

  • Add injectStripe build on the new Angular inject function

15.0.0 - 2023-01-16

  • Add support for Angular v15

14.3.0 - 2023-01-16

  • Add Elements Directive to share elements group between components
  • Address Element support
  • Affirm Message Element support
  • Afterpay Clearpay Message Element support
  • Eps Bank Element support
  • Link Authentication Element support
  • P24 Bank Element support
  • Better typings for Intents results
  • Pix Payments support
  • Radar session
  • Orders support
  • Boleto Payments support
  • Financial Connections support
  • Ephemeral Key Nonce support

14.2.0 - 2022-09-28

  • Fix peer dependencies

14.1.0 - 2022-06-24

  • Add support for fetchUpdates for Payment Element
  • Add support for loaderror for Payment Element
  • Add abort and isShowing for Payment Request Button

14.0.0 - 2022-06-24

  • Support for Angular 14

13.2.1 - 2022-05-17

  • Fixing mess with versions :(

13.2.0 - 2022-05-17

  • #103 Fix a little bug related to Payment Request Button

13.1.0 - 2022-04-27

  • Update whole project to use Angular v13
  • Add support for confirmSofort* options
  • Add support for confirmKonbiniPayments
  • Add support for US Bank Accounts

13.0.0 - 2021-11-11

  • Update peer dependencies for Angular v13

12.7.2 - 2021-11-05

  • Undo peer dependencies workaround

12.7.1 - 2021-11-03

  • Fix peer dependencies

12.7.0 - 2021-10-22

  • Add support for Payment Element
  • Add support for Affirm Payment
  • Add support for Prompt Pay Payment
  • Add support for Pay Now Payment

12.6.0 - 2021-10-10

  • Add support for Loading Template

12.5.0 - 2021-09-24

  • Add support for Boleto Payments

12.4.0 - 2021-08-09

  • Remove temporary workaround for Stripe Identity
  • Update types for Payment Request Button
  • Add support for PayPal Payments (requires beta access)

12.3.1 - 2021-06-30

  • Add Stripe Verified Partner badge

12.3.0 - 2021-06-30

  • Add support for Stripe Identity

12.2.1 - 2021-06-25

  • Update README.md with new Logo

12.2.0 - 2021-06-19

  • Add support for Acss Debit Payments
  • Add support for Afterpay Clearpay Payments
  • Add support for Alipay Payments
  • Add support for GrabPay Payments
  • Add support for Klarna Payments (requires beta access)
  • Add support for Sofort Payments
  • Add support for WeChat Pay Payments (requires beta access)
  • Add support for Verify Microdeposits Payments
  • Add support for Bacs Debit Setup
  • Add support for Bancontact Setup
  • Add support for Ideal Bank Setup
  • Add support for Sofort Setup
  • Add support for Verify Microdeposits Setup

12.1.1 - 2021-06-19

  • Add support for OXXO Payments

12.0.0 - 2021-05-15

  • Add support for Angular v12

10.1.2 - 2020-08-06

  • #103 Fix a little bug related to Payment Request Button
  • Adds a new Output notavailable to Payment Request Button

10.1.1 - 2020-07-17

  • #101 Fix ngx-stripe-card now requires elementsOptions and options to render?

10.1.0 - 2020-07-16

  • #100 Fix confirmCardSetup returns 400 error, if separated elements are used

10.0.4 - 2020-07-14

  • #99 Fix No provider for StripeElementsService

10.0.0 - 2020-07-11

  • Release new major version of the library. More info about migration here

9.0.3 - 2020-06-28

  • #89 Fix ModuleWithProviders generics issue

7.4.3 - 2019-06-15

  • #73 Fixed incorrect argument types on handleCardPayment().

7.4.0 - 2019-05-27

  • #66 Fixed missing methods and interface declarations for PaymentIntents and PaymentMethods