Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/common/paddle/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,24 @@ export const getPricingMetadataByPriceIds = async (
return Object.fromEntries(items.map(({ item }) => [item.idMap.paddle, item]));
};

export const getPlusPricePreview = async (ctx: AuthContext, ids: string[]) => {
export const getPlusPricePreview = async (
ctx: AuthContext,
ids: string[],
discountId?: string,
) => {
const region = ctx.region;
const sortedIds = ids.sort();

const hmac = createHmac('sha1', StorageTopic.Paddle);
hmac.update(sortedIds.toString());
const pricesHash = hmac.digest().toString('hex');

// Include discountId in cache key when present
const cacheKeyParts = [pricesHash, region, discountId].filter(Boolean);
const redisKey = generateStorageKey(
StorageTopic.Paddle,
StorageKey.PricingPreviewPlus,
[pricesHash, region].join(':'),
cacheKeyParts.join(':'),
);

const redisResult = await getRedisObject(redisKey);
Expand All @@ -239,6 +245,7 @@ export const getPlusPricePreview = async (ctx: AuthContext, ids: string[]) => {
quantity: 1,
})),
address: region ? { countryCode: region as CountryCode } : undefined,
discountId,
});

await setRedisObjectWithExpiry(
Expand Down
15 changes: 12 additions & 3 deletions src/schema/paddle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ export const typeDefs = /* GraphQL */ `
pricePreviews: PricePreviews! @auth
corePricePreviews: PricePreviews! @auth
pricingMetadata(type: PricingType): [ProductPricingMetadata!]! @auth
pricingPreview(type: PricingType, locale: String): [ProductPricingPreview!]!
pricingPreview(
type: PricingType
locale: String
discountId: String
): [ProductPricingPreview!]!
pricingPreviewByIds(
"""
The IDs of the prices to preview
Expand Down Expand Up @@ -323,6 +327,7 @@ export interface GQLCustomData {
interface PaddlePricingPreviewArgs {
type?: PurchaseType;
locale?: string;
discountId?: string;
}

interface PaddlePricingPreviewByIdsArgs {
Expand Down Expand Up @@ -445,15 +450,19 @@ export const resolvers: IResolvers<unknown, AuthContext> = traceResolvers<
): Promise<BasePricingMetadata[]> => getPricingMetadata(ctx, type),
pricingPreview: async (
_,
{ type = PurchaseType.Plus, locale }: PaddlePricingPreviewArgs,
{
type = PurchaseType.Plus,
locale,
discountId,
}: PaddlePricingPreviewArgs,
ctx,
): Promise<BasePricingPreview[]> => {
const metadata = await getPricingMetadata(ctx, type);
const ids = metadata
.map(({ idMap }) => idMap.paddle)
.filter(Boolean) as string[];

const preview = await getPlusPricePreview(ctx, ids);
const preview = await getPlusPricePreview(ctx, ids, discountId);
Comment thread
capJavert marked this conversation as resolved.

// consolidate the preview data and metadata
const consolidated = metadata.map((meta) => {
Expand Down
Loading