|
| 1 | +import { UserMeResponse } from "../core/ApiSchemas"; |
| 2 | +import { COSMETICS } from "../core/CosmeticSchemas"; |
| 3 | +import { getApiBase, getAuthHeader } from "./jwt"; |
| 4 | +import { translateText } from "./Utils"; |
| 5 | + |
| 6 | +interface StripeProduct { |
| 7 | + id: string; |
| 8 | + object: "product"; |
| 9 | + active: boolean; |
| 10 | + created: number; |
| 11 | + description: string | null; |
| 12 | + images: string[]; |
| 13 | + livemode: boolean; |
| 14 | + metadata: Record<string, string>; |
| 15 | + name: string; |
| 16 | + shippable: boolean | null; |
| 17 | + type: "good" | "service"; |
| 18 | + updated: number; |
| 19 | + url: string | null; |
| 20 | + price: string; |
| 21 | + price_id: string; |
| 22 | +} |
| 23 | + |
| 24 | +export interface Pattern { |
| 25 | + name: string; |
| 26 | + key: string; |
| 27 | + roleGroup?: string; |
| 28 | + price?: string; |
| 29 | + priceId?: string; |
| 30 | + lockedReason?: string; |
| 31 | + notShown?: boolean; |
| 32 | +} |
| 33 | + |
| 34 | +export async function patterns( |
| 35 | + userMe: UserMeResponse | null, |
| 36 | +): Promise<Pattern[]> { |
| 37 | + const patterns: Pattern[] = Object.entries(COSMETICS.patterns).map( |
| 38 | + ([key, patternData]) => { |
| 39 | + return { |
| 40 | + name: patternData.name, |
| 41 | + key, |
| 42 | + roleGroup: patternData.role_group, |
| 43 | + }; |
| 44 | + }, |
| 45 | + ); |
| 46 | + |
| 47 | + const products = await listAllProducts(); |
| 48 | + patterns.forEach((pattern) => { |
| 49 | + addRestrictions(pattern, userMe, products); |
| 50 | + }); |
| 51 | + return patterns; |
| 52 | +} |
| 53 | + |
| 54 | +function addRestrictions( |
| 55 | + pattern: Pattern, |
| 56 | + userMe: UserMeResponse | null, |
| 57 | + products: Map<string, StripeProduct>, |
| 58 | +) { |
| 59 | + if (userMe === null) { |
| 60 | + if (products.has(`pattern:${pattern.name}`)) { |
| 61 | + // Purchasable (flare-gated) patterns are shown as disabled |
| 62 | + pattern.lockedReason = translateText("territory_patterns.blocked.login"); |
| 63 | + } else { |
| 64 | + // Role-gated patterns are not shown |
| 65 | + pattern.notShown = true; |
| 66 | + } |
| 67 | + return; |
| 68 | + } |
| 69 | + const flares = userMe.player.flares ?? []; |
| 70 | + if ( |
| 71 | + flares.includes("pattern:*") || |
| 72 | + flares.includes(`pattern:${pattern.name}`) |
| 73 | + ) { |
| 74 | + // Pattern is unlocked by flare |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const roles = userMe.player.roles ?? []; |
| 79 | + if (roles.some((role) => role === pattern.roleGroup)) { |
| 80 | + // Pattern is unlocked by role |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const product = products.get(`pattern:${pattern.name}`); |
| 85 | + if (product) { |
| 86 | + pattern.price = product.price; |
| 87 | + pattern.priceId = product.price_id; |
| 88 | + pattern.lockedReason = translateText("territory_patterns.blocked.purchase"); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // Pattern is locked by role group and not purchasable, don't show it. |
| 93 | + pattern.notShown = true; |
| 94 | +} |
| 95 | + |
| 96 | +export async function handlePurchase(priceId: string) { |
| 97 | + try { |
| 98 | + const response = await fetch( |
| 99 | + `${getApiBase()}/stripe/create-checkout-session`, |
| 100 | + { |
| 101 | + method: "POST", |
| 102 | + headers: { |
| 103 | + "Content-Type": "application/json", |
| 104 | + authorization: getAuthHeader(), |
| 105 | + }, |
| 106 | + body: JSON.stringify({ |
| 107 | + priceId: priceId, |
| 108 | + successUrl: `${window.location.href}purchase-success`, |
| 109 | + cancelUrl: `${window.location.href}purchase-cancel`, |
| 110 | + }), |
| 111 | + }, |
| 112 | + ); |
| 113 | + |
| 114 | + if (!response.ok) { |
| 115 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 116 | + } |
| 117 | + |
| 118 | + const { url } = await response.json(); |
| 119 | + |
| 120 | + // Redirect to Stripe checkout |
| 121 | + window.location.href = url; |
| 122 | + } catch (error) { |
| 123 | + console.error("Purchase error:", error); |
| 124 | + alert("Something went wrong. Please try again later."); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// Returns a map of flare -> product |
| 129 | +export async function listAllProducts(): Promise<Map<string, StripeProduct>> { |
| 130 | + try { |
| 131 | + const response = await fetch(`${getApiBase()}/stripe/products`); |
| 132 | + if (!response.ok) { |
| 133 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 134 | + } |
| 135 | + const products = (await response.json()) as StripeProduct[]; |
| 136 | + const productMap = new Map<string, StripeProduct>(); |
| 137 | + products.forEach((product) => { |
| 138 | + productMap.set(product.metadata.flare, product); |
| 139 | + }); |
| 140 | + return productMap; |
| 141 | + } catch (error) { |
| 142 | + console.error("Failed to fetch products:", error); |
| 143 | + return new Map(); |
| 144 | + } |
| 145 | +} |
0 commit comments