Skip to content

Commit 2555f9b

Browse files
committed
actually fix discount codes
1 parent b8428e9 commit 2555f9b

5 files changed

Lines changed: 72 additions & 172 deletions

File tree

api/ApiHelper.tsx

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -989,120 +989,6 @@ export function initAPI(returnSSRResponse: boolean = false): API {
989989
})
990990
}
991991

992-
let stripePurchase = (productId: string, coinAmount?: number): Promise<PaymentResponse> => {
993-
return new Promise((resolve, reject) => {
994-
let googleId = sessionStorage.getItem('googleId')
995-
if (!googleId) {
996-
toast.error('You need to be logged in to purchase something.')
997-
reject()
998-
return
999-
}
1000-
1001-
let data = {
1002-
userId: googleId,
1003-
productId: productId
1004-
}
1005-
1006-
httpApi.sendApiRequest(
1007-
{
1008-
type: RequestType.STRIPE_PAYMENT_SESSION,
1009-
requestMethod: 'POST',
1010-
requestHeader: {
1011-
GoogleToken: data.userId,
1012-
'Content-Type': 'application/json'
1013-
},
1014-
data: data.productId,
1015-
resolve: (data: any) => {
1016-
resolve(parsePaymentResponse(data))
1017-
},
1018-
reject: (error: any) => {
1019-
apiErrorHandler(RequestType.STRIPE_PAYMENT_SESSION, error, data)
1020-
reject(error)
1021-
}
1022-
},
1023-
JSON.stringify({
1024-
coinAmount
1025-
})
1026-
)
1027-
})
1028-
}
1029-
1030-
let paypalPurchase = (productId: string, coinAmount?: number): Promise<PaymentResponse> => {
1031-
return new Promise((resolve, reject) => {
1032-
let googleId = sessionStorage.getItem('googleId')
1033-
if (!googleId) {
1034-
toast.error('You need to be logged in to purchase something.')
1035-
reject()
1036-
return
1037-
}
1038-
1039-
let data = {
1040-
userId: googleId,
1041-
productId: productId
1042-
}
1043-
1044-
httpApi.sendApiRequest(
1045-
{
1046-
type: RequestType.PAYPAL_PAYMENT,
1047-
requestMethod: 'POST',
1048-
data: data.productId,
1049-
requestHeader: {
1050-
GoogleToken: data.userId,
1051-
'Content-Type': 'application/json'
1052-
},
1053-
resolve: (response: any) => {
1054-
resolve(parsePaymentResponse(response))
1055-
},
1056-
reject: (error: any) => {
1057-
apiErrorHandler(RequestType.PAYPAL_PAYMENT, error, data)
1058-
reject(error)
1059-
}
1060-
},
1061-
JSON.stringify({
1062-
coinAmount
1063-
})
1064-
)
1065-
})
1066-
}
1067-
1068-
let lemonsqueezyPurchase = (productId: string, coinAmount?: number): Promise<PaymentResponse> => {
1069-
return new Promise((resolve, reject) => {
1070-
let googleId = sessionStorage.getItem('googleId')
1071-
if (!googleId) {
1072-
toast.error('You need to be logged in to purchase something.')
1073-
reject()
1074-
return
1075-
}
1076-
1077-
let data = {
1078-
userId: googleId,
1079-
productId: productId
1080-
}
1081-
1082-
httpApi.sendApiRequest(
1083-
{
1084-
type: RequestType.LEMONSQUEEZY_PAYMENT,
1085-
requestMethod: 'POST',
1086-
data: data.productId,
1087-
requestHeader: {
1088-
GoogleToken: data.userId,
1089-
'Content-Type': 'application/json'
1090-
},
1091-
resolve: (response: any) => {
1092-
resolve(parsePaymentResponse(response))
1093-
},
1094-
reject: (error: any) => {
1095-
apiErrorHandler(RequestType.LEMONSQUEEZY_PAYMENT, error, data)
1096-
reject(error)
1097-
}
1098-
},
1099-
JSON.stringify({
1100-
coinAmount
1101-
})
1102-
)
1103-
})
1104-
}
1105-
1106992
let purchaseWithCoflcoins = (productId: string, googleToken: string, count?: number): Promise<void> => {
1107993
return new Promise((resolve, reject) => {
1108994
let data = {
@@ -2508,7 +2394,6 @@ export function initAPI(returnSSRResponse: boolean = false): API {
25082394
unsubscribe,
25092395
getNotificationListener,
25102396
loginWithToken,
2511-
stripePurchase,
25122397
getRecentAuctions,
25132398
getFlips,
25142399
subscribeFlips,
@@ -2522,8 +2407,6 @@ export function initAPI(returnSSRResponse: boolean = false): API {
25222407
getEndedAuctions,
25232408
getNewAuctions,
25242409
getFlipBasedAuctions,
2525-
paypalPurchase,
2526-
lemonsqueezyPurchase,
25272410
getRefInfo,
25282411
setRef,
25292412
getActiveAuctions,

components/CoflCoins/CoflCoinPaymentSelection.tsx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ interface CoflCoinOption {
1919
googlePlayProductId: string
2020
}
2121

22+
export interface PurchaseCodes {
23+
creatorCode?: string
24+
discountCode?: string
25+
}
26+
2227
interface Props {
2328
selectedOption: CoflCoinOption
2429
onBack: () => void
2530
countryCode?: string
2631
coflCoins: number
27-
onPayPalPay: (productId: string, coflCoins?: number) => void
28-
onStripePay: (productId: string, coflCoins?: number) => void
29-
onLemonSqueezyPay: (productId: string, coflCoins?: number) => void
32+
onPayPalPay: (productId: string, coflCoins?: number, codes?: PurchaseCodes) => void
33+
onStripePay: (productId: string, coflCoins?: number, codes?: PurchaseCodes) => void
34+
onLemonSqueezyPay: (productId: string, coflCoins?: number, codes?: PurchaseCodes) => void
3035
onGooglePlayPay: (productId: string, coflCoins?: number) => void
3136
loadingProductId: string
3237
isGooglePlayAvailable?: boolean
@@ -137,15 +142,15 @@ function CoflCoinPaymentSelection({ selectedOption, onBack, countryCode, coflCoi
137142
}
138143

139144
const getProviderForComponent = (providerSlug: string, productSlug: string) => {
140-
return getProvider(pricingData, providerSlug, productSlug)
145+
return getProvider(pricingData, productSlug, providerSlug)
141146
}
142147

143148
const getProviderPriceForComponent = (providerSlug: string, productSlug: string): number | null => {
144-
return getProviderPrice(pricingData, providerSlug, productSlug)
149+
return getProviderPrice(pricingData, productSlug, providerSlug)
145150
}
146151

147152
const getProviderOriginalPriceForComponent = (providerSlug: string, productSlug: string): number | null => {
148-
return getProviderOriginalPrice(pricingData, providerSlug, productSlug)
153+
return getProviderOriginalPrice(pricingData, productSlug, providerSlug)
149154
}
150155

151156
const getGooglePlayProductId = (): string => {
@@ -195,17 +200,11 @@ function CoflCoinPaymentSelection({ selectedOption, onBack, countryCode, coflCoi
195200
return undefined
196201
}
197202

198-
// Append creator/discount codes as query params to a product slug so they reach the checkout endpoint
199-
const buildProductIdWithCodes = (productId: string): string => {
200-
const queryParams: string[] = []
201-
if (appliedCreatorCode) {
202-
queryParams.push(`creatorCode=${encodeURIComponent(appliedCreatorCode)}`)
203-
}
204-
if (validatedDiscount?.code) {
205-
queryParams.push(`discountCode=${encodeURIComponent(validatedDiscount.code)}`)
206-
}
207-
return queryParams.length > 0 ? `${productId}?${queryParams.join('&')}` : productId
208-
}
203+
// Codes to forward in the checkout request body (topup endpoints read them from TopUpArguments, not the URL)
204+
const getPurchaseCodes = (): PurchaseCodes => ({
205+
creatorCode: appliedCreatorCode || undefined,
206+
discountCode: validatedDiscount?.code || undefined
207+
})
209208

210209
const discountPercent = pricingData?.discountPercent || 0
211210
const hasDiscount = discountPercent > 0
@@ -343,14 +342,14 @@ function CoflCoinPaymentSelection({ selectedOption, onBack, countryCode, coflCoi
343342
stripePrice={dynamicPrices.stripe}
344343
lemonsqueezyPrice={dynamicPrices.lemonsqueezy}
345344
googlePlayPrice={dynamicPrices.googlepay}
346-
paypalProductId={buildProductIdWithCodes(selectedOption.paypalProductId)}
347-
stripeProductId={buildProductIdWithCodes(selectedOption.stripeProductId)}
348-
lemonsqueezyProductId={buildProductIdWithCodes(selectedOption.lemonsqueezyProductId)}
345+
paypalProductId={selectedOption.paypalProductId}
346+
stripeProductId={selectedOption.stripeProductId}
347+
lemonsqueezyProductId={selectedOption.lemonsqueezyProductId}
349348
googlePlayProductId={dynamicGooglePlayProductId}
350349
countryCode={countryCode}
351-
onPayPalPay={onPayPalPay}
352-
onStripePay={onStripePay}
353-
onLemonSqeezyPay={onLemonSqueezyPay}
350+
onPayPalPay={(productId, coflCoins) => onPayPalPay(productId, coflCoins, getPurchaseCodes())}
351+
onStripePay={(productId, coflCoins) => onStripePay(productId, coflCoins, getPurchaseCodes())}
352+
onLemonSqeezyPay={(productId, coflCoins) => onLemonSqueezyPay(productId, coflCoins, getPurchaseCodes())}
354353
onGooglePlayPay={onGooglePlayPay}
355354
loadingProductId={loadingProductId}
356355
disabledTooltip={undefined}

components/CoflCoins/CoflCoinPurchaseWizard.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22
import { useState } from 'react'
33
import CoflCoinAmountSelection from './CoflCoinAmountSelection'
4-
import CoflCoinPaymentSelection from './CoflCoinPaymentSelection'
4+
import CoflCoinPaymentSelection, { PurchaseCodes } from './CoflCoinPaymentSelection'
55

66
interface CoflCoinOption {
77
amount: number
@@ -18,9 +18,9 @@ interface CoflCoinOption {
1818
interface Props {
1919
coflCoins: number
2020
countryCode?: string
21-
onPayPalPay: (productId: string, coflCoins?: number) => void
22-
onStripePay: (productId: string, coflCoins?: number) => void
23-
onLemonSqueezyPay: (productId: string, coflCoins?: number) => void
21+
onPayPalPay: (productId: string, coflCoins?: number, codes?: PurchaseCodes) => void
22+
onStripePay: (productId: string, coflCoins?: number, codes?: PurchaseCodes) => void
23+
onLemonSqueezyPay: (productId: string, coflCoins?: number, codes?: PurchaseCodes) => void
2424
onGooglePlayPay: (productId: string, coflCoins?: number) => void
2525
loadingProductId: string
2626
isGooglePlayAvailable?: boolean

components/CoflCoins/CoflCoinsPurchase.tsx

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
import { useState } from 'react'
33
import { Alert, Button } from 'react-bootstrap'
44
import { toast } from 'react-toastify'
5-
import api from '../../api/ApiHelper'
6-
import { postApiTopupPlaystore } from '../../api/_generated/skyApi'
5+
import {
6+
postApiTopupPlaystore,
7+
postApiTopupStripeProductSlug,
8+
postApiTopupPaypalProductSlug,
9+
postApiTopupLemonsqueezyProductSlug
10+
} from '../../api/_generated/skyApi'
11+
import type { TopUpArguments } from '../../api/_generated/skyApi.schemas'
712
import { useCoflCoins } from '../../utils/Hooks'
813
import CoflCoinPurchaseWizard from './CoflCoinPurchaseWizard'
14+
import type { PurchaseCodes } from './CoflCoinPaymentSelection'
915
import PurchaseElement from './PurchaseElement'
1016
import CountrySelect from '../CountrySelect/CountrySelect'
1117
import { useCountryDetection } from '../../hooks/useCountryDetection'
@@ -156,37 +162,52 @@ function Payment(props: Props) {
156162
setIsGooglePlayAvailable(available)
157163
}
158164

159-
function onPayPaypal(productId: string, coflCoins?: number) {
165+
function startTopUp(
166+
topUpCall: (productSlug: string, args: TopUpArguments, options?: RequestInit) => Promise<any>,
167+
productId: string,
168+
coflCoins?: number,
169+
codes?: PurchaseCodes
170+
) {
171+
const googleId = typeof window !== 'undefined' ? sessionStorage.getItem('googleId') : null
172+
if (!googleId) {
173+
toast.error('You need to be logged in to purchase something.')
174+
return
175+
}
176+
160177
setLoadingId(coflCoins ? `${productId}_${coflCoins}` : productId)
161178
setCurrentRedirectLink('')
162-
api.paypalPurchase(productId, coflCoins)
163-
.then(data => {
164-
setCurrentRedirectLink(data.directLink)
165-
window.open(data.directLink, '_self')
179+
180+
topUpCall(
181+
productId,
182+
{
183+
coinAmount: coflCoins ?? 0,
184+
creatorCode: codes?.creatorCode ?? null,
185+
discountcode: codes?.discountCode ?? null
186+
},
187+
{ headers: { GoogleToken: googleId } }
188+
)
189+
.then(response => {
190+
const directLink = response?.data?.directLink ?? response?.data?.dirctLink
191+
if (response?.status !== 200 || !directLink) {
192+
onPaymentRedirectFail()
193+
return
194+
}
195+
setCurrentRedirectLink(directLink)
196+
window.open(directLink, '_self')
166197
})
167198
.catch(onPaymentRedirectFail)
168199
}
169200

170-
function onPayStripe(productId: string, coflCoins?: number) {
171-
setLoadingId(coflCoins ? `${productId}_${coflCoins}` : productId)
172-
setCurrentRedirectLink('')
173-
api.stripePurchase(productId, coflCoins)
174-
.then(data => {
175-
setCurrentRedirectLink(data.directLink)
176-
window.open(data.directLink, '_self')
177-
})
178-
.catch(onPaymentRedirectFail)
201+
function onPayPaypal(productId: string, coflCoins?: number, codes?: PurchaseCodes) {
202+
startTopUp(postApiTopupPaypalProductSlug, productId, coflCoins, codes)
179203
}
180204

181-
function onPayLemonSqueezy(productId: string, coflCoins?: number) {
182-
setLoadingId(coflCoins ? `${productId}_${coflCoins}` : productId)
183-
setCurrentRedirectLink('')
184-
api.lemonsqueezyPurchase(productId, coflCoins)
185-
.then(data => {
186-
setCurrentRedirectLink(data.directLink)
187-
window.open(data.directLink, '_self')
188-
})
189-
.catch(onPaymentRedirectFail)
205+
function onPayStripe(productId: string, coflCoins?: number, codes?: PurchaseCodes) {
206+
startTopUp(postApiTopupStripeProductSlug, productId, coflCoins, codes)
207+
}
208+
209+
function onPayLemonSqueezy(productId: string, coflCoins?: number, codes?: PurchaseCodes) {
210+
startTopUp(postApiTopupLemonsqueezyProductSlug, productId, coflCoins, codes)
190211
}
191212

192213
function onPayGooglePlay(productId: string, coflCoins?: number) {

global.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ interface API {
214214
unsubscribe(subscription: Subscription): Promise<void>
215215
getNotificationListener(): Promise<NotificationListener[]>
216216
loginWithToken(id: string): Promise<string>
217-
stripePurchase(productId: string, coinAmount?: number): Promise<PaymentResponse>
218217
getRecentAuctions(itemTag: string, itemFilter: ItemFilter): Promise<RecentAuction[]>
219218
getFlips(): Promise<FlipAuction[]>
220219
subscribeFlips(
@@ -253,7 +252,6 @@ interface API {
253252
getNewItems(): Promise<Item[]>
254253
getNewPlayers(): Promise<Player[]>
255254
getFlipBasedAuctions(flipUUID: string): Promise<Auction[]>
256-
paypalPurchase(productId: string, coinAmount?: number): Promise<PaymentResponse>
257255
getRefInfo(): Promise<RefInfo>
258256
setRef(refId: string): Promise<void>
259257
getActiveAuctions(item: Item, order: string, filter?: ItemFilter): Promise<RecentAuction[]>
@@ -295,7 +293,6 @@ interface API {
295293
getRelatedItems(tag: string): Promise<Item[]>
296294
getOwnerHistory(uid: string): Promise<OwnerHistory[]>
297295
getMayorData(start: Date, end: Date): Promise<MayorData[]>
298-
lemonsqueezyPurchase(productId: string, coinAmount?: number): Promise<PaymentResponse>
299296
getPlayerInventory(): Promise<InventoryData[]>
300297
createTradeOffer(playerUUID: string, offer?: InventoryData, wantedItems?: WantedItem[], offeredCoins?: number): Promise<void>
301298
getTradeOffers(onlyOwn: boolean, filter?: ItemFilter): Promise<TradeObject[]>

0 commit comments

Comments
 (0)