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

@ng-web-apis/payment-request

v3.0.6

Published

This is a library for declarative use of Payment Request API with Angular

Downloads

88

Readme

ng-web-apis logo Payment Request API for Angular

npm version npm bundle size codecov

Angular does not have any abstractions over Payment Request API. This library provides you two ways to use this API with Angular of 6+ version.

Install

If you do not have @ng-web-apis/common:

npm i @ng-web-apis/common

Now install the package:

npm i @ng-web-apis/payment-request

How to use

As an Angular service:

import {PaymentRequestService} from '@ng-web-apis/payment-request';

...
constructor(private readonly paymentRequest: PaymentRequestService) {}

pay(details: PaymentDetailsInit) {
    this.paymentRequest.request(details).then(
        response => {
            response.complete();
        },
        error => {},
    );
}

As a set of directives:

<div
  waPayment
  [paymentTotal]="total"
>
  <div
    *ngFor="let item of items"
    waPaymentItem
    [paymentLabel]="item.label"
    [paymentAmount]="item.amount"
  >
    {{item.label}} ({{item.amount}})
  </div>
  <button
    (waPaymentSubmit)="onPaymentSubmit($event)"
    (waPaymentError)="onPaymentError($event)"
  >
    Buy
  </button>
</div>

Do not forget to import PaymentRequestModule:

import {PaymentRequestModule} from '@ng-web-apis/payment-request';
...
@NgModule({
    ...
    imports: [
        ...
        PaymentRequestModule
    ]
})
export class YourModule {}

As a good example of usage you can take a look at a project live demo on CodeSandbox

waPayment

waPayment directive defines a scope for a new payment and needs PaymentItem object with information about a label and a total sum of the payment

How to use:

<any-element
  waPayment
  [paymentTotal]="total"
>
  ...
</any-element>

It implements PaymentDetailsInit

Required inputs:

  • paymentTotal is a information about a label and a total sum of the payment as PaymentItem

Additional inputs:

waPaymentItem

Each item of the payment is a waPaymentItem directive. It is a declarative PaymentItem for your Payment

How to use:

<any-element
  waPayment
  [paymentTotal]="total"
>
  <any-element
    *ngFor="let item of items"
    waPaymentItem
    [paymentLabel]="item.label"
    [paymentAmount]="item.amount"
  >
    {{item.label}}
  </any-element>
</any-element>

It implements PaymentItem

Required inputs:

  • paymentAmount is a price of payment item in modal as PaymentCurrencyAmount

  • paymentLabel is a title of payment item in modal as string

Additional inputs:

  • paymentPending is native property for PaymentItem as boolean

waPaymentSubmit

This directive starts a Payment Request modal in your browser that returns PaymentResponse or an error

How to use:

<any-element
  waPayment
  [paymentTotal]="total"
>
  ...
  <button
    (waPaymentSubmit)="onPayment($event)"
    (waPaymentError)="onPaymentError($event)"
  >
    Buy
  </button>
</any-element>

Outputs:

  • waPaymentSubmit emits PaymentResponse object to handle a payment request result

  • waPaymentError emits an Error or DOMException with information about user's problem that did not allow payment to proceed

Tokens

The library also provides some tokens to simplify working with Payment Request API:

  • PAYMENT_REQUEST_SUPPORT returns true if user's browser supports Payment Request API
export class YourComponent {
    constructor(
        @Inject(PAYMENT_REQUEST_SUPPORT) private readonly canRequest: boolean
    ) {}
    ...
  • You can provide PAYMENT_METHODS as an array of supported API methods. It uses [{supportedMethods: 'basic-card'}] by default
@Component({
    ...
    providers: [
        {
            provide: [PAYMENT_METHODS],
            useValue: [
                // a sample with Google Pay from https://developers.google.com/pay/api/web/guides/paymentrequest/tutorial?hl=en
                {supportedMethods: 'https://google.com/pay', data: googlePaymentDataRequest},
                {supportedMethods: 'basic-card'}
            ]
        }
    ]
})
export class YourComponentThatMakesPaymentRequests {
    ...
}
  • You can provide PAYMENT_OPTIONS as an object with info that you need about a payer. It uses {} by default
@Component({
    ...
    providers: [
        {
            provide: [PAYMENT_OPTIONS],
            useValue: {
                shippingType: 'express',
                requestPayerName: true,
                requestShipping: true,
                requestPayerEmail: true,
            }
        }
    ]
})
export class YourComponentThatMakesPaymentRequests {
    ...
}

Browser support

| | | | | | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | | 12+ | Disabled by default | 61+ | 11.1+ |

See also

All @ng-web-apis for your apps