-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checkout: extract calling payment processor and handling response to …
…useProcessPayment hook (#48283) * Clarify manual transaction processing in composite-checkout README * Separate payment processor response handling into own function * Export useCreatePaymentProcessorOnClick from composite-checkout * Add missing useCreatePaymentProcessorOnClick * Rethrow PaymentProcessorResponse error * Allow payment complete callback to include isComingFromUpsell * Add withPaymentCompleteCallback HOC * Clean up PaymentCompleteCallback type a bit * Remove withPaymentCompleteCallback and shouldHideUpsellNudges * Fix useCreatePaymentProcessorOnClick import * Rename onClick creator to useProcessPayment * Throw if using useStripe outside StripeHookProvider
- Loading branch information
1 parent
a56ef2e
commit 100413f
Showing
7 changed files
with
150 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/composite-checkout/src/components/use-process-payment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useCallback, useMemo } from 'react'; | ||
import debugFactory from 'debug'; | ||
import { useI18n } from '@automattic/react-i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { usePaymentProcessors, useTransactionStatus } from '../public-api'; | ||
import { | ||
PaymentProcessorResponse, | ||
PaymentProcessorResponseType, | ||
SetTransactionComplete, | ||
SetTransactionRedirecting, | ||
ProcessPayment, | ||
} from '../types'; | ||
|
||
const debug = debugFactory( 'composite-checkout:use-create-payment-processor-on-click' ); | ||
|
||
export default function useProcessPayment(): ProcessPayment { | ||
const paymentProcessors = usePaymentProcessors(); | ||
const { setTransactionPending } = useTransactionStatus(); | ||
const handlePaymentProcessorPromise = useHandlePaymentProcessorResponse(); | ||
|
||
return useCallback( | ||
async ( paymentProcessorId, submitData ) => { | ||
debug( 'beginning payment processor onClick handler' ); | ||
if ( ! paymentProcessors[ paymentProcessorId ] ) { | ||
throw new Error( `No payment processor found with key: ${ paymentProcessorId }` ); | ||
} | ||
setTransactionPending(); | ||
debug( 'calling payment processor function', paymentProcessorId ); | ||
return handlePaymentProcessorPromise( | ||
paymentProcessorId, | ||
paymentProcessors[ paymentProcessorId ]( submitData ) | ||
); | ||
}, | ||
[ handlePaymentProcessorPromise, paymentProcessors, setTransactionPending ] | ||
); | ||
} | ||
|
||
function useHandlePaymentProcessorResponse() { | ||
const { __ } = useI18n(); | ||
const redirectErrorMessage = useMemo( | ||
() => | ||
__( | ||
'An error occurred while redirecting to the payment partner. Please try again or contact support.' | ||
), | ||
[ __ ] | ||
); | ||
const { | ||
setTransactionComplete, | ||
setTransactionRedirecting, | ||
setTransactionError, | ||
} = useTransactionStatus(); | ||
|
||
return useCallback( | ||
async ( | ||
paymentProcessorId: string, | ||
processorPromise: Promise< PaymentProcessorResponse > | ||
): Promise< PaymentProcessorResponse > => { | ||
return processorPromise | ||
.then( ( response ) => | ||
handlePaymentProcessorResponse( response, paymentProcessorId, redirectErrorMessage, { | ||
setTransactionRedirecting, | ||
setTransactionComplete, | ||
} ) | ||
) | ||
.catch( ( error: Error ) => { | ||
setTransactionError( error.message ); | ||
throw error; | ||
} ); | ||
}, | ||
[ redirectErrorMessage, setTransactionError, setTransactionComplete, setTransactionRedirecting ] | ||
); | ||
} | ||
|
||
async function handlePaymentProcessorResponse( | ||
processorResponse: PaymentProcessorResponse, | ||
paymentProcessorId: string, | ||
redirectErrorMessage: string, | ||
{ | ||
setTransactionRedirecting, | ||
setTransactionComplete, | ||
}: { | ||
setTransactionRedirecting: SetTransactionRedirecting; | ||
setTransactionComplete: SetTransactionComplete; | ||
} | ||
): Promise< PaymentProcessorResponse > { | ||
debug( 'payment processor function response', processorResponse ); | ||
if ( processorResponse.type === PaymentProcessorResponseType.REDIRECT ) { | ||
if ( ! processorResponse.payload ) { | ||
throw new Error( redirectErrorMessage ); | ||
} | ||
setTransactionRedirecting( processorResponse.payload ); | ||
return processorResponse; | ||
} | ||
if ( processorResponse.type === PaymentProcessorResponseType.SUCCESS ) { | ||
setTransactionComplete( processorResponse.payload ); | ||
return processorResponse; | ||
} | ||
if ( processorResponse.type === PaymentProcessorResponseType.MANUAL ) { | ||
return processorResponse; | ||
} | ||
throw new Error( `Unknown payment processor response for processor "${ paymentProcessorId }"` ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters