Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide multiple locales and currencies #12

Merged
merged 7 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion adyen/context/adyen-checkout-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import PropTypes from 'prop-types'
import {useAccessToken, useCustomerId} from '@salesforce/commerce-sdk-react'
import {AdyenSessionsService} from '../services/sessions'
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
import { resolveLocaleFromUrl } from "@salesforce/retail-react-app/app/utils/site-utils";

const AdyenCheckoutContext = React.createContext()

export const AdyenCheckoutProvider = ({children}) => {
const {data: basket} = useCurrentBasket()
const {getTokenWhenReady} = useAccessToken()
const customerId = useCustomerId()
const locale = resolveLocaleFromUrl(`${window.location.pathname}${window.location.search}`)

const [adyenSession, setAdyenSession] = useState()
const [adyenStateData, setAdyenStateData] = useState()
Expand All @@ -19,7 +21,7 @@ export const AdyenCheckoutProvider = ({children}) => {
const token = await getTokenWhenReady()
const adyenSessionsService = new AdyenSessionsService(token)
try {
const data = await adyenSessionsService.createSession(customerId)
const data = await adyenSessionsService.createSession(customerId, locale)
setAdyenSession(data?.length ? data[0] : {error: true})
} catch (error) {
setAdyenSession({error})
Expand Down
1 change: 1 addition & 0 deletions adyen/controllers/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async function create(req, res) {
})

const response = await checkout.sessions({
shopperLocale: req.body?.locale?.id,
countryCode: countryCode,
amount: {
value: getCurrencyValueForApi(orderTotal, currency),
Expand Down
1 change: 1 addition & 0 deletions adyen/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class ApiClient {
base(method, options) {
return fetch(this.url, {
method: method,
body: options?.body || null,
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${this.token}`,
Expand Down
5 changes: 4 additions & 1 deletion adyen/services/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export class AdyenSessionsService {
this.apiClient = new ApiClient(this.baseUrl, token)
}

async createSession(customerId) {
async createSession(customerId, locale) {
const res = await this.apiClient.post({
body: JSON.stringify({
locale
}),
headers: {
customerid: customerId
}
Expand Down
2 changes: 1 addition & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
// Determine where the siteRef is located. Valid values include 'path|query_param|none'. Defaults to: 'none'
// site: 'none',
// Determine where the localeRef is located. Valid values include 'path|query_param|none'. Defaults to: 'none'
locale: 'none'
locale: 'query_param'
// This boolean value dictates whether or not default site or locale values are shown in the url. Defaults to: false
// showDefaults: true
},
Expand Down
15 changes: 13 additions & 2 deletions config/sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ module.exports = [
{
id: 'RefArch',
l10n: {
supportedCurrencies: ['USD'],
supportedCurrencies: ['USD', 'EUR', 'BRL', 'JPY'],
defaultCurrency: 'USD',
defaultLocale: 'en-US',
supportedLocales: [
{
id: 'en-US',
// alias: 'us',
preferredCurrency: 'USD'
},
{
id: 'fr-FR',
preferredCurrency: 'EUR'
},
{
id: 'pt-BR',
preferredCurrency: 'BRL'
},
{
id: 'ja-JP',
preferredCurrency: 'JPY'
}
]
}
Expand Down