fix(server): rcat event sync (#13648)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- New Features
- Subscriptions now include an explicit "trial" flag so trialing users
are identified and treated correctly.

- Bug Fixes
  - More robust handling when webhook fields are missing or null.
- Improved family-sharing detection to avoid incorrect async processing.

- Refactor
- Status determination and store resolution simplified to rely on
subscription data rather than event payloads.

- Tests
- Test fixtures updated to include trial and store details for accuracy.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
DarkSky
2025-09-26 03:00:48 +08:00
committed by GitHub
parent 7a90e1551c
commit 3f9d9fef63
4 changed files with 58 additions and 60 deletions

View File

@@ -30,11 +30,14 @@ const RcEventSchema = z
app_user_id: z.string().optional(),
store: z.string().optional(),
is_family_share: z.boolean().optional(),
period_type: z.string().optional(),
original_transaction_id: z.string().optional(),
transaction_id: z.string().optional(),
purchase_token: z.string().optional(),
is_family_share: z.boolean().nullable().optional(),
period_type: z
.enum(['TRIAL', 'INTRO', 'NORMAL', 'PROMOTIONAL', 'PREPAID'])
.nullable()
.optional(),
original_transaction_id: z.string().nullable().optional(),
transaction_id: z.string().nullable().optional(),
purchase_token: z.string().nullable().optional(),
})
.passthrough();
@@ -74,7 +77,11 @@ export class RevenueCatWebhookController {
`[${id}] RevenueCat Webhook {${type}} received for appUserId=${appUserId}.`
);
if (appUserId && !event.is_family_share) {
if (
appUserId &&
(typeof event.is_family_share !== 'boolean' ||
!event.is_family_share)
) {
// immediately ack and process asynchronously
this.event
.emitAsync('revenuecat.webhook', { appUserId, event })

View File

@@ -3,6 +3,18 @@ import { z } from 'zod';
import { Config } from '../../../base';
const Store = z.enum([
'amazon',
'app_store',
'mac_app_store',
'play_store',
'promotional',
'stripe',
'rc_billing',
'roku',
'paddle',
]);
const zRcV2RawProduct = z
.object({
id: z.string().nonempty(),
@@ -11,20 +23,7 @@ const zRcV2RawProduct = z
.object({ duration: z.string().nullable() })
.partial()
.nullable(),
app: z
.object({
type: z.enum([
'amazon',
'app_store',
'mac_app_store',
'play_store',
'stripe',
'rc_billing',
'roku',
'paddle',
]),
})
.partial(),
app: z.object({ type: Store }).partial(),
})
.passthrough();
@@ -49,7 +48,7 @@ const zRcV2RawSubscription = z
entitlements: zRcV2RawEntitlements,
starts_at: z.number(),
current_period_ends_at: z.number().nullable(),
store: z.string(),
store: Store,
auto_renewal_status: z.enum([
'will_renew',
'will_not_renew',
@@ -83,11 +82,12 @@ const zRcV2RawEnvelope = z
// v2 minimal, simplified structure exposed to callers
export const Subscription = z.object({
identifier: z.string(),
isTrial: z.boolean(),
isActive: z.boolean(),
latestPurchaseDate: z.date().nullable(),
expirationDate: z.date().nullable(),
productId: z.string(),
store: z.string(),
store: Store,
willRenew: z.boolean(),
duration: z.string().nullable(),
});
@@ -145,6 +145,7 @@ export class RevenueCatService {
}
return {
identifier: ent.lookup_key,
isTrial: sub.status === 'trialing',
isActive:
sub.gives_access === true ||
sub.status === 'active' ||

View File

@@ -51,10 +51,8 @@ export class RevenueCatWebhookHandler {
// ignore non-whitelisted and non-fallbackable products
if (!mapping) continue;
const { status, deleteInstead, canceledAt, iapStore } = this.mapStatus(
sub,
event
);
const { status, deleteInstead, canceledAt, iapStore } =
this.mapStatus(sub);
const rcExternalRef = this.pickExternalRef(event);
@@ -174,10 +172,7 @@ export class RevenueCatWebhookHandler {
);
}
private mapStatus(
sub: Subscription,
event?: RcEvent
): {
private mapStatus(sub: Subscription): {
status: SubscriptionStatus;
iapStore: IapStore | null;
deleteInstead: boolean;
@@ -185,29 +180,16 @@ export class RevenueCatWebhookHandler {
} {
const now = Date.now();
const exp = sub.expirationDate?.getTime();
const periodType = (event?.period_type || '').toLowerCase();
const eventType = (event?.type || '').toString().toLowerCase();
// Determine iap store and external reference for observability
const iapStore = this.mapIapStore(sub.store, event);
// Refund/chargeback/revocation should be treated as immediate expiration
// Prioritize these event types regardless of current sub.isActive flag
if (
eventType.includes('refund') ||
eventType.includes('chargeback') ||
eventType.includes('revocation') ||
eventType.includes('revoke')
) {
return {
iapStore,
status: SubscriptionStatus.Canceled,
deleteInstead: true,
};
}
const iapStore = ['app_store', 'mac_app_store'].includes(sub.store)
? IapStore.app_store
: ['play_store'].includes(sub.store)
? IapStore.play_store
: null;
if (sub.isActive) {
if (periodType === 'trial') {
if (sub.isTrial) {
return {
iapStore,
status: SubscriptionStatus.Trialing,
@@ -241,13 +223,4 @@ export class RevenueCatWebhookHandler {
deleteInstead: true,
};
}
private mapIapStore(store?: string, event?: RcEvent): IapStore | null {
const s = (store || event?.store || '').toString().toLowerCase();
if (!s) return null;
if (s.includes('app') || s.includes('ios')) return IapStore.app_store;
if (s.includes('play') || s.includes('android') || s.includes('google'))
return IapStore.play_store;
return null;
}
}