-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathuseStripePaymentBySetupIntentId.tsx
More file actions
88 lines (78 loc) · 2.42 KB
/
useStripePaymentBySetupIntentId.tsx
File metadata and controls
88 lines (78 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useSetPaymentByStripeIntent } from "Apps/Order/Mutations/useSetPaymentByStripeIntentMutation"
import { useRouter } from "System/Hooks/useRouter"
import { useEffect, useState } from "react"
/*
* Hook to handle Stripe redirect for newly-linked bank account
* pulls necessary params from Stripe redirect URL and sets payment by intentId
*/
export function useStripePaymentBySetupIntentId(orderId: string) {
const { submitMutation: setPaymentByStripeIntentMutation } =
useSetPaymentByStripeIntent()
const { match } = useRouter()
const [isProcessingRedirect, setIsProcessingRedirect] = useState(false)
const [stripeSetupIntentId, setStripeSetupIntentId] = useState<null | string>(
null,
)
const [isPaymentSetupSuccessful, setIsPaymentSetupSuccessful] =
useState(false)
const [paymentSetupError, setPaymentSetupError] = useState<null | object>(
null,
)
// biome-ignore lint/correctness/useExhaustiveDependencies: ignored using `--suppress`
useEffect(() => {
// pull necessary params from Stripe redirect URL
const {
save_account,
setup_intent,
setup_intent_client_secret,
redirect_status,
} = match.location.query
let oneTimeUse = false
if (
save_account === "false" ||
(typeof save_account === "boolean" && !save_account)
) {
oneTimeUse = true
}
if (
setup_intent &&
setup_intent_client_secret &&
redirect_status === "succeeded"
) {
setIsProcessingRedirect(true)
setStripeSetupIntentId(setup_intent)
// set payment with new bank account by Setup Intent ID
setPaymentBySetupIntentId(setup_intent, oneTimeUse)
}
setIsProcessingRedirect(false)
}, [orderId, match])
const setPaymentBySetupIntentId = async (
setupIntentId: string,
oneTimeUse: boolean,
) => {
try {
const orderOrError = (
await setPaymentByStripeIntentMutation({
variables: {
input: {
id: orderId,
oneTimeUse,
setupIntentId,
},
},
})
).commerceSetPaymentByStripeIntent?.orderOrError
if (orderOrError?.error) throw orderOrError.error
setIsPaymentSetupSuccessful(true)
} catch (error) {
setIsPaymentSetupSuccessful(false)
setPaymentSetupError(error)
}
}
return {
isProcessingRedirect,
stripeSetupIntentId,
isPaymentSetupSuccessful,
paymentSetupError,
}
}