From 6648e99dff1aad95f8384bb881bb75fb43f9f897 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 12 May 2021 19:22:42 +0100 Subject: [PATCH] Consolodate response handling --- .../cart-checkout/checkout-state/index.tsx | 6 +- .../cart-checkout/checkout-state/types.ts | 6 +- .../cart-checkout/checkout-state/utils.ts | 87 +++++++------------ 3 files changed, 38 insertions(+), 61 deletions(-) diff --git a/assets/js/base/context/providers/cart-checkout/checkout-state/index.tsx b/assets/js/base/context/providers/cart-checkout/checkout-state/index.tsx index 069b8f07774..1efca1781fd 100644 --- a/assets/js/base/context/providers/cart-checkout/checkout-state/index.tsx +++ b/assets/js/base/context/providers/cart-checkout/checkout-state/index.tsx @@ -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, @@ -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( diff --git a/assets/js/base/context/providers/cart-checkout/checkout-state/types.ts b/assets/js/base/context/providers/cart-checkout/checkout-state/types.ts index ded58972b6f..b6d5bc9d85f 100644 --- a/assets/js/base/context/providers/cart-checkout/checkout-state/types.ts +++ b/assets/js/base/context/providers/cart-checkout/checkout-state/types.ts @@ -4,7 +4,7 @@ import { STATUS } from './constants'; import type { emitterCallback } from '../../../event-emit'; -interface ErrorResponse { +export interface CheckoutResponseError { code: string; message: string; data: { @@ -12,7 +12,7 @@ interface ErrorResponse { }; } -interface SuccessResponse { +export interface CheckoutResponseSuccess { // eslint-disable-next-line camelcase payment_result: { // eslint-disable-next-line camelcase @@ -24,7 +24,7 @@ interface SuccessResponse { }; } -export type CheckoutResponse = SuccessResponse | ErrorResponse; +export type CheckoutResponse = CheckoutResponseSuccess | CheckoutResponseError; export interface PaymentResultDataType { message: string; diff --git a/assets/js/base/context/providers/cart-checkout/checkout-state/utils.ts b/assets/js/base/context/providers/cart-checkout/checkout-state/utils.ts index 3ced0720543..94f192fe606 100644 --- a/assets/js/base/context/providers/cart-checkout/checkout-state/utils.ts +++ b/assets/js/base/context/providers/cart-checkout/checkout-state/utils.ts @@ -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.',