Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Consolodate response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed May 13, 2021
1 parent d0e0d60 commit 6648e99
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import deprecated from '@wordpress/deprecated';
*/
import { actions } from './actions';
import { reducer } from './reducer';
import { preparePaymentResult } from './utils';
import { getPaymentResultFromCheckoutResponse } from './utils';
import {
DEFAULT_STATE,
STATUS,
Expand Down Expand Up @@ -144,7 +144,9 @@ export const CheckoutStateProvider = ( {
setOrderNotes: ( orderNotes ) =>
void dispatch( actions.setOrderNotes( orderNotes ) ),
setAfterProcessing: ( response ) => {
const paymentResult = preparePaymentResult( response );
const paymentResult = getPaymentResultFromCheckoutResponse(
response
);

if ( paymentResult.redirectUrl ) {
dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import { STATUS } from './constants';
import type { emitterCallback } from '../../../event-emit';

interface ErrorResponse {
export interface CheckoutResponseError {
code: string;
message: string;
data: {
status: number;
};
}

interface SuccessResponse {
export interface CheckoutResponseSuccess {
// eslint-disable-next-line camelcase
payment_result: {
// eslint-disable-next-line camelcase
Expand All @@ -24,7 +24,7 @@ interface SuccessResponse {
};
}

export type CheckoutResponse = SuccessResponse | ErrorResponse;
export type CheckoutResponse = CheckoutResponseSuccess | CheckoutResponseError;

export interface PaymentResultDataType {
message: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,46 @@ import type { PaymentResultDataType, CheckoutResponse } from './types';
/**
* Prepares the payment_result data from the server checkout endpoint response.
*/
export const prepareResponseData = ( data: {
message?: string;
// eslint-disable-next-line camelcase
payment_status: string;
// eslint-disable-next-line camelcase
redirect_url: string;
// eslint-disable-next-line camelcase
payment_details?: Record< string, string >;
} ): PaymentResultDataType => {
const responseData = {
message: data?.message || '',
paymentStatus: data.payment_status,
redirectUrl: data.redirect_url,
paymentDetails: {},
} as PaymentResultDataType;
if (
data.hasOwnProperty( 'payment_details' ) &&
Array.isArray( data.payment_details )
) {
data.payment_details.forEach(
( { key, value }: { key: string; value: string } ) => {
responseData.paymentDetails[ key ] = decodeEntities( value );
}
);
}
return responseData;
};

/**
* Prepares the payment_result data from the server checkout endpoint response.
*/
export const preparePaymentResult = (
export const getPaymentResultFromCheckoutResponse = (
response: CheckoutResponse
): PaymentResultDataType => {
const paymentResult = {
message: 'message' in response ? response.message : '',
paymentStatus:
'payment_result' in response
? response.payment_result.payment_status
: '',
paymentDetails:
'payment_result' in response &&
message: '',
paymentStatus: '',
redirectUrl: '',
paymentDetails: {},
} as PaymentResultDataType;

// payment_result is present in successful responses.
if ( 'payment_result' in response ) {
paymentResult.paymentStatus = response.payment_result.payment_status;
paymentResult.redirectUrl = response.payment_result.redirect_url;

if (
response.payment_result.hasOwnProperty( 'payment_details' ) &&
Array.isArray( response.payment_result.payment_details )
? ( fromEntriesPolyfill(
response.payment_result.payment_details.map(
( { key, value } ) => [
key,
decodeEntities( value ),
]
)
) as Record< string, string > )
: {},
redirectUrl:
'payment_result' in response &&
'redirect_url' in response.payment_result
? response.payment_result.redirect_url
: '',
};
) {
response.payment_result.payment_details.forEach(
( { key, value }: { key: string; value: string } ) => {
paymentResult.paymentDetails[ key ] = decodeEntities(
value
);
}
);
}
}

// message is present in error responses.
if ( 'message' in response ) {
paymentResult.message = decodeEntities( response.message );
}

// If there was an error code but no message, set a default message.
if (
! paymentResult.message &&
'data' in response &&
'status' in response.data &&
response.data.status > 299 &&
! paymentResult.message
response.data.status > 299
) {
paymentResult.message = __(
'Something went wrong. Please contact us to get assistance.',
Expand Down

0 comments on commit 6648e99

Please sign in to comment.