Skip to content

Instantly share code, notes, and snippets.

@keitaoouchi
Last active August 24, 2021 05:34
Show Gist options
  • Save keitaoouchi/4d89d88dffca6544a9111a25f137869e to your computer and use it in GitHub Desktop.
Save keitaoouchi/4d89d88dffca6544a9111a25f137869e to your computer and use it in GitHub Desktop.
Logging server for in-app purchase's server to server notification on google app script
function doGet(e) {
var row = defineRow(e);
var cell = getCell(row);
return ContentService.createTextOutput(cell).setMimeType(ContentService.MimeType.JSON);
}
function doPost(e) {
if (e == null || e.postData == null || e.postData.contents == null) { return; }
var body = e.postData.contents;
var json = JSON.parse(body);
var columns = [];
var headers = getHeaders()
for (i in headers){
var header = headers[i];
var column = "";
switch(header) {
case "date":
column = new Date();
break;
case "json":
if(json['environment'] == 'PROD') {
break;
}
column = body;
break;
case "次回更新":
var transaction = latestTransaction(json);
var expMs = parseInt(transaction["expires_date_ms"]);
var expiresDate = new Date(expMs);
var expStr = expiresDate.toLocaleDateString('ja-JP') + " " + expiresDate.toLocaleTimeString('ja-JP');
column = expStr;
break;
case "original_transaction_id":
var transaction = latestTransaction(json);
var oid = transaction["original_transaction_id"];
column = oid;
break;
default:
column = parse(json, header);
break;
}
columns.push(column);
}
getSheet().appendRow(columns);
}
/** PRIVATE */
function getCell(row) {
var values = getSheet().getDataRange().getValues();
var column = findHeader("json");
return values[row][column].trim();
}
function defineRow(e) {
if(e.parameter == undefined || e.parameter.row == undefined) {
return getSheet().getLastRow() - 1;
}
var row = parseInt(e.parameter.row);
return row;
}
function getHeaders() {
var sheet = getSheet();
return sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
}
function findHeader(key) {
return getHeaders().indexOf(key);
}
function getSheet() {
return SpreadsheetApp.getActive().getSheetByName("receiver");
}
function parse(json, header) {
var keys = header.split(".").reverse();
var key = keys.pop();
var value = json[key];
while(key && value) {
key = keys.pop();
if(key) {
value = value[key];
}
}
return stringify(value);
}
function stringify(value) {
if(typeof(value) === 'string') {
return value;
} else {
return JSON.stringify(value);
}
}
function latestTransaction(json) {
var product = parse(json, "auto_renew_product_id");
var transactions = json["unified_receipt"]["latest_receipt_info"];
var transaction = transactions.find(function(t) { return t["product_id"] == product });
return transaction
}
/* TEST */
function doTest() {
var row = defineRow({});
var cell = getCell(row);
return cell
}
@keitaoouchi
Copy link
Author

keitaoouchi commented Jul 5, 2021

This is a sample log of our sandbox app's server to server notification.

スクリーンショット 2021-07-05 16 53 52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment