npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nomadreservations/ngx-stripe

v1.2.3

Published

@angular wrapper for StripeJS

Downloads

250

Readme

Table of Contents

@nomadreservations/ngx-stripe

version license

@angular wrapper for StripeJS based on module by Ricardo Sánchez Gregorio at https://github.com/richnologies/ngx-stripe

Note: This version is designed to work with angular v8+ only

Features

  • Stripe Service
  • Lazy script loading
  • AoT compliant
  • SSR fallback

Installation

To install this library, run:

$ npm install @nomadreservations/ngx-stripe --save
or
$ yarn add @nomadreservations/ngx-stripe

Using the library

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

StripeCardComponent

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

RequestPaymentButton

A payment request button (e.g. apple pay, google chrome payment, etc.) can be enabled with the ngx-payment-request component. All of the options Request Payment Api options are exposed via the three options sections in the component.

// request-button.component.html

<div class="container">
  <mat-card>
    Pay: <mat-slider min="1" max="100" step="10" thumbLabel [value]="pay" (valueChange)="updatePay($event)"></mat-slider>
    <ngx-payment-request 
      [options]="requestOptions" 
      [elementsOptions]="elementsOptions" 
      [styles]="styles" 
      (shippingAddressChange)="updateShippingAddress($event)"
      (shippingOptionChange)="updateShippingOption($event)"
      (change)="requestUpdated($event)">
    </ngx-payment-request>

  </mat-card>

// request-button.compnent.ts

@Component({
  selector: 'app-test',
  templateUrl: 'request-button.component.html'
})
export class RequestButtonComponent implements OnInit {
  @Input set stripeKey(key) {
    this._stripe.changeKey(key);
  }

  public requestOptions: RequestElementOptions = {
    country: 'US',
    currency: 'usd',
    requestPayerName: true,
    requestPayerEmail: true,
    requestPayerPhone: true,
    requestShipping: true,
    total: {
      amount: 10000,
      label: 'Donate to the things'
    }
  };

  public styles: PaymentRequestButtonStyle = {
    type: 'donate',
    theme: 'light',
    height: '64px'
  };

  public elementsOptions: ElementsOptions = {
    locale: 'en'
  };

  public pay = 100;

  constructor(private _stripe: StripeService) { }
  
  public updatePay(amount: number) {
    this.pay = amount;
    this.requestOptions = {
      ...this.requestOptions,
      total: {
        ...this.requestOptions.total,
        amount: amount * 100
      }
    };
  }

  public async requestUpdated(result) {
    const response = await fetch('/charges', {
      method: 'POST',
      body: JSON.stringify({token: result.token.id}),
      headers: {'content-type': 'application/json'},
    });

    if (response.ok) {
      result.complete('success');
    } else {
      result.complete('fail');
    }    
  }

  public updateShippingAddress(result) {
    result.updateWith({
      status: 'success',
      shippingOptions: [
        {
          id: 'free-shipping',
          label: 'Free shipping',
          detail: 'Arrives in 5 to 7 days',
          amount: 0
        },
        {
          id: 'express',
          label: 'Express shipping',
          detail: 'Arrives in 1 to 2 days',
          amount: 1000
        }
      ]
    });
  }

  public updateShippingOption(result) {
    result.updateWith({
      status: 'success'
    });
  }
}

Dynamic Stripe Keys

Given the way an AOT angular build processes provided values if you need to provide a stripe key at runtime you'll want to forgo the forRoot initialization and instead add a factory and provider


  export function NgxStripeFactory(): string {
    return dynamicLocation.stripePkKey;
  }
  
  @NgModule({
    ...
    imports: []
      ...
      NgxStripeModule.forRoot('whatever key you think should be default'),
      ...
    ]
    providers: [
      {
        provide: STRIPE_PUBLISHABLE_KEY,
        useFactory: NgxStripeFactory
      }
    ]
  })

PaymentIntents

You can use the payment intents api with this service just like with stripe.js the following are exposed via the StripeService

  handleCardSetup(clientSecret: string, el: Element, cardSetupOptions?: SetupIntentData): Promise<SetupIntentResult>;
  handleCardAction(clientSecret: string);
  handleCardPayment(clientSecret: string, el: Element, data: CardPaymentData);
  confirmPaymentIntent(clientSecret: string, el: Element, data: ConfirmIntentData);
  retrievePaymentIntent(clientSecret: string);
  confirmSetupIntent(clientSecret: string, el: Element, data: ConfirmSetupIntentData);
  retrieveSetupIntent(clientSecret: string);

SCA/3D Secure payments

3D Secure payments are enabled by following a 3D Secure Flow using the payment intents api's. You will need to setup a proper next action in the payment intent based off of the 3D Secure response from the customers bank/institution.

Example: //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)="payNow()" [disabled]="!complete">Pay</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;
    if(this.complete) {
      this._stripe.
    }
    this.error = undefined;
  }

  keyUpdated() {
    this._stripe.changeKey(this.stripeKey);
  }

  async payNow() {
    const response = await fetch('/paymentIntent', {
      method: 'POST',
      body: JSON.stringify({amount: 10000, currency: 'usd'}),
      headers: {'content-type': 'application/json'},
    });

    const body = await response.json();
    this._stripe.handleCardPayment(body.client_secret, this.element, { payment_method_data: {
      billing_details: {name: 'Bob Smith'}
    }}).subscribe(result => {
      if(result.error) {
        console.error('got stripe error', result.error);
      } else {
        console.log('payment succeeded');
      }
    });
  }
}

Please note you can also manually handle this flow using the paymentIntent methods however that is not the default path and you will have to implement it yourself (see more here)

Testing

The following command runs unit & integration tests that are in the tests folder, and unit tests that are in src folder:

yarn test

Building

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

Publishing

yarn release:patch
or
yarn release:minor
or
yarn release:major

Documentation

To generate the documentation, this starter uses compodoc:

yarn compodoc
yarn compodoc:serve

License

MIT