diff --git a/src/purchases/CartUtil.ts b/src/purchases/CartUtil.ts index 8bfe88800..55a60623b 100644 --- a/src/purchases/CartUtil.ts +++ b/src/purchases/CartUtil.ts @@ -1,3 +1,4 @@ +import { loadScript, type PayPalNamespace } from '@paypal/paypal-js' import type { Product } from './CartTab' /** A map of all products in the cart. */ @@ -52,6 +53,17 @@ export const removeFromCart = (id: string) => { updateInCartCount() } +export const clearCart = () => { + for (const [id, product] of cartMap.entries()) { + if (product.quantity > 0) { + product.quantity = 1 /* removeFromCart decrements quantity */ + removeFromCart(id) + } + } + + updateInCartCount() +} + /** * Returns the price of everything in the cart. */ @@ -82,3 +94,15 @@ export const getProductsInCart = () => { return temp } + +let paypal: PayPalNamespace + +export const loadPayPal = async () => { + paypal ??= (await loadScript({ + clientId: 'AYaEpUZfchj2DRdTZJm0ukzxyXGQIHorqy3q1axPQ8RCpiRqkYqg23NiRRYtHptYBRBAyCTL28yEwtb9', + enableFunding: ['venmo'], + disableFunding: ['paylater', 'credit', 'card'] + }))! + + return paypal +} diff --git a/src/purchases/CheckoutTab.ts b/src/purchases/CheckoutTab.ts index 924cffb37..3377fb4be 100644 --- a/src/purchases/CheckoutTab.ts +++ b/src/purchases/CheckoutTab.ts @@ -1,10 +1,10 @@ -import { loadScript } from '@paypal/paypal-js' import { prod } from '../Config' import { changeSubTab, Tabs } from '../Tabs' import { Alert, Notification } from '../UpdateHTML' import { memoize } from '../Utility' import { products, subscriptionProducts } from './CartTab' -import { addToCart, getPrice, getProductsInCart, getQuantity, removeFromCart } from './CartUtil' +import { addToCart, clearCart, getPrice, getProductsInCart, getQuantity, loadPayPal, removeFromCart } from './CartUtil' +import { updatePseudoCoins } from './UpgradesSubtab' const tab = document.querySelector('#pseudoCoins > #cartContainer')! const form = tab.querySelector('div.cartList')! @@ -163,11 +163,7 @@ const updateTotalPriceInCart = () => { async function initializePayPal () { try { - const paypal = await loadScript({ - clientId: 'AYaEpUZfchj2DRdTZJm0ukzxyXGQIHorqy3q1axPQ8RCpiRqkYqg23NiRRYtHptYBRBAyCTL28yEwtb9', - enableFunding: ['venmo'], - disableFunding: ['paylater', 'credit', 'card'] - }) + const paypal = await loadPayPal() paypal?.Buttons?.({ style: { @@ -181,6 +177,10 @@ async function initializePayPal () { const fd = new FormData() for (const product of getProductsInCart()) { + if (product.quantity > 0 && product.subscription) { + throw new TypeError('skipping') + } + fd.set(product.id, `${product.quantity}`) } @@ -235,8 +235,14 @@ async function initializePayPal () { ?.authorizations?.[0] Notification( - `Transaction ${transaction.status}: ${transaction.id}. See console for all available details` + `Transaction ${transaction.status}: ${transaction.id}. Please give us a few minutes to process it.` ) + + clearCart() + updateItemList() + updateTotalPriceInCart() + + exponentialPseudoCoinBalanceCheck() } }, @@ -247,3 +253,19 @@ async function initializePayPal () { }).render('#checkout-paypal') } catch {} } + +const sleep = (delay: number) => new Promise((r) => setTimeout(r, delay)) + +async function exponentialPseudoCoinBalanceCheck () { + const delays = [0, 30_000, 60_000, 120_000, 180_000, 240_000, 300_000] + const lastCoinAmount = 0 + + for (const delay of delays) { + await sleep(delay) + const coins = await updatePseudoCoins() + + if (lastCoinAmount !== coins) { + break + } + } +} diff --git a/src/purchases/UpgradesSubtab.ts b/src/purchases/UpgradesSubtab.ts index d147a8968..f0cb8a326 100644 --- a/src/purchases/UpgradesSubtab.ts +++ b/src/purchases/UpgradesSubtab.ts @@ -125,12 +125,7 @@ async function purchaseUpgrade (upgrades: Map) { setActiveUpgrade(upgrade) - const response = await fetch('https://synergism.cc/stripe/coins') - const coins = await response.json() as CoinsResponse - - tab!.querySelector('#pseudoCoinAmounts > #currentCoinBalance')!.innerHTML = `${ - i18next.t('pseudoCoins.coinCount', { amount: Intl.NumberFormat().format(coins.coins) }) - }` + await updatePseudoCoins() updatePCoinCache(upgrade.internalName, parsed.data.level) } else { @@ -205,3 +200,14 @@ export const toggleUpgradeSubtab = () => { export const clearUpgradeSubtab = () => { tab.style.display = 'none' } + +export const updatePseudoCoins = async () => { + const response = await fetch('https://synergism.cc/stripe/coins') + const coins = await response.json() as CoinsResponse + + tab!.querySelector('#pseudoCoinAmounts > #currentCoinBalance')!.innerHTML = `${ + i18next.t('pseudoCoins.coinCount', { amount: Intl.NumberFormat().format(coins.coins) }) + }` + + return coins.coins +}