forked from SalesforceCommerceCloud/pwa-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-standalone-payment-methods.js
More file actions
54 lines (47 loc) · 1.86 KB
/
use-standalone-payment-methods.js
File metadata and controls
54 lines (47 loc) · 1.86 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
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {useState, useEffect} from 'react'
import {AdyenPaymentMethodsService} from '@salesforce/retail-react-app/app/components/apple-pay-express/utils/payment-methods'
/**
* Hook for fetching payment methods without basket dependency (for "Buy Now" flows)
* @param {string} authToken - Authentication token
* @param {object} site - Site configuration
* @param {object} locale - Locale configuration
* @param {boolean} enabled - Whether the hook should make API calls (default: true)
* @returns {object} Payment methods data, loading state, and error
*/
export const useStandalonePaymentMethods = (authToken, site, locale, enabled = true) => {
const [paymentMethods, setPaymentMethods] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
useEffect(() => {
// Only make API call if enabled and required parameters are available
if (!enabled || !authToken || !site) {
return
}
const fetchPaymentMethods = async () => {
try {
setLoading(true)
setError(null)
const service = new AdyenPaymentMethodsService(authToken, site)
const data = await service.getPaymentMethods()
setPaymentMethods(data)
} catch (err) {
console.error('Error fetching standalone payment methods:', err)
setError(err)
} finally {
setLoading(false)
}
}
fetchPaymentMethods()
}, [authToken, site, locale, enabled])
return {
paymentMethods,
loading,
error
}
}