Skip to content

Commit 0333227

Browse files
authored
feat: useCart hook stripe intent support (#5)
* feat: added stripe intent support to the cart hook - This should be redesigned and a useCheckout hook created if needed support multiple payment gateways better. * chore: added changeset
1 parent c6e992b commit 0333227

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

.changeset/warm-mice-applaud.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@elasticpath/react-shopper-hooks": minor
3+
---
4+
5+
Added stripe intent support to the cart hook
6+
7+
- This is a quick fix for now and will be redesigned and a useCheckout hook created if needed support multiple payment gateways better.

packages/react-shopper-hooks/lib/cart/use-cart-hook.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CartItemsResponse,
1515
ConfirmPaymentResponse,
1616
Moltin as EPCCClient,
17+
Order,
1718
OrderBillingAddress,
1819
OrderShippingAddress,
1920
PaymentRequestBody
@@ -48,17 +49,49 @@ export function useCart() {
4849
resolveCartId,
4950
emit
5051
),
52+
stripeIntent: _stripeIntent(dispatch, resolveCartId, client, emit),
5153
checkout: _checkout(dispatch, resolveCartId, client, emit),
5254
isUpdatingCart: state.kind === "updating-cart-state",
5355
state
5456
}
5557
}
5658

59+
function _stripeIntent(
60+
dispatch: (action: CartAction) => void,
61+
resolveCartId: () => string,
62+
client: EPCCClient,
63+
_emit?: (event: StoreEvent) => void
64+
) {
65+
return async (
66+
email: string,
67+
shippingAddress: Partial<OrderShippingAddress>,
68+
sameAsShipping?: boolean,
69+
billingAddress?: Partial<OrderBillingAddress>
70+
): Promise<{ data: Order }> => {
71+
const cartId = resolveCartId()
72+
dispatch({
73+
type: "updating-cart",
74+
payload: { action: "checkout" }
75+
})
76+
const customer = `${shippingAddress.first_name} ${shippingAddress.last_name}`
77+
return await checkout(
78+
cartId,
79+
{
80+
email,
81+
name: customer
82+
},
83+
billingAddress && !sameAsShipping ? billingAddress : shippingAddress,
84+
shippingAddress,
85+
client
86+
)
87+
}
88+
}
89+
5790
function _checkout(
5891
dispatch: (action: CartAction) => void,
5992
resolveCartId: () => string,
6093
client: EPCCClient,
61-
emit?: (event: StoreEvent) => void
94+
_emit?: (event: StoreEvent) => void
6295
) {
6396
return async (
6497
email: string,

0 commit comments

Comments
 (0)