@rnw-community/react-native-payments
Version:
Accept Payments with Apple Pay and Android Pay using the Payment Request API.
509 lines • 28.7 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentRequest = void 0;
/* eslint-disable max-lines */
const react_native_1 = require("react-native");
const react_native_uuid_1 = __importDefault(require("react-native-uuid"));
const shared_1 = require("@rnw-community/shared");
const android_payment_method_tokenization_type_enum_js_1 = require("../../@standard/android/enum/android-payment-method-tokenization-type.enum.js");
const android_payment_data_request_js_1 = require("../../@standard/android/request/android-payment-data-request.js");
const android_payment_method_js_1 = require("../../@standard/android/request/android-payment-method.js");
const android_transaction_info_js_1 = require("../../@standard/android/request/android-transaction-info.js");
const ios_pk_contact_field_enum_js_1 = require("../../@standard/ios/enum/ios-pk-contact-field.enum.js");
const ios_pk_merchant_capability_enum_js_1 = require("../../@standard/ios/enum/ios-pk-merchant-capability.enum.js");
const ios_pk_payment_networks_enum_js_1 = require("../../@standard/ios/enum/ios-pk-payment-networks.enum.js");
const payment_method_name_enum_js_1 = require("../../enum/payment-method-name.enum.js");
const payments_error_enum_js_1 = require("../../enum/payments-error.enum.js");
const supported_networks_enum_js_1 = require("../../enum/supported-networks.enum.js");
const constructor_error_js_1 = require("../../error/constructor.error.js");
const dom_exception_js_1 = require("../../error/dom.exception.js");
const payments_error_js_1 = require("../../error/payments.error.js");
const get_native_payments_event_emitter_util_js_1 = require("../../util/get-native-payments-event-emitter/get-native-payments-event-emitter.util.js");
const is_native_user_cancellation_util_js_1 = require("../../util/is-native-user-cancellation.util.js");
const resolve_payment_details_modifier_util_js_1 = require("../../util/resolve-payment-details-modifier.util.js");
const validate_android_transaction_info_util_js_1 = require("../../util/validate-android-transaction-info.util.js");
const validate_details_update_util_js_1 = require("../../util/validate-details-update.util.js");
const validate_display_items_util_js_1 = require("../../util/validate-display-items.util.js");
const validate_modifiers_util_js_1 = require("../../util/validate-modifiers.util.js");
const validate_payment_methods_util_js_1 = require("../../util/validate-payment-methods.util.js");
const validate_shipping_options_util_js_1 = require("../../util/validate-shipping-options.util.js");
const validate_shipping_type_util_js_1 = require("../../util/validate-shipping-type.util.js");
const validate_total_util_js_1 = require("../../util/validate-total.util.js");
const warn_change_event_error_util_js_1 = require("../../util/warn-change-event-error.util.js");
const change_event_dispatcher_js_1 = require("../change-event-dispatcher/change-event-dispatcher.js");
const native_payments_js_1 = require("../native-payments/native-payments.js");
const android_payment_response_js_1 = require("../payment-response/android-payment-response.js");
const ios_payment_response_js_1 = require("../payment-response/ios-payment-response.js");
const uuid = react_native_uuid_1.default;
/*
* HINT: Troubleshooting: https://developers.google.com/pay/api/android/support/troubleshooting
* HINT: Google Pay API Errors: https://developers.google.com/pay/api/web/reference/error-objects
*/
class PaymentRequest {
constructor(methodData, details) {
this.methodData = methodData;
this.details = details;
this.updating = false;
this.state = 'created';
this.couponCode = null;
this.shippingAddress = null;
this.shippingOption = null;
this.eventRegistrations = new Map();
this.pendingDispatchers = new Set();
this.attributeHandlers = new Map();
this.attributeHandlerWrappers = new Map();
this.eventGeneration = 0;
this.isNativeEventsSynced = false;
this.acceptPromiseRejecter = shared_1.emptyFn;
// 3. Establish the request's id:
if (!(0, shared_1.isNotEmptyString)(details.id)) {
// TODO: Can we avoid using external lib? Use Math.random?
details.id = uuid.v4();
}
this.id = details.id;
this.validateConstructorInputs(methodData, details);
// 17. Set request.[[serializedMethodData]] to serializedMethodData. */
this.platformMethodData = this.findPlatformPaymentMethodData();
const nativePlatformMethodData = react_native_1.Platform.OS === 'android'
? this.getAndroidPaymentMethodData(this.platformMethodData, this.resolveEffectiveDetails(details).total)
: this.getIosPaymentMethodData(this.platformMethodData);
this.serializedMethodData = JSON.stringify(nativePlatformMethodData);
}
// https://www.w3.org/TR/payment-request/#dom-paymentrequest-onshippingaddresschange
get onshippingaddresschange() {
return this.getAttributeHandler('shippingaddresschange');
}
// https://www.w3.org/TR/payment-request/#dom-paymentrequest-onshippingoptionchange
get onshippingoptionchange() {
return this.getAttributeHandler('shippingoptionchange');
}
// https://www.w3.org/TR/payment-request/#dom-paymentrequest-onpaymentmethodchange
get onpaymentmethodchange() {
return this.getAttributeHandler('paymentmethodchange');
}
// couponcodechange is a PassKit extension: https://developer.apple.com/documentation/passkit/pkpaymentrequest/3801275-couponcode?language=objc
get oncouponcodechange() {
return this.getAttributeHandler('couponcodechange');
}
set onshippingaddresschange(listener) {
this.setAttributeHandler('shippingaddresschange', listener);
}
set onshippingoptionchange(listener) {
this.setAttributeHandler('shippingoptionchange', listener);
}
set onpaymentmethodchange(listener) {
this.setAttributeHandler('paymentmethodchange', listener);
}
set oncouponcodechange(listener) {
this.setAttributeHandler('couponcodechange', listener);
}
// https://www.w3.org/TR/payment-request/#canmakepayment-method
async canMakePayment() {
if (this.state !== 'created') {
throw new dom_exception_js_1.DOMException(payments_error_enum_js_1.PaymentsErrorEnum.InvalidStateError);
}
return native_payments_js_1.NativePayments.canMakePayments(this.serializedMethodData);
}
// https://www.w3.org/TR/payment-request/#hasenrolledinstrument-method
async hasEnrolledInstrument() {
if (this.state !== 'created') {
throw new dom_exception_js_1.DOMException(payments_error_enum_js_1.PaymentsErrorEnum.InvalidStateError);
}
return native_payments_js_1.NativePayments.hasEnrolledInstrument(this.serializedMethodData);
}
// https://www.w3.org/TR/payment-request/#show-method
show() {
if (this.state !== 'created') {
return Promise.reject(new dom_exception_js_1.DOMException(payments_error_enum_js_1.PaymentsErrorEnum.InvalidStateError));
}
this.state = 'interactive';
this.syncActiveEvents();
const resolvedDetails = this.resolveEffectiveDetails(this.details);
// HINT: We need to pass Android environment configuration to native module via details
const details = react_native_1.Platform.OS === 'android'
? {
...this.details,
environment: this.platformMethodData.environment,
...resolvedDetails,
}
: { ...this.details, ...resolvedDetails };
return new Promise((resolve, reject) => {
this.acceptPromiseRejecter = reject;
native_payments_js_1.NativePayments.show(this.id, this.serializedMethodData, details)
.then(jsonDetails => {
const paymentResponse = this.handleAccept(jsonDetails);
this.closeRequest();
resolve(paymentResponse);
return void 0;
})
.catch((error) => {
this.closeRequest();
if ((0, is_native_user_cancellation_util_js_1.isNativeUserCancellation)(error)) {
reject(new dom_exception_js_1.DOMException(payments_error_enum_js_1.PaymentsErrorEnum.AbortError));
return;
}
reject((0, shared_1.isError)(error) ? error : new payments_error_js_1.PaymentsError(`Failed showing PaymentRequest`));
});
});
}
// https://www.w3.org/TR/payment-request/#abort-method
async abort() {
if (this.state !== 'interactive') {
throw new dom_exception_js_1.DOMException(payments_error_enum_js_1.PaymentsErrorEnum.InvalidStateError);
}
await native_payments_js_1.NativePayments.abort().catch(() => {
throw new payments_error_js_1.PaymentsError(`Failed aborting PaymentRequest`);
});
this.closeRequest();
this.acceptPromiseRejecter(new dom_exception_js_1.DOMException(payments_error_enum_js_1.PaymentsErrorEnum.AbortError));
}
addEventListener(type, eventListener) {
if (this.state === 'closed') {
return;
}
const listener = eventListener;
const registration = this.eventRegistrations.get(type);
if ((0, shared_1.isDefined)(registration)) {
if (!registration.listeners.includes(listener)) {
registration.listeners.push(listener);
}
return;
}
const listeners = [listener];
this.eventRegistrations.set(type, { listeners, subscription: this.subscribeToNativeEvent(type, listeners) });
this.syncActiveEvents();
}
removeEventListener(type, eventListener) {
const registration = this.eventRegistrations.get(type);
if (!(0, shared_1.isDefined)(registration)) {
return;
}
this.forgetListener(registration, eventListener);
if ((0, shared_1.isNotEmptyArray)(registration.listeners)) {
return;
}
this.dropRegistration(type, registration);
}
closeRequest() {
this.state = 'closed';
this.clearEventRegistrations();
}
validateConstructorInputs(methodData, details) {
// 4. Process payment methods
(0, validate_payment_methods_util_js_1.validatePaymentMethods)(methodData);
(0, validate_android_transaction_info_util_js_1.validateAndroidTransactionInfo)(methodData, constructor_error_js_1.ConstructorError);
(0, validate_shipping_type_util_js_1.validateShippingType)(methodData, constructor_error_js_1.ConstructorError);
// 5. Process the total
(0, validate_total_util_js_1.validateTotal)(details.total, constructor_error_js_1.ConstructorError);
// 6. If the displayItems member of details is present, then for each item in details.displayItems:
(0, validate_display_items_util_js_1.validateDisplayItems)(constructor_error_js_1.ConstructorError, details.displayItems);
(0, validate_shipping_options_util_js_1.validateShippingOptions)(constructor_error_js_1.ConstructorError, details.shippingOptions);
(0, validate_modifiers_util_js_1.validateModifiers)(constructor_error_js_1.ConstructorError, details.modifiers);
}
getAttributeHandler(type) {
return this.attributeHandlers.get(type) ?? null;
}
setAttributeHandler(type, listener) {
if (!(0, shared_1.isDefined)(listener)) {
this.attributeHandlers.delete(type);
this.removeAttributeHandlerWrapper(type);
return;
}
this.attributeHandlers.set(type, listener);
this.ensureAttributeHandlerWrapper(type);
}
ensureAttributeHandlerWrapper(type) {
if (this.attributeHandlerWrappers.has(type)) {
return;
}
const wrapper = event => this.dispatchToAttributeHandler(type, event);
this.attributeHandlerWrappers.set(type, wrapper);
this.addEventListener(type, wrapper);
}
removeAttributeHandlerWrapper(type) {
const wrapper = this.attributeHandlerWrappers.get(type);
if (!(0, shared_1.isDefined)(wrapper)) {
return;
}
this.attributeHandlerWrappers.delete(type);
this.removeEventListener(type, wrapper);
}
dispatchToAttributeHandler(type, event) {
return this.attributeHandlers.get(type)(event);
}
handleAccept(details) {
return react_native_1.Platform.OS === 'android'
? new android_payment_response_js_1.AndroidPaymentResponse(this.id, payment_method_name_enum_js_1.PaymentMethodNameEnum.AndroidPay, details, this.shippingOption)
: new ios_payment_response_js_1.IosPaymentResponse(this.id, payment_method_name_enum_js_1.PaymentMethodNameEnum.ApplePay, details, this.shippingOption);
}
subscribeToNativeEvent(type, listeners) {
const eventEmitter = (0, get_native_payments_event_emitter_util_js_1.getNativePaymentsEventEmitter)();
if (!(0, shared_1.isDefined)(eventEmitter)) {
return null;
}
return eventEmitter.addListener(type, (payload) => {
this.handleChangeEvent(type, listeners, payload).catch(warn_change_event_error_util_js_1.warnChangeEventError);
});
}
dropRegistration(type, registration) {
if ((0, shared_1.isDefined)(registration.subscription)) {
registration.subscription.remove();
}
this.eventRegistrations.delete(type);
this.syncActiveEvents();
}
syncActiveEvents() {
const eventNames = [...this.eventRegistrations.keys()];
const { setActiveEvents } = native_payments_js_1.NativePayments;
if (!(0, shared_1.isDefined)(setActiveEvents) || ((0, shared_1.isEmptyArray)(eventNames) && !this.isNativeEventsSynced)) {
return;
}
this.isNativeEventsSynced = (0, shared_1.isNotEmptyArray)(eventNames);
setActiveEvents(this.id, eventNames).catch(shared_1.emptyFn);
}
clearEventRegistrations() {
this.eventGeneration += 1;
this.pendingDispatchers.forEach(dispatcher => {
dispatcher.abandon();
});
this.pendingDispatchers.clear();
this.eventRegistrations.forEach(registration => {
if ((0, shared_1.isDefined)(registration.subscription)) {
registration.subscription.remove();
}
});
this.eventRegistrations.clear();
this.syncActiveEvents();
}
forgetListener(registration, listener) {
const listenerIndex = registration.listeners.indexOf(listener);
if (listenerIndex >= 0) {
registration.listeners.splice(listenerIndex, 1);
}
}
async handleChangeEvent(type, listeners, payload) {
const generation = this.eventGeneration;
if (payload.requestId !== this.id || !this.isDispatchActive(generation)) {
return;
}
this.applyEventPayload(payload);
if (this.updating) {
await this.sendDetailsUpdate(type, payload.eventId, null, generation);
return;
}
this.updating = true;
try {
await this.dispatchChangeEvent(type, listeners, payload, generation);
}
finally {
this.updating = false;
}
}
isDispatchActive(generation) {
return this.state === 'interactive' && this.eventGeneration === generation;
}
async dispatchChangeEvent(type, listeners, payload, generation) {
const dispatcher = new change_event_dispatcher_js_1.ChangeEventDispatcher(type, payload, () => this.isDispatchActive(generation));
this.pendingDispatchers.add(dispatcher);
try {
const detailsUpdate = await this.resolveDetailsUpdate(dispatcher, listeners);
await this.sendDetailsUpdate(type, payload.eventId, detailsUpdate, generation);
}
finally {
this.pendingDispatchers.delete(dispatcher);
}
}
applyEventPayload(payload) {
if ((0, shared_1.isDefined)(payload.shippingAddress)) {
this.shippingAddress = payload.shippingAddress;
}
if ((0, shared_1.isDefined)(payload.shippingOption)) {
this.shippingOption = payload.shippingOption;
}
if ((0, shared_1.isDefined)(payload.couponCode)) {
this.couponCode = payload.couponCode;
}
}
async resolveDetailsUpdate(dispatcher, listeners) {
try {
const detailsUpdate = await dispatcher.dispatch(listeners);
if ((0, shared_1.isDefined)(detailsUpdate)) {
(0, validate_details_update_util_js_1.validateDetailsUpdate)(detailsUpdate);
}
return detailsUpdate;
}
catch (error) {
(0, warn_change_event_error_util_js_1.warnChangeEventError)(error);
return null;
}
}
async sendDetailsUpdate(type, eventId, detailsUpdate, generation) {
const { updatePaymentDetails } = native_payments_js_1.NativePayments;
if (!this.isDispatchActive(generation) || !(0, shared_1.isDefined)(updatePaymentDetails)) {
return;
}
const updatedDetails = this.getUpdatedDetails(detailsUpdate);
const resolvedDetails = this.resolveEffectiveDetails(updatedDetails);
const update = {
error: detailsUpdate?.error ?? '',
eventName: type,
requestId: this.id,
total: resolvedDetails.total,
...((0, shared_1.isDefined)(eventId) && { eventId }),
};
await updatePaymentDetails(update, resolvedDetails.displayItems, updatedDetails.shippingOptions ?? []);
this.details = updatedDetails;
}
getUpdatedDetails(detailsUpdate) {
if (!(0, shared_1.isDefined)(detailsUpdate)) {
return this.details;
}
return {
...this.details,
...((0, shared_1.isDefined)(detailsUpdate.total) && { total: detailsUpdate.total }),
...((0, shared_1.isDefined)(detailsUpdate.displayItems) && { displayItems: detailsUpdate.displayItems }),
...((0, shared_1.isDefined)(detailsUpdate.shippingOptions) && { shippingOptions: detailsUpdate.shippingOptions }),
...((0, shared_1.isDefined)(detailsUpdate.modifiers) && { modifiers: detailsUpdate.modifiers }),
};
}
resolveEffectiveDetails(details) {
return (0, resolve_payment_details_modifier_util_js_1.resolvePaymentDetailsModifier)(this.getPlatformSupportedMethod(), details.total, details.displayItems, details.modifiers);
}
getPlatformSupportedMethod() {
return react_native_1.Platform.OS === 'ios' ? payment_method_name_enum_js_1.PaymentMethodNameEnum.ApplePay : payment_method_name_enum_js_1.PaymentMethodNameEnum.AndroidPay;
}
findPlatformPaymentMethodData() {
const platformSupportedMethod = this.getPlatformSupportedMethod();
const platformMethod = this.methodData.find(paymentMethodData => paymentMethodData.supportedMethods === platformSupportedMethod);
if (!(0, shared_1.isDefined)(platformMethod)) {
throw new dom_exception_js_1.DOMException(payments_error_enum_js_1.PaymentsErrorEnum.NotSupportedError);
}
return platformMethod.data;
}
getAndroidPaymentMethodData(methodData, total) {
const isBillingRequired = methodData.requestBillingAddress === true ||
methodData.requestPayerName === true ||
methodData.requestPayerPhone === true;
const totalPriceStatus = methodData.totalPriceStatus ?? android_transaction_info_js_1.defaultAndroidTransactionInfo.totalPriceStatus;
return {
...android_payment_data_request_js_1.defaultAndroidPaymentDataRequest,
merchantInfo: {
merchantName: total.label,
},
transactionInfo: {
...android_transaction_info_js_1.defaultAndroidTransactionInfo,
currencyCode: methodData.currencyCode,
totalPrice: total.amount.value,
totalPriceLabel: total.label,
countryCode: methodData.countryCode,
totalPriceStatus,
...((0, shared_1.isDefined)(methodData.checkoutOption) && { checkoutOption: methodData.checkoutOption }),
...((0, shared_1.isDefined)(methodData.transactionId) && { transactionId: methodData.transactionId }),
},
allowedPaymentMethods: [
{
...android_payment_method_js_1.defaultAndroidPaymentMethod,
parameters: {
...android_payment_method_js_1.defaultAndroidPaymentMethod.parameters,
allowedCardNetworks: methodData.supportedNetworks.map(network => network.toUpperCase()),
allowedAuthMethods: methodData.allowedAuthMethods ?? android_payment_method_js_1.defaultAndroidPaymentMethod.parameters.allowedAuthMethods,
...(isBillingRequired && {
billingAddressRequired: true,
billingAddressParameters: {
format: methodData.requestBillingAddress === true ? 'FULL' : 'MIN',
phoneNumberRequired: methodData.requestPayerPhone === true,
},
}),
},
...((0, shared_1.isDefined)(methodData.gatewayConfig) && {
tokenizationSpecification: {
parameters: methodData.gatewayConfig,
type: android_payment_method_tokenization_type_enum_js_1.AndroidPaymentMethodTokenizationType.PAYMENT_GATEWAY,
},
}),
...((0, shared_1.isDefined)(methodData.directConfig) && {
tokenizationSpecification: {
parameters: methodData.directConfig,
type: android_payment_method_tokenization_type_enum_js_1.AndroidPaymentMethodTokenizationType.DIRECT,
},
}),
},
],
...(methodData.requestPayerEmail === true && { emailRequired: true }),
...(methodData.requestShipping === true && {
shippingAddressRequired: true,
shippingAddressParameters: {
phoneNumberRequired: methodData.requestPayerPhone === true,
},
}),
};
}
getIosPaymentMethodData(methodData) {
// TODO: Add mappings for other systems if needed
const supportedNetworkMap = {
[supported_networks_enum_js_1.SupportedNetworkEnum.Amex]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkAmex,
[supported_networks_enum_js_1.SupportedNetworkEnum.Mastercard]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkMasterCard,
[supported_networks_enum_js_1.SupportedNetworkEnum.Visa]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkVisa,
[supported_networks_enum_js_1.SupportedNetworkEnum.Discover]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkDiscover,
[supported_networks_enum_js_1.SupportedNetworkEnum.Bancontact]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkBancontact,
[supported_networks_enum_js_1.SupportedNetworkEnum.CartesBancaires]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkCartesBancaires,
[supported_networks_enum_js_1.SupportedNetworkEnum.ChinaUnionPay]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkChinaUnionPay,
[supported_networks_enum_js_1.SupportedNetworkEnum.Dankort]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkDankort,
[supported_networks_enum_js_1.SupportedNetworkEnum.Eftpos]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkEftpos,
[supported_networks_enum_js_1.SupportedNetworkEnum.Electron]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkElectron,
[supported_networks_enum_js_1.SupportedNetworkEnum.Elo]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkElo,
[supported_networks_enum_js_1.SupportedNetworkEnum.Girocard]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkGirocard,
[supported_networks_enum_js_1.SupportedNetworkEnum.Interac]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkInterac,
[supported_networks_enum_js_1.SupportedNetworkEnum.Jcb]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkJCB,
[supported_networks_enum_js_1.SupportedNetworkEnum.Mada]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkMada,
[supported_networks_enum_js_1.SupportedNetworkEnum.Maestro]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkMaestro,
[supported_networks_enum_js_1.SupportedNetworkEnum.Mir]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkMir,
[supported_networks_enum_js_1.SupportedNetworkEnum.PrivateLabel]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkPrivateLabel,
[supported_networks_enum_js_1.SupportedNetworkEnum.Vpay]: ios_pk_payment_networks_enum_js_1.IosPKPaymentNetworksEnum.PKPaymentNetworkVPay,
};
const defaultMerchantCapabilities = [
ios_pk_merchant_capability_enum_js_1.IosPKMerchantCapability.PKMerchantCapability3DS,
ios_pk_merchant_capability_enum_js_1.IosPKMerchantCapability.PKMerchantCapabilityDebit,
ios_pk_merchant_capability_enum_js_1.IosPKMerchantCapability.PKMerchantCapabilityCredit,
];
const requestedShippingFields = this.getRequestedShippingFields(methodData);
const isShippingRequested = requestedShippingFields.length > 0;
return {
countryCode: methodData.countryCode,
currencyCode: methodData.currencyCode,
merchantIdentifier: methodData.merchantIdentifier,
supportedNetworks: methodData.supportedNetworks.map(network => supportedNetworkMap[network]),
merchantCapabilities: (0, shared_1.isNotEmptyArray)(methodData.merchantCapabilities)
? methodData.merchantCapabilities
: defaultMerchantCapabilities,
...(methodData.requestBillingAddress === true && {
requiredBillingContactFields: [ios_pk_contact_field_enum_js_1.IOSPKContactField.PKContactFieldPostalAddress],
}),
...(isShippingRequested && { requiredShippingContactFields: requestedShippingFields }),
...((0, shared_1.isDefined)(methodData.applicationData) && { applicationData: methodData.applicationData }),
...((0, shared_1.isNotEmptyString)(methodData.couponCode) && { couponCode: methodData.couponCode }),
...((0, shared_1.isDefined)(methodData.shippingType) && { shippingType: methodData.shippingType }),
};
}
getRequestedShippingFields(methodData) {
const requiredShippingFields = [];
if (methodData.requestPayerEmail ?? false) {
requiredShippingFields.push(ios_pk_contact_field_enum_js_1.IOSPKContactField.PKContactFieldEmailAddress);
}
if (methodData.requestPayerName ?? false) {
requiredShippingFields.push(ios_pk_contact_field_enum_js_1.IOSPKContactField.PKContactFieldName);
}
if (methodData.requestPayerPhone ?? false) {
requiredShippingFields.push(ios_pk_contact_field_enum_js_1.IOSPKContactField.PKContactFieldPhoneNumber);
}
if (methodData.requestShipping ?? false) {
requiredShippingFields.push(ios_pk_contact_field_enum_js_1.IOSPKContactField.PKContactFieldPostalAddress);
}
return requiredShippingFields;
}
}
exports.PaymentRequest = PaymentRequest;
//# sourceMappingURL=payment-request.js.map