|
| 1 | +import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client'; |
| 2 | + |
1 | 3 | import { getSessionCustomerAccessToken } from '~/auth'; |
2 | 4 | import { client } from '~/client'; |
3 | 5 | import { FragmentOf, graphql, VariablesOf } from '~/client/graphql'; |
4 | 6 | import { getShippingZones } from '~/client/management/get-shipping-zones'; |
5 | 7 | import { TAGS } from '~/client/tags'; |
| 8 | +import { getPreferredCurrencyCode } from '~/lib/currency'; |
6 | 9 |
|
7 | 10 | export const PhysicalItemFragment = graphql(` |
8 | 11 | fragment PhysicalItemFragment on CartPhysicalItem { |
@@ -262,6 +265,128 @@ export const getCart = async (variables: Variables) => { |
262 | 265 | return data; |
263 | 266 | }; |
264 | 267 |
|
| 268 | +const PaymentWalletsQuery = graphql(` |
| 269 | + query PaymentWalletsQuery($filters: PaymentWalletsFilterInput) { |
| 270 | + site { |
| 271 | + paymentWallets(filter: $filters) { |
| 272 | + edges { |
| 273 | + node { |
| 274 | + entityId |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | +`); |
| 281 | + |
| 282 | +type PaymentWalletsVariables = VariablesOf<typeof PaymentWalletsQuery>; |
| 283 | + |
| 284 | +export const getPaymentWallets = async (variables: PaymentWalletsVariables) => { |
| 285 | + const customerAccessToken = await getSessionCustomerAccessToken(); |
| 286 | + |
| 287 | + const { data } = await client.fetch({ |
| 288 | + document: PaymentWalletsQuery, |
| 289 | + customerAccessToken, |
| 290 | + fetchOptions: { cache: 'no-store' }, |
| 291 | + variables, |
| 292 | + }); |
| 293 | + |
| 294 | + return removeEdgesAndNodes(data.site.paymentWallets).map(({ entityId }) => entityId); |
| 295 | +}; |
| 296 | + |
| 297 | +const PaymentWalletWithInitializationDataQuery = graphql(` |
| 298 | + query PaymentWalletWithInitializationDataQuery($entityId: String!, $cartId: String!) { |
| 299 | + site { |
| 300 | + paymentWalletWithInitializationData( |
| 301 | + filter: { paymentWalletEntityId: $entityId, cartEntityId: $cartId } |
| 302 | + ) { |
| 303 | + clientToken |
| 304 | + initializationData |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | +`); |
| 309 | + |
| 310 | +export const getPaymentWalletWithInitializationData = async (entityId: string, cartId: string) => { |
| 311 | + const { data } = await client.fetch({ |
| 312 | + document: PaymentWalletWithInitializationDataQuery, |
| 313 | + variables: { |
| 314 | + entityId, |
| 315 | + cartId, |
| 316 | + }, |
| 317 | + customerAccessToken: await getSessionCustomerAccessToken(), |
| 318 | + fetchOptions: { cache: 'no-store' }, |
| 319 | + }); |
| 320 | + |
| 321 | + return data.site.paymentWalletWithInitializationData; |
| 322 | +}; |
| 323 | + |
| 324 | +const CurrencyQuery = graphql(` |
| 325 | + query Currency($currencyCode: currencyCode!) { |
| 326 | + site { |
| 327 | + currency(currencyCode: $currencyCode) { |
| 328 | + display { |
| 329 | + decimalPlaces |
| 330 | + symbol |
| 331 | + } |
| 332 | + name |
| 333 | + code |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | +`); |
| 338 | + |
| 339 | +export const getCurrencyData = async (currencyCode?: string) => { |
| 340 | + const code = await getPreferredCurrencyCode(currencyCode); |
| 341 | + |
| 342 | + if (!code) { |
| 343 | + throw new Error('Could not get currency code'); |
| 344 | + } |
| 345 | + |
| 346 | + const customerAccessToken = await getSessionCustomerAccessToken(); |
| 347 | + |
| 348 | + const { data } = await client.fetch({ |
| 349 | + document: CurrencyQuery, |
| 350 | + fetchOptions: { cache: 'no-store' }, |
| 351 | + variables: { |
| 352 | + currencyCode: code, |
| 353 | + }, |
| 354 | + customerAccessToken, |
| 355 | + }); |
| 356 | + |
| 357 | + return data.site.currency; |
| 358 | +}; |
| 359 | + |
| 360 | +export const createWalletButtonsInitOptions = async ( |
| 361 | + walletButtons: string[], |
| 362 | + cart: { |
| 363 | + entityId: string; |
| 364 | + currencyCode: string; |
| 365 | + }, |
| 366 | +) => { |
| 367 | + const currencyData = await getCurrencyData(cart.currencyCode); |
| 368 | + |
| 369 | + return Promise.all( |
| 370 | + walletButtons.map(async (entityId) => { |
| 371 | + const initData = await getPaymentWalletWithInitializationData(entityId, cart.entityId); |
| 372 | + const methodId = entityId.split('.').join(''); |
| 373 | + |
| 374 | + return { |
| 375 | + methodId, |
| 376 | + containerId: `${methodId}-button`, |
| 377 | + [methodId]: { |
| 378 | + cartId: cart.entityId, |
| 379 | + currency: { |
| 380 | + code: currencyData?.code, |
| 381 | + decimalPlaces: currencyData?.display.decimalPlaces, |
| 382 | + }, |
| 383 | + ...initData, |
| 384 | + }, |
| 385 | + }; |
| 386 | + }), |
| 387 | + ); |
| 388 | +}; |
| 389 | + |
265 | 390 | export const getShippingCountries = async (geography: FragmentOf<typeof GeographyFragment>) => { |
266 | 391 | const hasAccessToken = Boolean(process.env.BIGCOMMERCE_ACCESS_TOKEN); |
267 | 392 | const shippingZones = hasAccessToken ? await getShippingZones() : []; |
|
0 commit comments