@ng-web-apis/payment-request
Version:
This is a library for declarative use of Payment Request API with Angular
172 lines (160 loc) • 7.75 kB
JavaScript
import * as i0 from '@angular/core';
import { Directive, Input, ContentChildren, InjectionToken, inject, Injectable, ElementRef, Output } from '@angular/core';
import { fromEvent, switchMap, from, catchError, of, share, filter } from 'rxjs';
import { WA_WINDOW } from '@ng-web-apis/common';
class WaPaymentItem {
amount;
label;
pending;
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: WaPaymentItem, deps: [], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: WaPaymentItem, isStandalone: true, selector: "[waPaymentItem][paymentAmount][paymentLabel]", inputs: { amount: ["paymentAmount", "amount"], label: ["paymentLabel", "label"], pending: ["paymentPending", "pending"] }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: WaPaymentItem, decorators: [{
type: Directive,
args: [{
standalone: true,
selector: '[waPaymentItem][paymentAmount][paymentLabel]',
}]
}], propDecorators: { amount: [{
type: Input,
args: ['paymentAmount']
}], label: [{
type: Input,
args: ['paymentLabel']
}], pending: [{
type: Input,
args: ['paymentPending']
}] } });
/**
* @deprecated: use {@link WaPaymentItem}
*/
const PaymentItemDirective = WaPaymentItem;
class WaPayment {
total;
id;
modifiers;
shippingOptions;
displayItems;
set paymentItems(items) {
this.displayItems = items.toArray();
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: WaPayment, deps: [], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: WaPayment, isStandalone: true, selector: "[waPayment][paymentTotal]", inputs: { total: ["paymentTotal", "total"], id: ["paymentId", "id"], modifiers: ["paymentModifiers", "modifiers"], shippingOptions: ["paymentShippingOptions", "shippingOptions"] }, queries: [{ propertyName: "paymentItems", predicate: WaPaymentItem }], ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: WaPayment, decorators: [{
type: Directive,
args: [{
standalone: true,
selector: '[waPayment][paymentTotal]',
}]
}], propDecorators: { total: [{
type: Input,
args: ['paymentTotal']
}], id: [{
type: Input,
args: ['paymentId']
}], modifiers: [{
type: Input,
args: ['paymentModifiers']
}], shippingOptions: [{
type: Input,
args: ['paymentShippingOptions']
}], paymentItems: [{
type: ContentChildren,
args: [WaPaymentItem]
}] } });
/**
* @deprecated use {@link WaPayment}
*/
const PaymentDirective = WaPayment;
const WA_PAYMENT_METHODS = new InjectionToken('[WA_PAYMENT_METHODS]', {
factory: () => [{ supportedMethods: 'basic-card' }],
});
/**
* @deprecated: drop in v5.0, use {@link WA_PAYMENT_METHODS}
*/
const PAYMENT_METHODS = WA_PAYMENT_METHODS;
const WA_PAYMENT_OPTIONS = new InjectionToken('[WA_PAYMENT_OPTIONS]', {
factory: () => ({}),
});
/**
* @deprecated: drop in v5.0, use {@link WA_PAYMENT_OPTIONS}
*/
const PAYMENT_OPTIONS = WA_PAYMENT_OPTIONS;
const WA_PAYMENT_REQUEST_SUPPORT = new InjectionToken('[WA_PAYMENT_REQUEST_SUPPORT]', {
factory: () => !!inject(WA_WINDOW).PaymentRequest,
});
/**
* @deprecated: drop in v5.0, use {@link WA_PAYMENT_REQUEST_SUPPORT}
*/
const PAYMENT_REQUEST_SUPPORT = WA_PAYMENT_REQUEST_SUPPORT;
class PaymentRequestService {
supported = inject(PAYMENT_REQUEST_SUPPORT);
paymentMethods = inject(PAYMENT_METHODS);
paymentOptions = inject(PAYMENT_OPTIONS);
async request(details, methods = this.paymentMethods, options = this.paymentOptions) {
if (!this.supported) {
return Promise.reject(new Error('Payment Request is not supported in your browser'));
}
const gateway = new PaymentRequest(methods, details,
// @ts-ignore
options);
return gateway
.canMakePayment()
.then(async (canPay) => canPay
? gateway.show(details)
: Promise.reject(new Error('Payment Request cannot make the payment')));
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: PaymentRequestService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: PaymentRequestService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: PaymentRequestService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}] });
function isError(item) {
return item instanceof Error || item instanceof DOMException;
}
class WaPaymentSubmit {
paymentHost = inject(WaPayment);
paymentRequest = inject(PaymentRequestService);
nativeElement = inject(ElementRef).nativeElement;
methods = inject(PAYMENT_METHODS);
options = inject(PAYMENT_OPTIONS);
waPaymentSubmit;
waPaymentError;
constructor() {
const requests$ = fromEvent(this.nativeElement, 'click').pipe(switchMap(() => from(this.paymentRequest.request({ ...this.paymentHost }, this.methods, this.options)).pipe(catchError((error) => of(error)))), share());
this.waPaymentSubmit = requests$.pipe(filter((response) => !isError(response)));
this.waPaymentError = requests$.pipe(filter(isError));
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: WaPaymentSubmit, deps: [], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: WaPaymentSubmit, isStandalone: true, selector: "[waPaymentSubmit]", outputs: { waPaymentSubmit: "waPaymentSubmit", waPaymentError: "waPaymentError" }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: WaPaymentSubmit, decorators: [{
type: Directive,
args: [{
standalone: true,
selector: '[waPaymentSubmit]',
}]
}], ctorParameters: function () { return []; }, propDecorators: { waPaymentSubmit: [{
type: Output
}], waPaymentError: [{
type: Output
}] } });
/**
* @deprecated: use {@link WaPaymentSubmit}
*/
const PaymentSubmitDirective = WaPaymentSubmit;
const WaPaymentRequest = [WaPayment, WaPaymentItem, WaPaymentSubmit];
/**
* @deprecated: use {@link WaPaymentRequest}
*/
const PaymentRequestModule = WaPaymentRequest;
/**
* Generated bundle index. Do not edit.
*/
export { PAYMENT_METHODS, PAYMENT_OPTIONS, PAYMENT_REQUEST_SUPPORT, PaymentDirective, PaymentItemDirective, PaymentRequestModule, PaymentRequestService, PaymentSubmitDirective, WA_PAYMENT_METHODS, WA_PAYMENT_OPTIONS, WA_PAYMENT_REQUEST_SUPPORT, WaPayment, WaPaymentItem, WaPaymentRequest, WaPaymentSubmit };
//# sourceMappingURL=ng-web-apis-payment-request.mjs.map