From f8428c369ce60934c533aabf8263a565c3f55240 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Wed, 16 Aug 2023 11:11:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AA=8C=E8=AF=81iOS=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=20=E4=BC=98=E5=85=88=E5=8E=BBproduction?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=EF=BC=8C=20=E9=94=99=E8=AF=AF=E5=90=8E?= =?UTF-8?q?=E5=8E=BBsandbox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/iospay.svr.ts | 45 +++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/service/iospay.svr.ts b/src/service/iospay.svr.ts index f52e343..c46c035 100644 --- a/src/service/iospay.svr.ts +++ b/src/service/iospay.svr.ts @@ -22,13 +22,20 @@ const ISSUER_ID = process.env.IOS_ISSUER_ID const APP_BUNDLE_ID = process.env.IOS_APP_BUNDLE_ID const environment = sandbox ? Environment.Sandbox : Environment.Production -const api = new AppStoreServerAPI(KEY, KEY_ID, ISSUER_ID, APP_BUNDLE_ID, environment) +const apiProd = new AppStoreServerAPI(KEY, KEY_ID, ISSUER_ID, APP_BUNDLE_ID, Environment.Production) +const apiSandbox = new AppStoreServerAPI(KEY, KEY_ID, ISSUER_ID, APP_BUNDLE_ID, Environment.Sandbox) @singleton export class IosPaySvr { public async iosPayVerify(orderId: string) { logger.info('iosPayVerify: ', orderId) - const response = await api.lookupOrder(orderId) + let response; + try { + response = await apiProd.lookupOrder(orderId) + } catch (err) { + logger.info('iosPayVerify err: ', err) + response = await apiSandbox.lookupOrder(orderId) + } if (response.status === OrderLookupStatus.Valid) { const transactions = await decodeTransactions(response.signedTransactions) @@ -44,7 +51,17 @@ export class IosPaySvr { if (rversion) { reqData['revision'] = rversion } - const response = await api.getTransactionHistory(originalTransactionId, reqData) + let response + try { + response = await apiProd.getTransactionHistory(originalTransactionId, reqData) + } catch (err) { + logger.info(`transaction with id ${originalTransactionId} not found in production, try sandbox`) + try { + response = await apiSandbox.getTransactionHistory(originalTransactionId, reqData) + } catch (errSandbox) { + logger.info(`transaction with id ${originalTransactionId} not found in sandbox`) + } + } let results: any = [] // Decoding not only reveals the contents of the transactions but also verifies that they were signed by Apple. const transactions = await decodeTransactions(response.signedTransactions) @@ -61,7 +78,21 @@ export class IosPaySvr { } public async getTransactionInfo(originalTransactionId: string) { - const response = await api.getTransactionInfo(originalTransactionId) + let response; + try { + response = await apiProd.getTransactionInfo(originalTransactionId) + } catch (err) { + try { + logger.info(`transaction with id ${originalTransactionId} not found in production, try sandbox`) + response = await apiSandbox.getTransactionInfo(originalTransactionId) + } catch (errSandbox) { + logger.info(`transaction with id ${originalTransactionId} not found in sandbox`) + throw new Error('getTransactionInfo err') + } + } + if (!response) { + throw new Error('empty transaction info') + } const transaction = await decodeTransaction(response.signedTransactionInfo) return transaction } @@ -69,7 +100,7 @@ export class IosPaySvr { public async queryNotificationHistory() { // Start and end date are required. // The earliest supported start date is June 6th (the start of WWDC 2022). - const response = await api.getNotificationHistory({ + const response = await apiProd.getNotificationHistory({ startDate: 1690175687060, // June 6th 2022 endDate: new Date().getTime(), }) @@ -85,9 +116,9 @@ export class IosPaySvr { } public async requestTestNotification() { - let resp = await api.requestTestNotification() + let resp = await apiSandbox.requestTestNotification() logger.info('requestTestNotification response: ', resp) - let info = await api.getTestNotificationStatus(resp.testNotificationToken) + let info = await apiSandbox.getTestNotificationStatus(resp.testNotificationToken) let decodeInfo = await decodeNotificationPayload(info.signedPayload) logger.info(decodeInfo) return decodeInfo