Provide interaction completed invoice event (#309)

This commit is contained in:
Ildar Galeev 2024-06-05 19:10:52 +07:00 committed by GitHub
parent 29950a5740
commit 812afc589e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { PaymentCondition } from './types';
import { invoiceEventsToConditions, provideInstantPayment } from './utils';
import { invoiceEventsToConditions, provideInstantPayment, provideInteractionCompleted } from './utils';
import { ServiceProviderContactInfo, ServiceProviderMetadataField, getInvoiceEvents } from '../backend/payments';
import { InitContext, PaymentModel, PaymentModelInvoice, PaymentTerminal } from '../paymentModel';
import { extractError, isNil, last, withRetry } from '../utils';
@ -104,8 +104,9 @@ const provideInvoiceUnpaid = async (model: PaymentModelInvoice): Promise<Payment
case 'paymentStarted':
case 'paymentStatusChanged':
case 'interactionRequested':
case 'interactionCompleted':
return conditions;
case 'interactionCompleted':
return provideInteractionCompleted(model, lastEventId);
}
}
} catch (exception) {

View File

@ -2,3 +2,4 @@ export { invoiceEventsToConditions } from './invoiceEventsToConditions';
export { pollingResultToConditions } from './pollingResultToConditions';
export { toDefaultFormValuesMetadata } from './toDefaultFormValuesMetadata';
export { provideInstantPayment } from './provideInstantPayment';
export { provideInteractionCompleted } from './provideInteractionCompleted';

View File

@ -0,0 +1,28 @@
import { pollInvoiceEvents } from 'checkout/paymentMgmt';
import { PaymentModelInvoice } from 'checkout/paymentModel';
import { pollingResultToConditions } from './pollingResultToConditions';
const DEFAULT_TIMEOUT_MS = 60 * 1000 * 3;
const API_METHOD_CALL_MS = 1000;
export const provideInteractionCompleted = async (model: PaymentModelInvoice, lastEventId: number) => {
const {
invoiceParams: { invoiceID, invoiceAccessToken },
apiEndpoint,
} = model;
const pollingResult = await pollInvoiceEvents({
apiEndpoint,
invoiceAccessToken,
invoiceID,
startFromEventID: lastEventId,
stopPollingTypes: ['PaymentStatusChanged'],
delays: {
pollingTimeout: DEFAULT_TIMEOUT_MS,
apiMethodCall: API_METHOD_CALL_MS,
},
});
const skipUserInteraction = false;
const isInstantPayment = false;
return pollingResultToConditions(pollingResult, skipUserInteraction, isInstantPayment);
};

View File

@ -216,7 +216,6 @@ export const useViewModel = (paymentMethods: PaymentMethod[], conditions: Paymen
switch (lastCondition.name) {
case 'invoiceStatusChanged':
case 'paymentStatusChanged':
case 'interactionCompleted':
case 'paymentStatusUnknown':
case 'paymentStarted':
dispatch({ type: 'SET_VIEW', payload: { name: 'PaymentResultView', id: 'PaymentResultView' } });
@ -231,6 +230,11 @@ export const useViewModel = (paymentMethods: PaymentMethod[], conditions: Paymen
payload: { name: 'PaymentProcessFailedView', id: 'PaymentProcessFailedView' },
});
break;
case 'interactionCompleted':
// Error for an inappropriate condition usage. 'InteractionCompleted' event requires invoice status change waiting.
console.error('Inappropriate condition state: interactionCompleted');
dispatch({ type: 'SET_VIEW', payload: { name: 'PaymentResultView', id: 'PaymentResultView' } });
break;
}
}, [lastCondition]);