Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Feb 8, 2025
1 parent 478d8bb commit cc37d7c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
24 changes: 24 additions & 0 deletions src/purchases/CartUtil.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
}
38 changes: 30 additions & 8 deletions src/purchases/CheckoutTab.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLElement>('#pseudoCoins > #cartContainer')!
const form = tab.querySelector('div.cartList')!
Expand Down Expand Up @@ -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: {
Expand All @@ -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}`)
}

Expand Down Expand Up @@ -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()
}
},

Expand All @@ -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
}
}
}
18 changes: 12 additions & 6 deletions src/purchases/UpgradesSubtab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ async function purchaseUpgrade (upgrades: Map<number, UpgradesList>) {

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 {
Expand Down Expand Up @@ -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
}

0 comments on commit cc37d7c

Please sign in to comment.