From 3f0ff96ede206d1c38bfdbecbc848c23ac37706a Mon Sep 17 00:00:00 2001 From: Ildar Galeev Date: Mon, 10 Jun 2024 18:52:15 +0700 Subject: [PATCH] Disable retry payment with externalID (#310) --- .../backend/payments/createInvoiceWithTemplate.ts | 1 + src/common/backend/payments/paymentModel.ts | 3 ++- .../init/configResolver/resolveInitConfig.test.ts | 2 +- src/common/init/configResolver/resolveInitConfig.ts | 2 +- src/common/paymentCondition/types.ts | 1 + .../utils/invoiceEventsToConditions.ts | 1 + src/common/paymentMgmt/createInvoiceWithTemplate.ts | 2 ++ src/common/paymentMgmt/createPayment.ts | 2 +- src/common/paymentModel/initPaymentModel.ts | 3 ++- src/common/paymentModel/toInitContext.ts | 4 ++-- src/common/paymentModel/types.ts | 3 ++- .../PaymentResultView/PaymentResultView.tsx | 4 ++-- .../ViewContainer/PaymentResultView/utils/index.ts | 1 + .../PaymentResultView/utils/isExternalIdEmpty.ts | 11 +++++++++++ 14 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/components/ViewContainer/PaymentResultView/utils/isExternalIdEmpty.ts diff --git a/src/common/backend/payments/createInvoiceWithTemplate.ts b/src/common/backend/payments/createInvoiceWithTemplate.ts index b9f6dbf5..2d367132 100644 --- a/src/common/backend/payments/createInvoiceWithTemplate.ts +++ b/src/common/backend/payments/createInvoiceWithTemplate.ts @@ -5,6 +5,7 @@ export type InvoiceParamsWithTemplate = { amount: number; currency: string; metadata: object; + externalID?: string; }; export const createInvoiceWithTemplate = async ( diff --git a/src/common/backend/payments/paymentModel.ts b/src/common/backend/payments/paymentModel.ts index 2779129e..8a139239 100644 --- a/src/common/backend/payments/paymentModel.ts +++ b/src/common/backend/payments/paymentModel.ts @@ -224,6 +224,7 @@ export type InvoiceTemplate = { lifetime: LifetimeInterval; details: InvoiceTemplateMultiLine | InvoiceTemplateSingleLine; metadata: object; + externalID?: string; }; export type ServiceProvider = { @@ -273,5 +274,5 @@ export type Invoice = { status: InvoiceStatus; reason: string; cart: InvoiceLine[]; - externalID: string; + externalID?: string; }; diff --git a/src/common/init/configResolver/resolveInitConfig.test.ts b/src/common/init/configResolver/resolveInitConfig.test.ts index f7e90029..21bbace3 100644 --- a/src/common/init/configResolver/resolveInitConfig.test.ts +++ b/src/common/init/configResolver/resolveInitConfig.test.ts @@ -18,7 +18,7 @@ it('should return resolved init config', () => { invoiceID: 'someID', invoiceAccessToken: 'some token', recurring: false, - isExternalIDIncluded: false, + isExternalIDIncluded: true, locale: 'en', requireCardHolder: false, obscureCardCvv: true, diff --git a/src/common/init/configResolver/resolveInitConfig.ts b/src/common/init/configResolver/resolveInitConfig.ts index 11532ee4..b14e3d91 100644 --- a/src/common/init/configResolver/resolveInitConfig.ts +++ b/src/common/init/configResolver/resolveInitConfig.ts @@ -45,6 +45,6 @@ export const resolveInitConfig = (userConfig: Partial): InitConfig = metadata: setDefault(resolveObject(metadata), undefined), terminalFormValues: setDefault(resolveObject(terminalFormValues), undefined), skipUserInteraction: setDefault(resolveBoolean(skipUserInteraction), false), - isExternalIDIncluded: setDefault(resolveBoolean(isExternalIDIncluded), false), + isExternalIDIncluded: setDefault(resolveBoolean(isExternalIDIncluded), true), }; }; diff --git a/src/common/paymentCondition/types.ts b/src/common/paymentCondition/types.ts index e75c147e..63aea96b 100644 --- a/src/common/paymentCondition/types.ts +++ b/src/common/paymentCondition/types.ts @@ -22,6 +22,7 @@ export type PaymentStarted = { eventId: number; paymentId: string; provider?: string; + externalId?: string; isInstantPayment: boolean; }; diff --git a/src/common/paymentCondition/utils/invoiceEventsToConditions.ts b/src/common/paymentCondition/utils/invoiceEventsToConditions.ts index 2e869033..51063fbb 100644 --- a/src/common/paymentCondition/utils/invoiceEventsToConditions.ts +++ b/src/common/paymentCondition/utils/invoiceEventsToConditions.ts @@ -49,6 +49,7 @@ export const invoiceEventsToConditions = ( eventId: id, provider: getProvider(change), paymentId: change.payment.id, + externalId: change.payment.externalID, isInstantPayment, }, ]; diff --git a/src/common/paymentMgmt/createInvoiceWithTemplate.ts b/src/common/paymentMgmt/createInvoiceWithTemplate.ts index b066b6b1..91fd40c9 100644 --- a/src/common/paymentMgmt/createInvoiceWithTemplate.ts +++ b/src/common/paymentMgmt/createInvoiceWithTemplate.ts @@ -7,12 +7,14 @@ export const createInvoiceWithTemplate = async (model: PaymentModelInvoiceTempla apiEndpoint, metadata, paymentAmount, + externalID, invoiceTemplateParams: { invoiceTemplateID, invoiceTemplateAccessToken }, } = model; const invoiceAndToken = await request(apiEndpoint, invoiceTemplateAccessToken, invoiceTemplateID, { amount: paymentAmount.value, currency: paymentAmount.currency, metadata, + externalID, }); return invoiceToInvoiceContext(invoiceAndToken); }; diff --git a/src/common/paymentMgmt/createPayment.ts b/src/common/paymentMgmt/createPayment.ts index 092b8e4f..697a6773 100644 --- a/src/common/paymentMgmt/createPayment.ts +++ b/src/common/paymentMgmt/createPayment.ts @@ -18,7 +18,7 @@ export const createPayment = async ( payer, metadata, makeRecurrent: recurring, - externalID: isExternalIDIncluded ? invoiceContext.externalID : undefined, + externalID: isExternalIDIncluded ? invoiceContext?.externalID : undefined, }; const { invoiceID, invoiceAccessToken } = invoiceContext.invoiceParams; const createPaymentWithRetry = withRetry(request); diff --git a/src/common/paymentModel/initPaymentModel.ts b/src/common/paymentModel/initPaymentModel.ts index 160a72c0..9d0d5362 100644 --- a/src/common/paymentModel/initPaymentModel.ts +++ b/src/common/paymentModel/initPaymentModel.ts @@ -19,11 +19,12 @@ const applyInvoice = ( const applyInvoiceTemplate = ( { invoiceTemplateParams, type }: Partial, - { invoiceTemplate: { metadata } }: BackendModelInvoiceTemplate, + { invoiceTemplate: { metadata, externalID } }: BackendModelInvoiceTemplate, ): InvoiceTemplateContext => ({ type, invoiceTemplateParams, metadata, + externalID, }); const applyBackendModel = ( diff --git a/src/common/paymentModel/toInitContext.ts b/src/common/paymentModel/toInitContext.ts index 8200dd4b..f187a966 100644 --- a/src/common/paymentModel/toInitContext.ts +++ b/src/common/paymentModel/toInitContext.ts @@ -8,10 +8,10 @@ const toContactInfo = ({ phoneNumber, email }: InitConfig) => ({ }); export const toInitContext = (initConfig: InitConfig): InitContext => ({ - skipUserInteraction: initConfig?.skipUserInteraction || false, + skipUserInteraction: initConfig?.skipUserInteraction, + isExternalIDIncluded: initConfig?.isExternalIDIncluded, terminalFormValues: initConfig.terminalFormValues, paymentMetadata: initConfig.metadata, - isExternalIDIncluded: initConfig.isExternalIDIncluded, contactInfo: toContactInfo(initConfig), redirectUrl: initConfig.redirectUrl, metadata: initConfig.metadata, diff --git a/src/common/paymentModel/types.ts b/src/common/paymentModel/types.ts index 4a630d93..d4f2a2d5 100644 --- a/src/common/paymentModel/types.ts +++ b/src/common/paymentModel/types.ts @@ -74,14 +74,15 @@ export type InvoiceTemplateContext = { readonly type: 'InvoiceTemplateContext'; readonly invoiceTemplateParams: InvoiceTemplateParams; readonly metadata: object; + readonly externalID?: string; }; export type InvoiceContext = { readonly type: 'InvoiceContext'; readonly invoiceParams: InvoiceParams; readonly dueDate: string; - readonly externalID: string; readonly status: InvoiceStatus; + readonly externalID?: string; }; export type PaymentModelInvoice = InvoiceContext & CommonPaymentModel; diff --git a/src/components/ViewContainer/PaymentResultView/PaymentResultView.tsx b/src/components/ViewContainer/PaymentResultView/PaymentResultView.tsx index d0058b36..183042c5 100644 --- a/src/components/ViewContainer/PaymentResultView/PaymentResultView.tsx +++ b/src/components/ViewContainer/PaymentResultView/PaymentResultView.tsx @@ -11,7 +11,7 @@ import { import { isNil, last } from 'checkout/utils'; import { ResultIcon } from './ResultIcon'; -import { getPaymentFormViewId, getResultInfo, isInstantPayment } from './utils'; +import { getPaymentFormViewId, getResultInfo, isExternalIdEmpty, isInstantPayment } from './utils'; export function PaymentResultView() { const { l } = useContext(LocaleContext); @@ -66,7 +66,7 @@ export function PaymentResultView() { - {hasActions && ( + {hasActions && isExternalIdEmpty(conditions) && ( diff --git a/src/components/ViewContainer/PaymentResultView/utils/index.ts b/src/components/ViewContainer/PaymentResultView/utils/index.ts index 48bfed47..198ebbe1 100644 --- a/src/components/ViewContainer/PaymentResultView/utils/index.ts +++ b/src/components/ViewContainer/PaymentResultView/utils/index.ts @@ -1,3 +1,4 @@ export { getResultInfo } from './getResultInfo'; export { getPaymentFormViewId } from './getPaymentFormViewId'; export { isInstantPayment } from './isInstantPayment'; +export { isExternalIdEmpty } from './isExternalIdEmpty'; diff --git a/src/components/ViewContainer/PaymentResultView/utils/isExternalIdEmpty.ts b/src/components/ViewContainer/PaymentResultView/utils/isExternalIdEmpty.ts new file mode 100644 index 00000000..cca928fb --- /dev/null +++ b/src/components/ViewContainer/PaymentResultView/utils/isExternalIdEmpty.ts @@ -0,0 +1,11 @@ +import { PaymentCondition, PaymentStarted } from 'checkout/paymentCondition'; +import { isNil } from 'checkout/utils'; + +export const isExternalIdEmpty = (conditions: PaymentCondition[]): boolean => { + const found = conditions + .slice() + .reverse() + .find((condition) => condition.name === 'paymentStarted') as PaymentStarted; + if (isNil(found)) return false; + return isNil(found.externalId); +};