-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add payment processing using ECE on the Blocks checkout and cart pages (
- Loading branch information
1 parent
08f4852
commit ef195c0
Showing
22 changed files
with
1,456 additions
and
548 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Add payment processing using ECE in the Blocks checkout and cart pages. |
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
52 changes: 52 additions & 0 deletions
52
client/express-checkout/blocks/components/express-checkout-component.js
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,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { ExpressCheckoutElement } from '@stripe/react-stripe-js'; | ||
import { shippingAddressChangeHandler } from '../../event-handlers'; | ||
import { useExpressCheckout } from '../hooks/use-express-checkout'; | ||
|
||
/** | ||
* ExpressCheckout express payment method component. | ||
* | ||
* @param {Object} props PaymentMethodProps. | ||
* | ||
* @return {ReactNode} Stripe Elements component. | ||
*/ | ||
const ExpressCheckoutComponent = ( { | ||
api, | ||
billing, | ||
shippingData, | ||
setExpressPaymentError, | ||
onClick, | ||
onClose, | ||
} ) => { | ||
const { | ||
buttonOptions, | ||
onButtonClick, | ||
onConfirm, | ||
onCancel, | ||
} = useExpressCheckout( { | ||
api, | ||
billing, | ||
shippingData, | ||
onClick, | ||
onClose, | ||
setExpressPaymentError, | ||
} ); | ||
|
||
const onShippingAddressChange = ( event ) => { | ||
shippingAddressChangeHandler( api, event ); | ||
}; | ||
|
||
return ( | ||
<ExpressCheckoutElement | ||
options={ buttonOptions } | ||
onClick={ onButtonClick } | ||
onConfirm={ onConfirm } | ||
onCancel={ onCancel } | ||
onShippingAddressChange={ onShippingAddressChange } | ||
/> | ||
); | ||
}; | ||
|
||
export default ExpressCheckoutComponent; |
28 changes: 28 additions & 0 deletions
28
client/express-checkout/blocks/components/express-checkout-container.js
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,28 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Elements } from '@stripe/react-stripe-js'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ExpressCheckoutComponent from './express-checkout-component'; | ||
|
||
const ExpressCheckoutContainer = ( props ) => { | ||
const { stripe, billing } = props; | ||
|
||
const options = { | ||
mode: 'payment', | ||
paymentMethodCreation: 'manual', | ||
amount: billing.cartTotal.value, | ||
currency: billing.currency.code.toLowerCase(), | ||
}; | ||
|
||
return ( | ||
<Elements stripe={ stripe } options={ options }> | ||
<ExpressCheckoutComponent { ...props } /> | ||
</Elements> | ||
); | ||
}; | ||
|
||
export default ExpressCheckoutContainer; |
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
client/express-checkout/blocks/hooks/use-express-checkout.js
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,93 @@ | ||
/* global wcpayExpressCheckoutParams */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { useCallback } from '@wordpress/element'; | ||
import { useStripe, useElements } from '@stripe/react-stripe-js'; | ||
import { normalizeLineItems } from 'wcpay/express-checkout/utils'; | ||
import { onConfirmHandler } from 'wcpay/express-checkout/event-handlers'; | ||
|
||
export const useExpressCheckout = ( { | ||
api, | ||
billing, | ||
shippingData, | ||
onClick, | ||
onClose, | ||
setExpressPaymentError, | ||
} ) => { | ||
const stripe = useStripe(); | ||
const elements = useElements(); | ||
|
||
const buttonOptions = { | ||
paymentMethods: { | ||
applePay: 'always', | ||
googlePay: 'always', | ||
link: 'auto', | ||
}, | ||
buttonType: { | ||
googlePay: wcpayExpressCheckoutParams.button.type, | ||
applePay: wcpayExpressCheckoutParams.button.type, | ||
}, | ||
}; | ||
|
||
const onCancel = () => { | ||
onClose(); | ||
}; | ||
|
||
const completePayment = ( redirectUrl ) => { | ||
window.location = redirectUrl; | ||
}; | ||
|
||
const abortPayment = ( onConfirmEvent, message ) => { | ||
onConfirmEvent.paymentFailed( 'fail' ); | ||
setExpressPaymentError( message ); | ||
}; | ||
|
||
const onButtonClick = useCallback( | ||
( event ) => { | ||
const options = { | ||
lineItems: normalizeLineItems( billing?.cartTotalItems ), | ||
emailRequired: true, | ||
shippingAddressRequired: shippingData?.needsShipping, | ||
phoneNumberRequired: | ||
wcpayExpressCheckoutParams?.checkout?.needs_payer_phone, | ||
shippingRates: shippingData?.shippingRates[ 0 ]?.shipping_rates?.map( | ||
( r ) => { | ||
return { | ||
id: r.rate_id, | ||
amount: parseInt( r.price, 10 ), | ||
displayName: r.name, | ||
}; | ||
} | ||
), | ||
}; | ||
event.resolve( options ); | ||
onClick(); | ||
}, | ||
[ | ||
onClick, | ||
billing.cartTotalItems, | ||
shippingData.needsShipping, | ||
shippingData.shippingRates, | ||
] | ||
); | ||
|
||
const onConfirm = async ( event ) => { | ||
onConfirmHandler( | ||
api, | ||
stripe, | ||
elements, | ||
completePayment, | ||
abortPayment, | ||
event | ||
); | ||
}; | ||
|
||
return { | ||
buttonOptions, | ||
onButtonClick, | ||
onConfirm, | ||
onCancel, | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizeOrderData, normalizeShippingAddress } from './utils'; | ||
import { getErrorMessageFromNotice } from 'utils/express-checkout'; | ||
|
||
export const shippingAddressChangeHandler = async ( api, event ) => { | ||
const response = await api.expressCheckoutECECalculateShippingOptions( | ||
normalizeShippingAddress( event.shippingAddress ) | ||
); | ||
event.resolve( { | ||
shippingRates: response.shipping_options, | ||
} ); | ||
}; | ||
|
||
export const onConfirmHandler = async ( | ||
api, | ||
stripe, | ||
elements, | ||
completePayment, | ||
abortPayment, | ||
event | ||
) => { | ||
const { paymentMethod, error } = await stripe.createPaymentMethod( { | ||
elements, | ||
} ); | ||
|
||
if ( error ) { | ||
abortPayment( event, error.message ); | ||
return; | ||
} | ||
|
||
// Kick off checkout processing step. | ||
const createOrderResponse = await api.expressCheckoutECECreateOrder( | ||
normalizeOrderData( event, paymentMethod.id ) | ||
); | ||
|
||
if ( createOrderResponse.result !== 'success' ) { | ||
return abortPayment( | ||
event, | ||
getErrorMessageFromNotice( createOrderResponse.messages ) | ||
); | ||
} | ||
|
||
try { | ||
const confirmationRequest = api.confirmIntent( | ||
createOrderResponse.redirect | ||
); | ||
|
||
// `true` means there is no intent to confirm. | ||
if ( confirmationRequest === true ) { | ||
completePayment( createOrderResponse.redirect ); | ||
} else { | ||
const redirectUrl = await confirmationRequest; | ||
|
||
completePayment( redirectUrl ); | ||
} | ||
} catch ( e ) { | ||
abortPayment( event, error.message ); | ||
} | ||
}; |
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 @@ | ||
export * from './normalize'; |
Oops, something went wrong.