Return payment flow hold (#318)

This commit is contained in:
Ildar Galeev 2024-07-16 18:37:46 +07:00 committed by GitHub
parent 7f714d28fd
commit bb91ee091d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 6 deletions

View File

@ -38,6 +38,9 @@ export type {
Payer,
PaymentResourcePayer,
ContactInfo,
PaymentFlow,
PaymentFlowInstant,
PaymentFlowHold,
} from './paymentModel';
export type {
ServiceProviderMetadata,

View File

@ -30,7 +30,6 @@ export type PaymentFlowInstant = {
export type PaymentFlowHold = {
type: 'PaymentFlowHold';
onHoldExpiration: 'cancel' | 'capture';
heldUntil?: string;
};
export type PaymentFlow = PaymentFlowInstant | PaymentFlowHold;

View File

@ -30,6 +30,7 @@ it('should return resolved init config', () => {
redirectUrl: null,
skipUserInteraction: false,
theme: null,
paymentFlow: null,
};
expect(actual).toEqual(expected);
});

View File

@ -31,6 +31,7 @@ export const resolveInitConfig = (userConfig: Partial<InitConfig>): InitConfig =
skipUserInteraction,
isExternalIDIncluded,
theme,
paymentFlow,
} = userConfig;
return {
...resolvedIntegrationType,
@ -48,5 +49,6 @@ export const resolveInitConfig = (userConfig: Partial<InitConfig>): InitConfig =
skipUserInteraction: setDefault(resolveBoolean(skipUserInteraction), false),
isExternalIDIncluded: setDefault(resolveBoolean(isExternalIDIncluded), true),
theme: resolveString(theme),
paymentFlow: resolveObject(paymentFlow),
};
};

View File

@ -18,6 +18,7 @@ export type InitConfig = {
terminalFormValues?: object;
skipUserInteraction?: boolean;
isExternalIDIncluded?: boolean;
paymentFlow?: object;
};
export type ThemeConfig = Record<string, any>;

View File

@ -10,11 +10,9 @@ export const createPayment = async (
payload: StartPaymentPayload,
): Promise<Payment> => {
const payer = await createPayer(model, invoiceContext, payload);
const { isExternalIDIncluded, metadata, recurring } = model.initContext;
const { isExternalIDIncluded, metadata, recurring, paymentFlow } = model.initContext;
const params: PaymentParams = {
flow: {
type: 'PaymentFlowInstant',
},
flow: paymentFlow,
payer,
metadata,
makeRecurrent: recurring,

View File

@ -1,3 +1,5 @@
import { PaymentFlow, PaymentFlowHold, PaymentFlowInstant } from 'checkout/backend/payments';
import { InitContext } from './types';
import { InitConfig } from '../init';
import { isNil } from '../utils';
@ -7,6 +9,29 @@ const toContactInfo = ({ phoneNumber, email }: InitConfig) => ({
email: isNil(email) ? undefined : email,
});
const isPaymentFlowInstant = (obj: any): obj is PaymentFlowInstant => {
if (isNil(obj)) return false;
return obj.type === 'PaymentFlowInstant';
};
const isPaymentFlowHold = (obj: any): obj is PaymentFlowHold => {
if (isNil(obj)) return false;
const isOnHoldExpiration = obj.onHoldExpiration === 'cancel' || obj.onHoldExpiration === 'capture';
return obj.type === 'PaymentFlowHold' && isOnHoldExpiration;
};
const toPaymentFlow = (paymentFlow: object | null): PaymentFlow => {
if (isPaymentFlowInstant(paymentFlow)) {
return paymentFlow;
}
if (isPaymentFlowHold(paymentFlow)) {
return paymentFlow;
}
return {
type: 'PaymentFlowInstant',
};
};
export const toInitContext = (initConfig: InitConfig): InitContext => ({
skipUserInteraction: initConfig?.skipUserInteraction,
isExternalIDIncluded: initConfig?.isExternalIDIncluded,
@ -16,4 +41,5 @@ export const toInitContext = (initConfig: InitConfig): InitContext => ({
redirectUrl: initConfig.redirectUrl,
metadata: initConfig.metadata,
recurring: initConfig.recurring,
paymentFlow: toPaymentFlow(initConfig.paymentFlow),
});

View File

@ -1,4 +1,4 @@
import { InvoiceStatus, ServiceProviderMetadata } from '../backend/payments';
import { InvoiceStatus, PaymentFlow, ServiceProviderMetadata } from '../backend/payments';
export type PaymentAmount = {
readonly value: number;
@ -40,6 +40,7 @@ export type InitContextContactInfo = {
export type InitContext = {
readonly skipUserInteraction: boolean;
readonly paymentFlow: PaymentFlow;
readonly contactInfo?: InitContextContactInfo;
readonly terminalFormValues?: object;
readonly paymentMetadata?: object;