Skip to content

Commit

Permalink
always resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeiroguilherme committed Dec 21, 2023
1 parent 689b362 commit 5879bc3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 87 deletions.
5 changes: 3 additions & 2 deletions packages/lib/src/components/internal/UIElement/UIElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Resources } from '../../../core/Context/Resources';
import { NewableComponent } from '../../../core/core.registry';
import { ComponentMethodsRef, IUIElement, PayButtonFunctionProps, UIElementProps, UIElementStatus } from './types';
import {
OnPaymentFailedData,
Order,
PaymentAction,
PaymentData,
Expand Down Expand Up @@ -311,11 +310,13 @@ export abstract class UIElement<P extends UIElementProps = UIElementProps>
*
* @param result
*/
protected handleFailedResult = (result: OnPaymentFailedData): void => {
protected handleFailedResult = (result: PaymentResponseData): void => {
if (this.props.setStatusAutomatically) {
this.setElementStatus('error');
}

cleanupFinalResult(result);

this.props.onPaymentFailed?.(result, this.elementRef);
};

Expand Down
7 changes: 4 additions & 3 deletions packages/lib/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PaymentMethods from './ProcessResponse/PaymentMethods';
import getComponentForAction from './ProcessResponse/PaymentAction';
import { resolveEnvironment, resolveCDNEnvironment } from './Environment';
import Analytics from './Analytics';
import { OnPaymentFailedData, PaymentAction, PaymentResponseData } from '../types/global-types';
import { PaymentAction, PaymentResponseData } from '../types/global-types';
import { CoreConfiguration, ICore } from './types';
import { processGlobalOptions } from './utils';
import Session from './CheckoutSession';
Expand Down Expand Up @@ -147,8 +147,9 @@ class Core implements ICore {
cleanupFinalResult(response);
this.options.onPaymentCompleted?.(response);
})
.catch((result: OnPaymentFailedData) => {
this.options.onPaymentFailed?.(result);
.catch((response: PaymentResponseData) => {
cleanupFinalResult(response);
this.options.onPaymentFailed?.(response);
});
}

Expand Down
28 changes: 15 additions & 13 deletions packages/lib/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
PaymentAction,
PaymentMethodsResponse,
ActionHandledReturnObject,
OnPaymentCompletedData,
PaymentData,
PaymentResponseAdvancedFlow,
OnPaymentFailedData,
PaymentMethodsRequestData
PaymentMethodsRequestData,
SessionsResponse,
ResultCode
} from '../types/global-types';
import { AnalyticsOptions } from './Analytics/types';
import { RiskModuleOptions } from './RiskModule/RiskModule';
Expand Down Expand Up @@ -51,12 +51,12 @@ export interface ICore {

export type AdyenEnvironment = 'test' | 'live' | 'live-us' | 'live-au' | 'live-apse' | 'live-in' | string;

export type onSubmitReject = {
error?: {
googlePayError?: Partial<google.payments.api.PaymentDataError>;
applePayError?: ApplePayJS.ApplePayError[] | ApplePayJS.ApplePayError;
};
};
// export type onSubmitReject = {
// error?: {
// googlePayError?: Partial<google.payments.api.PaymentDataError>;
// applePayError?: ApplePayJS.ApplePayError[] | ApplePayJS.ApplePayError;
// };
// };

export interface CoreConfiguration {
session?: any;
Expand Down Expand Up @@ -182,7 +182,7 @@ export interface CoreConfiguration {
* @param data
* @param element
*/
onPaymentCompleted?(data: OnPaymentCompletedData, element?: UIElement): void;
onPaymentCompleted?(data: SessionsResponse | { resultCode: ResultCode }, element?: UIElement): void;

/**
* Called when the payment fails.
Expand All @@ -193,14 +193,15 @@ export interface CoreConfiguration {
* @param data
* @param element
*/
onPaymentFailed?(data?: OnPaymentFailedData, element?: UIElement): void;
onPaymentFailed?(data?: SessionsResponse | { resultCode: ResultCode }, element?: UIElement): void;

onSubmit?(
state: any,
element: UIElement,
actions: {
resolve: (response: PaymentResponseAdvancedFlow) => void;
reject: (error?: onSubmitReject) => void;
reject: () => void;
// reject: (error?: onSubmitReject) => void;
}
): void;

Expand All @@ -216,7 +217,8 @@ export interface CoreConfiguration {
element: UIElement,
actions: {
resolve: (response: PaymentResponseAdvancedFlow) => void;
reject: (error?: onSubmitReject) => void;
reject: () => void;
// reject: (error?: onSubmitReject) => void;
}
): void;

Expand Down
9 changes: 5 additions & 4 deletions packages/lib/src/types/global-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ADDRESS_SCHEMA } from '../components/internal/Address/constants';
import actionTypes from '../core/ProcessResponse/PaymentAction/actionTypes';
import { onSubmitReject } from '../core/types';
// import { onSubmitReject } from '../core/types';

export type PaymentActionsType = keyof typeof actionTypes;

Expand Down Expand Up @@ -335,9 +335,6 @@ export type SessionsResponse = {
sessionResult: string;
resultCode: ResultCode;
};
export type OnPaymentCompletedData = SessionsResponse | { resultCode: ResultCode };

export type OnPaymentFailedData = SessionsResponse | onSubmitReject;

//TODO double check these values
export interface PaymentMethodsRequestData {
Expand All @@ -351,6 +348,10 @@ export interface PaymentResponseAdvancedFlow {
action?: PaymentAction;
order?: Order;
donationToken?: string;
error?: {
googlePayError?: Partial<google.payments.api.PaymentDataError>;
applePayError?: ApplePayJS.ApplePayError[] | ApplePayJS.ApplePayError;
};
}

export interface PaymentResponseData {
Expand Down
55 changes: 28 additions & 27 deletions packages/playground/src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,43 @@ export function handleError(obj) {
export async function handleSubmit(state, component, actions) {
component.setStatus('loading');

console.log('onSubmit', state, actions);

try {
const result = await makePayment(state.data);
const { action, order, resultCode, donationToken } = await makePayment(state.data);

if (!result.resultCode) actions.reject();
if (!resultCode) actions.reject();

if (result.resultCode.includes('Refused', 'Cancelled', 'Error')) {
actions.reject({
resultCode: result.resultCode,
error: {
googlePayError: {},
applePayError: {}
}
});
} else {
actions.resolve({
action: result.action,
order: result.order,
resultCode: result.resultCode,
donationToken: result.donationToken
});
}
actions.resolve({
resultCode,
action,
order,
donationToken
});
} catch (error) {
console.error('## onSubmit - critical error', error);
actions.reject();
}
}

export function handleAdditionalDetails(details, component) {
// component.setStatus('processing');
export async function handleAdditionalDetails(state, component, actions) {
try {
console.log('onAdditionalDetails', state, component, actions);

const { resultCode, action, order, resultCode, donationToken } = await makeDetailsCall(state.data);

return makeDetailsCall(details.data)
.then(response => {
component.setStatus('ready');
handleResponse(response, component);
})
.catch(error => {
throw Error(error);
if (!resultCode) actions.reject();

actions.resolve({
resultCode,
action,
order,
donationToken
// error: {},
});
return;
} catch (error) {
console.error('## onAdditionalDetails - critical error', error);
actions.reject();
}
}
65 changes: 27 additions & 38 deletions packages/playground/src/pages/Dropin/manual.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,21 @@ export async function initManual() {
console.log('onSubmit', state, component.authorizedEvent);

try {
const result = await makePayment(state.data);

if (!result.resultCode) actions.reject();

if (result.resultCode.includes('Refused', 'Cancelled', 'Error')) {
actions.reject({
resultCode: result.resultCode
// error: {
// googlePayError: {},
// applePayError: {}
// }
});
} else {
actions.resolve({
action: result.action,
order: result.order,
resultCode: result.resultCode,
donationToken: result.donationToken
});
}
const { action, order, resultCode, donationToken } = await makePayment(state.data);

if (!resultCode) actions.reject();

actions.resolve({
resultCode,
action,
order,
donationToken
// error: {
// googlePayError: {},
// applePayError: {}
// }
// }
});
} catch (error) {
console.error('## onSubmit - critical error', error);
actions.reject();
Expand All @@ -81,26 +76,20 @@ export async function initManual() {

onAdditionalDetails: async (state, component, actions) => {
try {
const result = await makeDetailsCall(state.data);
console.log('onAdditionalDetails', state, component, actions);

if (!result.resultCode) actions.reject();
const { resultCode, action, order, resultCode, donationToken } = await makeDetailsCall(state.data);

if (result.resultCode.includes('Refused', 'Cancelled', 'Error')) {
actions.reject({
resultCode: result.resultCode
// error: {
// googlePayError: {},
// applePayError: {}
// }
});
} else {
actions.resolve({
action: result.action,
order: result.order,
resultCode: result.resultCode,
donationToken: result.donationToken
});
}
if (!resultCode) actions.reject();

actions.resolve({
resultCode,
action,
order,
donationToken
// error: {},
});
return;
} catch (error) {
console.error('## onAdditionalDetails - critical error', error);
actions.reject();
Expand Down

0 comments on commit 5879bc3

Please sign in to comment.