Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@ng-web-apis/payment-request

taiga-family192Apache-2.04.12.0TypeScript support: included

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

angular, ng, payment, payment request api, apple pay, google pay, pwa, progressive web app

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 Example {
  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 Example {
  // ...
}
  • 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 Example {
  // ...
}

Browser support

IE / Edge Firefox Chrome Safari
12+ Disabled by default 61+ 11.1+

See also

All @ng-web-apis for your apps

changelog

4.11.1 (2024-10-14)

🐞 Bug Fixes

  • screen-orientation: Viewport fix initial value (#952) (ab6d632)

4.11.0 (2024-10-14)

🚀 Features

  • screen-orientation: Viewport add new service (#951) (6781bf6)

4.10.2 (2024-10-11)

🐞 Bug Fixes

  • platform: invalid regexp for ios (#950) (34f3458)

4.10.1 (2024-10-10)

🐞 Bug Fixes

  • intersection-observer: fix wrong attributes aliasing (#948) (22a8cde)

4.10.0 (2024-10-10)

🐞 Bug Fixes

  • platform: correct behavior for isApple util (#946) (9a36f07)

4.9.0 (2024-10-10)

🚀 Features

  • platform: add WA_IS_TOUCH, WA_IS_WEBKIT, WA_IS_ANDROID, WA_IS_IOS, WA_IS_MOBILE (#944) (70543f1)

4.8.0 (2024-10-08)

🐞 Bug Fixes

  • typo platform package name (#943) (29b8dc7)

4.7.0 (2024-10-08)

🚀 Features

🐞 Bug Fixes

  • deps: update dependency @types/node to >=22.7.5 (#941) (35b605f)

Changelog

All notable changes to this project will be documented in this file. See conventional commits guidelines.

4.6.5 (2024-09-29)

4.6.4 (2024-09-25)

🐞 Bug Fixes

  • deps: update dependency @types/node to >=22.7.0 (#889) (afaa064)

4.6.3 (2024-09-23)

4.6.2 (2024-09-23)

4.6.1 (2024-09-23)

4.6.0 (2024-09-16)

4.5.0 (2024-09-13)

🚀 Features

🐞 Bug Fixes

4.4.0 (2024-09-12)

🚀 Features

🐞 Bug Fixes

  • coverage bandges (3916016)
  • compat for legacy modules (#559) (b720c40)
  • update peerDependencies (#556) (06f02d9)
  • speech, view-transition: add peer dependencies on appropriate types (#119) (a1ffd76)
  • view transition deploy (#118) (12efc9b)
  • universal: add secondary entry point for mocks (#112) (9e01f2d)

2.1.0 (2022-11-15)

🚀 Features

2.0.1 (2022-07-15)

🐞 Bug Fixes

  • page_visibility: share replay of page visibility (#45) (09b0956)

2.0.0 (2022-01-27)

🚀 Features

  • update to Angular 12 and Ivy (#31) (135f43c)

1.13.0 (2022-01-27)

🚀 Features

  • media_devices: added new token (#33) (2c05446)

1.12.1 (2021-10-15)

🐞 Bug Fixes

  • types: compatibility issues with modern TypeScript (#26) (1c43f38)

1.12.0 (2021-07-12)

🚀 Features

1.11.0 (2021-06-29)

🚀 Features

  • history: add new token (#23) (4ca7c33)
  • caches: add new token (#19) (b49a270)
  • page-visibility: document.hidden -> document.visibilityState (#18) (244ede0)

1.9.0 (2020-10-28)

🚀 Features

  • speech_recognition: add new token (#13) (ad0f627)

1.8.1 (2020-10-26)

🐞 Bug Fixes

1.8.0 (2020-10-26)

🚀 Features

1.7.0 (2020-09-02)

🚀 Features

  • network_information: add new token (492018e)

1.6.1 (2020-08-24)

🐞 Bug Fixes

1.6.0 (2020-08-10)

🚀 Features

  • session_storage: add new token (#10) (a4c39d9)

1.5.0 (2020-07-24)

🚀 Features

1.4.1 (2020-06-29)

🐞 Bug Fixes

1.4.0 (2020-04-30)

🚀 Features

  • css: inject mock object when CSS is not available, i.e. in IE or SSR (acea44c)

1.3.0 (2020-04-29)

🚀 Features

  • tokens: LOCAL_STORAGE, LOCATION, USER_AGENT add new tokens (07c7bda)

1.2.0 (2020-04-29)

🚀 Features

1.1.0 (2020-02-28)

🚀 Features

1.0.1 (2020-01-21)

🐞 Bug Fixes

1.0.0 (2020-01-21)