Skip to content

Commit

Permalink
Add better response type guards other payment processors
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Sep 20, 2024
1 parent 2381dca commit 0aad7a2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
13 changes: 7 additions & 6 deletions client/my-sites/checkout/src/lib/existing-card-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ export default async function existingCardProcessor(
}
return stripeResponse;
} )
.then( ( stripeResponse ) => {
if ( stripeResponse?.redirect_url && ! doesTransactionResponseRequire3DS( stripeResponse ) ) {
debug( 'transaction requires redirect' );
return makeRedirectResponse( stripeResponse.redirect_url );
.then( ( response ) => {
if ( ! response || ! ( 'redirect_url' in response ) || ! response.redirect_url ) {
return makeSuccessResponse( response );
}
debug( 'transaction was successful' );
return makeSuccessResponse( stripeResponse );
if ( ! doesTransactionResponseRequire3DS( response ) ) {
return makeRedirectResponse( response.redirect_url );
}
return makeSuccessResponse( response );
} )
.catch( ( error: Error ) => {
debug( 'transaction failed' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ export default async function genericRedirectProcessor(

return submitWpcomTransaction( formattedTransactionData, transactionOptions )
.then( ( response?: WPCOMTransactionEndpointResponse ) => {
if ( ! response?.redirect_url ) {
throw new Error( 'Error during transaction' );
if ( ! response || ! ( 'redirect_url' in response ) || ! response.redirect_url ) {
throw new Error( 'Error during transaction: no redirect URL' );
}
return makeRedirectResponse( response?.redirect_url );
return makeRedirectResponse( response.redirect_url );
} )
.catch( ( error ) => makeErrorResponse( error.message ) );
}
Expand Down
11 changes: 7 additions & 4 deletions client/my-sites/checkout/src/lib/multi-partner-card-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,14 @@ async function stripeCardProcessor(
}
return stripeResponse;
} )
.then( ( stripeResponse ) => {
if ( stripeResponse.redirect_url && ! doesTransactionResponseRequire3DS( stripeResponse ) ) {
return makeRedirectResponse( stripeResponse.redirect_url );
.then( ( response ) => {
if ( ! response || ! ( 'redirect_url' in response ) || ! response.redirect_url ) {
return makeSuccessResponse( response );
}
return makeSuccessResponse( stripeResponse );
if ( ! doesTransactionResponseRequire3DS( response ) ) {
return makeRedirectResponse( response.redirect_url );
}
return makeSuccessResponse( response );
} )
.catch( ( error: Error ) => {
debug( 'transaction failed' );
Expand Down
2 changes: 1 addition & 1 deletion client/my-sites/checkout/src/lib/pix-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export async function pixProcessor(

return submitWpcomTransaction( formattedTransactionData, options )
.then( async ( response?: WPCOMTransactionEndpointResponse ) => {
if ( ! response?.redirect_url ) {
if ( ! response || ! ( 'redirect_url' in response ) || ! response.redirect_url ) {
// eslint-disable-next-line no-console
console.error( 'Transaction response was missing required redirect url' );
throw new Error( genericErrorMessage );
Expand Down
2 changes: 1 addition & 1 deletion client/my-sites/checkout/src/lib/we-chat-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default async function weChatProcessor(

return submitWpcomTransaction( formattedTransactionData, options )
.then( async ( response?: WPCOMTransactionEndpointResponse ) => {
if ( ! response?.redirect_url ) {
if ( ! response || ! ( 'redirect_url' in response ) || ! response.redirect_url ) {
// eslint-disable-next-line no-console
console.error( 'Transaction response was missing required redirect url' );
throw new Error( genericErrorMessage );
Expand Down

0 comments on commit 0aad7a2

Please sign in to comment.