From e73a9deb42bf40af95367093f58a477c74edbd6d Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Fri, 25 Sep 2020 21:59:38 +0530 Subject: [PATCH] feat: save token and sek from auth request --- .../e_invoice_settings/e_invoice_settings.js | 23 ++++++++++------- .../e_invoice_settings.json | 25 +++++++++++++++++-- .../e_invoice_settings/e_invoice_settings.py | 18 ++++++++++++- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.js b/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.js index 1f9054461f4..9fc67ce1b98 100644 --- a/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.js +++ b/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.js @@ -7,14 +7,19 @@ frappe.ui.form.on('E Invoice Settings', { }, show_fetch_token_btn(frm) { - frm.add_custom_button(__("Fetch Token"), - () => { - frm.call({ - doc: frm.doc, - method: 'make_authentication_request', - freeze: true - }); - } - ); + const { token_expiry } = frm.doc; + const now = frappe.datetime.now_datetime(); + const expiry_in_mins = moment(token_expiry).diff(now, "minute"); + if (expiry_in_mins <= 1) { + frm.add_custom_button(__("Fetch Token"), + () => { + frm.call({ + doc: frm.doc, + method: 'make_authentication_request', + freeze: true + }); + } + ); + } } }); diff --git a/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.json b/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.json index 5278c6eebd2..888406b534c 100644 --- a/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.json +++ b/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.json @@ -13,7 +13,10 @@ "gstin", "username", "password", - "auto_refresh_token" + "auto_refresh_token", + "auth_token", + "token_expiry", + "sek" ], "fields": [ { @@ -69,12 +72,30 @@ "fieldname": "auto_refresh_token", "fieldtype": "Check", "label": "Auto Refresh Token" + }, + { + "fieldname": "auth_token", + "fieldtype": "Data", + "hidden": 1, + "read_only": 1 + }, + { + "fieldname": "token_expiry", + "fieldtype": "Datetime", + "hidden": 1, + "read_only": 1 + }, + { + "fieldname": "sek", + "fieldtype": "Data", + "hidden": 1, + "read_only": 1 } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2020-09-25 21:32:36.553678", + "modified": "2020-09-25 21:57:58.187647", "modified_by": "Administrator", "module": "ERPNext Integrations", "name": "E Invoice Settings", diff --git a/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.py b/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.py index 129dcd35b6e..f157e4ca0fa 100644 --- a/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.py +++ b/erpnext/erpnext_integrations/doctype/e_invoice_settings/e_invoice_settings.py @@ -10,6 +10,7 @@ import frappe from frappe.utils import cstr from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 +from frappe.utils.data import get_datetime from frappe.model.document import Document from frappe.integrations.utils import make_post_request @@ -35,6 +36,9 @@ class EInvoiceSettings(Document): enc_msg = cipher.encrypt(msg) b64_enc_msg = base64.b64encode(enc_msg) return b64_enc_msg.decode() + + def decrypt_sek(self, enc_sek, key): + return enc_sek def make_authentication_request(self): endpoint = 'https://einv-apisandbox.nic.in/eivital/v1.03/auth' @@ -52,5 +56,17 @@ class EInvoiceSettings(Document): res = make_post_request(endpoint, headers=headers, data=json.dumps({ 'data': payload })) - print(res) + self.extract_token_and_sek(res, appkey) + + def extract_token_and_sek(self, response, appkey): + data = response.get('Data') + auth_token = data.get('AuthToken') + token_expiry = data.get('TokenExpiry') + enc_sek = data.get('Sek') + sek = self.decrypt_sek(enc_sek, appkey) + + self.auth_token = auth_token + self.token_expiry = get_datetime(token_expiry) + self.sek = sek + self.save()