#Frontend Server #Node JS #Koa
#appStore.controller.js
1. Config
const {
AppStoreServerAPI,
Environment,
decodeTransactions,
OrderLookupStatus,
decodeNotificationPayload,
decodeTransaction,
decodeRenewalInfo,
} = require('app-store-server-api');
const KEY = process.env.APP_STORE_CONNECT_AUTH_KEY;
const KEY_ID = process.env.APP_STORE_CONNECT_KEY_ID;
const ISSUER_ID = process.env.APP_STORE_CONNECT_ISSUER_ID;
const APP_BUNDLE_ID = 'com.hello.world';
const api = new AppStoreServerAPI(
KEY,
KEY_ID,
ISSUER_ID,
APP_BUNDLE_ID,
Environment.Production
);
/**
* lookup transactions by orderId
* @path {get} /api/lookup/:orderId
* @param {String} orderId
*/
exports.lookup = async (ctx) => {
const { orderId } = ctx.params;
const response = await api.lookupOrder(orderId);
if (response.status !== OrderLookupStatus.Valid) {
return (ctx.body = {succes: false});
}
const transactions = await decodeTransactions(
response.signedTransactions
);
ctx.body = {
success: true,
data: transactions,
}
};
/**
* get transaction history by originalTransactionId
* @path {get} /api/transactionHistory/:originalTransactionId
* @param {String} originalTransactionId
*/
exports.transactionHistory = async (ctx) => {
const { originalTransactionId } = ctx.params;
const response = await api.getTransactionHistory(originalTransactionId);
const transactions = await decodeTransactions(response.signedTransactions);
// The response contains at most 20 entries. You can check to see if there are more.
// if (response.hasMore) {
// const nextResponse = await api.getTransactionHistory(
// originalTransactionId,
// { revision: response.revision }
// );
ctx.body = {
success: true,
data: transactions,
};
};
/**
* get notification history during the period
* @path {get} /api/payment/inapp/appstore/notificationHistory
* @param {String} startDate
* @param {String} endDate
*/
exports.notificationHistory = async (ctx) => {
const { startDate, endDate } = ctx.request.query;
const response = await api.getNotificationHistory({
startDate,
endDate,
});
const { notificationHistory } = response;
let notifications = [];
if (notificationHistory?.length) {
for (const i in notificationHistory) {
const history = notificationHistory[i];
const payload = await decodeNotificationPayload(history.signedPayload);
const transactionInfo = await decodeTransaction(payload.data.signedTransactionInfo);
const renewalInfo = await decodeRenewalInfo(payload.data.signedRenewalInfo);
payload.data.signedTransactionInfo = transactionInfo;
payload.data.signedRenewalInfo = renewalInfo;
notifications.push({
payload
});
}
}
ctx.body = {
success: true,
data: notifications,
};
}
'CODE > Javascript' 카테고리의 다른 글
| How to handle Google Play Subscription (0) | 2023.01.20 |
|---|---|
| How to handle App Store Subscription (0) | 2023.01.20 |