@cohostvip/cohost-react 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,6 +5,8 @@ export type PaymentElementProviderProps = {
|
|
|
5
5
|
};
|
|
6
6
|
export type PaymentElementContextType = {
|
|
7
7
|
tokenizeCard: (cardInfo: CreditCardInformation) => Promise<any>;
|
|
8
|
+
paymentIntent: any | null;
|
|
9
|
+
isLoading: boolean;
|
|
8
10
|
};
|
|
9
11
|
export declare const PaymentElementProvider: React.FC<PaymentElementProviderProps>;
|
|
10
12
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PaymentElementContext.d.ts","sourceRoot":"","sources":["../../src/context/PaymentElementContext.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"PaymentElementContext.d.ts","sourceRoot":"","sources":["../../src/context/PaymentElementContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAiE,MAAM,OAAO,CAAC;AAGtF,OAAO,EAAE,qBAAqB,EAAa,MAAM,yBAAyB,CAAC;AAG3E,MAAM,MAAM,2BAA2B,GAAG;IACtC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACpC,YAAY,EAAE,CAAC,QAAQ,EAAE,qBAAqB,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IAChE,aAAa,EAAE,GAAG,GAAG,IAAI,CAAC;IAC1B,SAAS,EAAE,OAAO,CAAC;CACtB,CAAC;AAUF,eAAO,MAAM,sBAAsB,EAAE,KAAK,CAAC,EAAE,CAAC,2BAA2B,CA2ExE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAO,yBAIpC,CAAC"}
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { createContext, useContext, useEffect, useState, useRef } from 'react';
|
|
3
3
|
import { useCohostCheckout } from './CohostCheckoutContext';
|
|
4
|
+
import { useCohostClient } from './CohostContext';
|
|
4
5
|
import { authnetTokenizer } from '../lib/tokenizers/authnet';
|
|
5
6
|
const PaymentElementContext = createContext(null);
|
|
6
7
|
const tokenizers = {
|
|
7
8
|
'authnet': authnetTokenizer
|
|
8
9
|
};
|
|
9
10
|
export const PaymentElementProvider = ({ children }) => {
|
|
10
|
-
const { cartSession } = useCohostCheckout();
|
|
11
|
+
const { cartSession, cartSessionId } = useCohostCheckout();
|
|
12
|
+
const { client } = useCohostClient();
|
|
11
13
|
const [paymentIntent, setPaymentIntent] = useState(null);
|
|
12
14
|
const [tokenizer, setTokenizer] = useState(null);
|
|
15
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
16
|
+
const fetchAttemptedRef = useRef(false);
|
|
13
17
|
const tokenizeCard = async (cardInfo) => {
|
|
14
18
|
if (!tokenizer) {
|
|
15
19
|
throw new Error("Tokenizer not found");
|
|
16
20
|
}
|
|
17
|
-
const paymentIntent = cartSession?.meta?.paymentIntent;
|
|
18
21
|
if (!paymentIntent) {
|
|
19
22
|
throw new Error("Payment intent not found");
|
|
20
23
|
}
|
|
@@ -32,10 +35,35 @@ export const PaymentElementProvider = ({ children }) => {
|
|
|
32
35
|
}
|
|
33
36
|
}, [paymentIntent]);
|
|
34
37
|
useEffect(() => {
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
if (!cartSession) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// If cart session has payment intent, use it
|
|
42
|
+
if (cartSession.meta?.paymentIntent) {
|
|
43
|
+
setPaymentIntent(cartSession.meta.paymentIntent);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// Only fetch once per mount
|
|
47
|
+
if (fetchAttemptedRef.current) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
fetchAttemptedRef.current = true;
|
|
51
|
+
setIsLoading(true);
|
|
52
|
+
client.cart.getPaymentIntent(cartSessionId)
|
|
53
|
+
.then((result) => {
|
|
54
|
+
setPaymentIntent(result);
|
|
55
|
+
})
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
console.error("Error fetching payment intent:", error);
|
|
58
|
+
})
|
|
59
|
+
.finally(() => {
|
|
60
|
+
setIsLoading(false);
|
|
61
|
+
});
|
|
62
|
+
}, [cartSession, cartSessionId, client]);
|
|
37
63
|
return (_jsx(PaymentElementContext.Provider, { value: {
|
|
38
|
-
tokenizeCard
|
|
64
|
+
tokenizeCard,
|
|
65
|
+
paymentIntent,
|
|
66
|
+
isLoading,
|
|
39
67
|
}, children: children }));
|
|
40
68
|
};
|
|
41
69
|
/**
|