UNPKG

@rnw-community/react-native-payments

Version:

Accept Payments with Apple Pay and Android Pay using the Payment Request API.

65 lines 2.63 kB
import { isDefined } from '@rnw-community/shared'; import { PaymentsErrorEnum } from '../../enum/payments-error.enum.js'; import { DOMException } from '../../error/dom.exception.js'; import { PaymentsError } from '../../error/payments.error.js'; import { NativePayments } from '../native-payments/native-payments.js'; /* * https://www.w3.org/TR/payment-request/#paymentresponse-interface */ export class PaymentResponse { constructor( // https://www.w3.org/TR/payment-request/#dom-paymentresponse-requestid requestId, // https://www.w3.org/TR/payment-request/#dom-paymentresponse-methodname methodName, // https://www.w3.org/TR/payment-request/#dom-paymentresponse-details details, shippingOption = null) { this.requestId = requestId; this.methodName = methodName; this.details = details; this.shippingOption = shippingOption; this.completeCalled = false; this.retryCalled = false; } // https://www.w3.org/TR/payment-request/#complete-method async complete(result) { if (this.completeCalled || this.retryCalled) { throw new DOMException(PaymentsErrorEnum.InvalidStateError); } this.completeCalled = true; // TODO: Implement logic https://www.w3.org/TR/payment-request/#complete-method return NativePayments.complete(result); } // https://www.w3.org/TR/payment-request/#dom-paymentresponse-retry async retry(errorFields) { if (this.completeCalled) { throw new DOMException(PaymentsErrorEnum.InvalidStateError); } if (this.retryCalled) { throw new DOMException(PaymentsErrorEnum.InvalidStateError); } const { retry } = NativePayments; if (!isDefined(retry)) { throw new DOMException(PaymentsErrorEnum.NotSupportedError); } this.retryCalled = true; await retry(this.requestId, errorFields ?? {}).catch(() => { throw new PaymentsError(`Failed retrying PaymentRequest`); }); // eslint-disable-next-line no-undefined return undefined; } toJSON() { return { requestId: this.requestId, methodName: this.methodName, details: this.details, shippingAddress: this.details.shippingAddress ?? null, shippingOption: this.shippingOption, payerEmail: this.details.payerEmail ?? null, payerName: this.details.payerName ?? null, payerPhone: this.details.payerPhone ?? null, }; } } //# sourceMappingURL=payment-response.js.map