|
| 1 | +import { mainLogger } from '@stdlib/misc'; |
| 2 | +import type Fastify from 'fastify'; |
| 3 | +import jws from 'jws'; |
| 4 | +import { createContext } from 'src/trpc/context'; |
| 5 | +import type { InferProcedureOpts } from 'src/trpc/helpers'; |
| 6 | +import { publicProcedure } from 'src/trpc/helpers'; |
| 7 | +import { z } from 'zod'; |
| 8 | + |
| 9 | +interface responseBodyV2DecodedPayload { |
| 10 | + /* A unique identifier for the notification. Use this value to identify a duplicate notification. */ |
| 11 | + |
| 12 | + notificationUUID: string; |
| 13 | + |
| 14 | + /* |
| 15 | + The in-app purchase event for which the App Store sends this version 2 notification. |
| 16 | +
|
| 17 | + Possible values: |
| 18 | + - CONSUMPTION_REQUEST |
| 19 | + - DID_CHANGE_RENEWAL_PREF |
| 20 | + - DID_CHANGE_RENEWAL_STATUS |
| 21 | + - DID_FAIL_TO_RENEW |
| 22 | + - DID_RENEW |
| 23 | + - EXPIRED |
| 24 | + - GRACE_PERIOD_EXPIRED |
| 25 | + - OFFER_REDEEMED |
| 26 | + - PRICE_INCREASE |
| 27 | + - REFUND |
| 28 | + - REFUND_DECLINED |
| 29 | + - REFUND_REVERSED |
| 30 | + - RENEWAL_EXTENDED |
| 31 | + - RENEWAL_EXTENSION |
| 32 | + - REVOKE |
| 33 | + - SUBSCRIBED |
| 34 | + - TEST |
| 35 | + */ |
| 36 | + |
| 37 | + notificationType: 'SUBSCRIBED' | 'EXPIRED'; |
| 38 | + |
| 39 | + /* The object that contains the app metadata and signed renewal and transaction information. */ |
| 40 | + |
| 41 | + data: { |
| 42 | + /* |
| 43 | + The unique identifier of the app that the notification applies to. |
| 44 | + This property is available for apps that users download from the App Store. |
| 45 | + It isn’t present in the sandbox environment. |
| 46 | + */ |
| 47 | + |
| 48 | + appAppleId: string; |
| 49 | + |
| 50 | + /* The server environment that the notification applies to, either sandbox or production. */ |
| 51 | + |
| 52 | + environment: 'Sandbox' | 'Production'; |
| 53 | + |
| 54 | + /* Transaction information signed by the App Store, in JSON Web Signature (JWS) format. */ |
| 55 | + |
| 56 | + signedTransactionInfo: string; |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +interface JWSTransactionDecodedPayload { |
| 61 | + appAccountToken: string; |
| 62 | + |
| 63 | + bundleId: string; |
| 64 | + |
| 65 | + environment: 'Sandbox' | 'Production'; |
| 66 | + |
| 67 | + productId: string; |
| 68 | + |
| 69 | + type: |
| 70 | + | 'Auto-Renewable Subscription' |
| 71 | + | 'Non-Consumable' |
| 72 | + | 'Consumable' |
| 73 | + | 'Non-Renewing Subscription'; |
| 74 | + |
| 75 | + transactionReason: 'PURCHASE' | 'RENEWAL'; |
| 76 | +} |
| 77 | + |
| 78 | +const _webhookLogger = mainLogger.sub('app-store-webhook'); |
| 79 | + |
| 80 | +const baseProcedure = publicProcedure.input( |
| 81 | + z.object({ |
| 82 | + signedPayload: z.string(), |
| 83 | + }), |
| 84 | +); |
| 85 | + |
| 86 | +export function registerAppStoreWebhook(fastify: ReturnType<typeof Fastify>) { |
| 87 | + fastify.post('/app-store/webhook', { |
| 88 | + handler: async (req, res) => { |
| 89 | + const ctx = createContext({ req, res }); |
| 90 | + |
| 91 | + return await webhook({ ctx, input: req.body as any }); |
| 92 | + }, |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +export async function webhook({ |
| 97 | + input, |
| 98 | +}: InferProcedureOpts<typeof baseProcedure>) { |
| 99 | + _webhookLogger.info('Signed payload: %o', input); |
| 100 | + |
| 101 | + const decodedPayloadSignature = jws.decode(input.signedPayload); |
| 102 | + const decodedPayload = |
| 103 | + decodedPayloadSignature.payload as responseBodyV2DecodedPayload; |
| 104 | + |
| 105 | + _webhookLogger.info('Decoded payload: %o', decodedPayload); |
| 106 | + |
| 107 | + const decodedTransactionSignature = jws.decode( |
| 108 | + decodedPayload.data.signedTransactionInfo, |
| 109 | + ); |
| 110 | + const decodedTransaction = |
| 111 | + decodedTransactionSignature.payload as JWSTransactionDecodedPayload; |
| 112 | + |
| 113 | + _webhookLogger.info('Decoded transaction: %o', decodedTransaction); |
| 114 | +} |
0 commit comments