-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpayment.gs
138 lines (107 loc) · 4.67 KB
/
payment.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//資金処理のための関数群
//cacheにtransMoneyのキューを追加する.
function transMoney(recvId, sendId, productName, value){
var data = getCache("transMoney");
var newData = {
"recvId": recvId,
"sendId": sendId,
"productName": productName,
"value": value
}
//オブジェクトであるnewDataをstrに変換して配列に追加.
data.push(JSON.stringify(newData));
//配列を;で分割するstrに変換してcacheに格納
cache = CacheService.getScriptCache();
cache.put("transMoney", data.join(';'), 60*2);
}
//定期実行でcacheを読みtransMoneyを実行する
function timeDrivenTransMoney(){
//get slack access token from properties.
var slack_access_token = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN');
//cacheを取得
var data = getCache("transMoney");
//配列の中身をstrからjsonに戻し,postMessageExecに投げる.
for(var i=0; i<data.length; i++){
data[i] = JSON.parse(data[i]);
transMoneyExec(data[i].recvId, data[i].sendId, data[i].productName, data[i].value);
}
return;
}
function transMoneyExec(recvId, sendId, productName, value) {
//出品者自身の商品を買った場合の例外処理
if(recvId == sendId){
postMessage("@"+recvId,"出品者自身の購入のため、処理は行いませんでした。["+value+"円]");
return;
}
var memberSheetId = PropertiesService.getScriptProperties().getProperty('SHEET_ID');
var recvInfo = arrayFormat(SpreadSheetsSQL.open(memberSheetId, "sheet1").select(['id', 'price', 'name']).filter("id = " + recvId).result());
var sendInfo = arrayFormat(SpreadSheetsSQL.open(memberSheetId, "sheet1").select(['id', 'price', 'name']).filter("id = " + sendId).result());
recvInfo.price = parseInt(recvInfo.price) + parseInt(value);
sendInfo.price = parseInt(sendInfo.price) - parseInt(value);
SpreadSheetsSQL.open(memberSheetId, "sheet1").updateRows({price: recvInfo.price}, "id = "+recvId);
SpreadSheetsSQL.open(memberSheetId, "sheet1").updateRows({price: sendInfo.price}, "id = "+sendId);
//Logに出力
var logSheetId = PropertiesService.getScriptProperties().getProperty('LOG_SHEET_ID');
var date = Utilities.formatDate(new Date(), "JST", "yyyy/MM/dd/HH/mm");
SpreadSheetsSQL.open(logSheetId, "sheet1").insertRows([
{date: date, productName: productName, recvId: recvInfo.name, sendId: sendInfo.name, price: value},
]);
//Logここまで
//Slackに投稿
postMessage("@" + recvId, "[販売]" + productName + " / 残高:¥" + recvInfo.price + "(+" + value + ") <- <@" + sendId + ">");
postMessage("@" + sendId, "[購入]" + productName + " / 残高:¥" + sendInfo.price + "(-" + value + ") -> <@" + recvId + ">");
postMessage("#money_log","[送金]" + productName + " <@" + sendId + "> -> <@" + recvId + ">[¥" + value + "]");
return;
}
function addMoney(recvId, value) {
var memberSheetId = PropertiesService.getScriptProperties().getProperty('SHEET_ID');
var recvInfo = arrayFormat(SpreadSheetsSQL.open(memberSheetId, "sheet1").select(['id', 'price', 'name']).filter("id = " + recvId).result());
recvInfo.price = parseInt(recvInfo.price) + parseInt(value);
SpreadSheetsSQL.open(memberSheetId, "sheet1").updateRows({price: recvInfo.price}, "id = "+recvId);
//Slackに投稿
postMessage("@" + recvId, "[入金] 残高:¥" + recvInfo.price + "(+" + value + ")");
postMessage("#money_log","[入金] <@" + recvId + ">[¥" + value + "]");
return;
}
function getCache(key){
cache = CacheService.getScriptCache();
var data = cache.get(key);
//cacheの競合が怖いのでなるべく早く消しておく
cache.remove(key);
//cacheの中身がnullならば空配列に,nullでないならstrを配列に変換する.
if(data==null){
data = [];
}else{
data = data.split(';');
}
return data;
}
//SlackのWebAPIを叩く.
function postMessage(id,message){
//get slack access token from properties.
var slack_access_token = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN');
var slackUrl = "https://slack.com/api/chat.postMessage"
var options = {
method: 'post',
payload: {
"token": slack_access_token,
"channel": id,
"text": message,
"username": "ISDLのウィーゴ",
"icon_url": "http://www.hasegawa-model.co.jp/hsite/wp-content/uploads/2016/04/cw12p5.jpg",
"link_names": 1
},
};
// post to Slack
UrlFetchApp.fetch(slackUrl, options);
}
function arrayFormat(array){
return array[0]
}
function arrayParse(array) {
var parseArray = [];
for (var i = 0; i < array.length; i++) {
parseArray[i] = array[i][0];
}
return parseArray;
}