【发布时间】:2021-03-30 08:58:47
【问题描述】:
我想使用 webhook 在 Google 表格中捕获交易。我已经尝试过,但它对我来说并不完美。每当我运行代码时,我都会将所有数据放入 1 个单元格中。
【问题讨论】:
标签: javascript google-apps-script google-sheets razorpay
我想使用 webhook 在 Google 表格中捕获交易。我已经尝试过,但它对我来说并不完美。每当我运行代码时,我都会将所有数据放入 1 个单元格中。
【问题讨论】:
标签: javascript google-apps-script google-sheets razorpay
好吧,在你介绍了 JSON 之后,我理解了它的结构。正如您在 json 中看到的,您需要深入研究 json 以获得金额。像这样:
const dig = myData.payload.payment.entity;
const amount = dig.amount
因此,如果您想要 bank_transaction_id,您将获得它:
const bankTransferId = dig.acquirer_data.bank_transaction_id;
我希望这是有道理的?正如您所看到的,发布了 awnser 和一个测试功能,因此您可以自己尝试和测试,使用控制台日志您可以检查您的工作。在您看到调试器的屏幕截图中,您可以看到 JSON 树;)
解决方案:
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}
function doPost(e) {
const params = JSON.stringify(e.postData.contents);
const myData = JSON.parse(params);
const ts = Utilities.formatDate(new Date(), "GMT+5:30", "dd/MM/YYYY");
const dig = myData.payload.payment.entity;
const row = [ts,params,myData.id,dig.amount,dig.status,dig.method,dig.vpa,dig.email,dig.contact];
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data")
sheet.getRange(sheet.getLastRow()+1, 1, 1, row.length).setValues([row]);
SpreadsheetApp.flush();
return HtmlService.createHtmlOutput("post request received");
};
测试:
function test(){
const raw =
{
"entity": "event",
"account_id": "acc_BFQ7uQEaa7j2z7",
"event": "payment.authorized",
"contains": [
"payment"
],
"payload": {
"payment": {
"entity": {
"id": "pay_DESlfW9H8K9uqM",
"entity": "payment",
"amount": 100,
"currency": "INR",
"status": "authorized",
"order_id": "order_DESlLckIVRkHWj",
"invoice_id": null,
"international": false,
"method": "netbanking",
"amount_refunded": 0,
"refund_status": null,
"captured": false,
"description": null,
"card_id": null,
"bank": "HDFC",
"wallet": null,
"vpa": null,
"email": "gaurav.kumar@example.com",
"contact": "+919876543210",
"notes": [],
"fee": null,
"tax": null,
"error_code": null,
"error_description": null,
"error_source": null,
"error_step": null,
"error_reason": null,
"acquirer_data": {
"bank_transaction_id": "0125836177"
},
"created_at": 1567674599
}
}
},
"created_at": 1567674606
};
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName("Data");
const string = JSON.stringify(raw);
const myData = JSON.parse(string);
const dig = myData.payload.payment.entity;
const ts = Utilities.formatDate(new Date(), "GMT+5:30", "dd/MM/YYYY");
const row = [ts,myData.id,dig.amount,dig.status,dig.method,dig.vpa,dig.email,dig.contact];
console.log(dig.email);
sh.getRange(sh.getLastRow()+1, 1, 1, row.length).setValues([row]);
}
【讨论】: