From 448ab6e3dfe3cc4fac6a5ff497f7bcad9b703d11 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Tue, 29 Sep 2020 12:47:37 +0530 Subject: [PATCH] feat: decode signed json and QR code --- .../e_invoice_settings/e_invoice_settings.js | 13 ++++++++++++ .../e_invoice_settings/e_invoice_settings.py | 21 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 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 4dc7988ec5e..23415bb60a6 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 @@ -29,6 +29,19 @@ frappe.ui.form.on('E Invoice Settings', { callback: (res) => console.log(res) }); }); + + frm.add_custom_button(__("Fetch IRN Details"), + () => { + frm.call({ + doc: frm.doc, + method: 'get_irn_details', + args: { + 'irn': 'c63d9e180dfdaa9242e29e2e1e0a8d76f20e116ed3de179a2e9120f384e1b432' + }, + freeze: true, + callback: (res) => console.log(res) + }); + }); }, show_fetch_token_btn(frm) { 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 87d06f45bce..b141971f76f 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 @@ -5,6 +5,7 @@ from __future__ import unicode_literals import os import re +import jwt import json import base64 import frappe @@ -65,6 +66,9 @@ class EInvoiceSettings(Document): enc_msg = cipher.encrypt(padded_bytes_msg) b64_enc_msg = base64.b64encode(enc_msg) return b64_enc_msg.decode() + + def jwt_decrypt(self, token): + return jwt.decode(token, verify=False) def make_authentication_request(self): endpoint = 'https://einv-apisandbox.nic.in/eivital/v1.03/auth' @@ -129,9 +133,12 @@ class EInvoiceSettings(Document): res = make_post_request(endpoint, headers=headers, data=json.dumps(payload)) self.handle_err_response(res) - return res + data = json.loads(res) + self.handle_irn_response(data) + + return data - def get_irn_detials(self, irn): + def get_irn_details(self, irn): endpoint = 'https://einv-apisandbox.nic.in/eicore/v1.03/Invoice/irn/{irn}'.format(irn=irn) headers = self.get_header() @@ -140,9 +147,19 @@ class EInvoiceSettings(Document): enc_json = res.get('Data') json_str = self.aes_decrypt(enc_json, self.sek) + data = json.loads(json_str) + self.handle_irn_response(data) return data + + def handle_irn_response(self, data): + enc_signed_invoice = data['SignedInvoice'] + enc_signed_qr_code = data['SignedQRCode'] + signed_invoice = self.jwt_decrypt(enc_signed_invoice)['data'] + signed_qr_code = self.jwt_decrypt(enc_signed_qr_code)['data'] + data['DecryptedSignedInvoice'] = json.loads(signed_invoice) + data['DecryptedSignedQRCode'] = json.loads(signed_qr_code) def handle_err_response(self, response): if response.get('Status') == 0: