验证iOS支付时, 优先去production验证, 错误后去sandbox

This commit is contained in:
CounterFire2023 2023-08-16 11:11:05 +08:00
parent fe0eace046
commit f8428c369c

View File

@ -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