-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: enhanced otp handeling taxpayer apis
(cherry picked from commit 7afa94c) # Conflicts: # india_compliance/__init__.py
- Loading branch information
1 parent
f5ab72f
commit fcb8ae5
Showing
7 changed files
with
184 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
<<<<<<< HEAD | ||
__version__ = "15.0.2" | ||
======= | ||
from .exceptions import * | ||
|
||
__version__ = "16.0.0-dev" | ||
>>>>>>> 7afa94c7 (fix: enhanced otp handeling taxpayer apis) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
frappe.provide("taxpayer_api"); | ||
frappe.provide("india_compliance"); | ||
|
||
taxpayer_api.call = async function (...args) { | ||
const response = await frappe.call(...args); | ||
const { message } = response; | ||
if (!["otp_requested", "invalid_otp"].includes(message?.error_type)) return response; | ||
|
||
await india_compliance.authenticate_otp(message.gstin, message.error_type); | ||
return taxpayer_api.call(...args); | ||
} | ||
|
||
Object.assign(india_compliance, { | ||
get_gstin_otp(company_gstin, error_type) { | ||
let description = `An OTP has been sent to the registered mobile/email for GSTIN ${company_gstin} for further authentication. Please provide OTP.`; | ||
if (error_type === "invalid_otp") | ||
description = `Invalid OTP was provided for GSTIN ${company_gstin}. Please try again.`; | ||
|
||
return new Promise(resolve => { | ||
const prompt = new frappe.ui.Dialog({ | ||
title: __("Enter OTP"), | ||
fields: [ | ||
{ | ||
fieldtype: "Data", | ||
label: __("One Time Password"), | ||
fieldname: "otp", | ||
reqd: 1, | ||
description: description, | ||
}, | ||
], | ||
primary_action_label: __("Submit"), | ||
primary_action(values) { | ||
resolve(values.otp); | ||
prompt.hide(); | ||
}, | ||
secondary_action_label: __("Resend OTP"), | ||
secondary_action() { | ||
frappe.call({ | ||
method: "india_compliance.gst_india.utils.gstr_utils.request_otp", | ||
args: { company_gstin }, | ||
callback: function () { | ||
frappe.show_alert({ | ||
message: __("OTP has been resent."), | ||
indicator: "green", | ||
}); | ||
prompt.get_secondary_btn().addClass("disabled"); | ||
}, | ||
}); | ||
}, | ||
}); | ||
prompt.show(); | ||
}); | ||
}, | ||
|
||
async authenticate_company_gstins(company, company_gstin) { | ||
const { message: gstin_authentication_status } = await frappe.call({ | ||
method: "india_compliance.gst_india.utils.gstr_utils.validate_company_gstins", | ||
args: { company: company, company_gstin: company_gstin }, | ||
}); | ||
|
||
for (let gstin of Object.keys(gstin_authentication_status)) { | ||
if (gstin_authentication_status[gstin]) continue; | ||
|
||
gstin_authentication_status[gstin] = | ||
await this.request_and_authenticate_otp(gstin); | ||
} | ||
|
||
return Object.keys(gstin_authentication_status); | ||
}, | ||
|
||
async request_and_authenticate_otp(gstin) { | ||
await frappe.call({ | ||
method: "india_compliance.gst_india.utils.gstr_utils.request_otp", | ||
args: { company_gstin: gstin }, | ||
}); | ||
|
||
// wait for OTP to be authenticated to proceed | ||
await this.authenticate_otp(gstin); | ||
}, | ||
|
||
async authenticate_otp(gstin, error_type = null) { | ||
console.log(gstin, error_type); | ||
if (!error_type) error_type = "otp_requested"; | ||
|
||
let is_authenticated = false; | ||
|
||
while (!is_authenticated) { | ||
const otp = await this.get_gstin_otp(gstin, error_type); | ||
|
||
const { message } = await frappe.call({ | ||
method: "india_compliance.gst_india.utils.gstr_utils.authenticate_otp", | ||
args: { company_gstin: gstin, otp: otp }, | ||
}); | ||
|
||
if ( | ||
message && | ||
["otp_requested", "invalid_otp"].includes(message.error_type) | ||
) { | ||
error_type = message.error_type; | ||
continue; | ||
} | ||
|
||
is_authenticated = true; | ||
return true; | ||
} | ||
}, | ||
}); | ||
|
||
class IndiaComplianceForm extends frappe.ui.form.Form { | ||
taxpayer_api_call(method, args, callback) { | ||
// similar to frappe.ui.form.Form.prototype.call | ||
const opts = { | ||
method: method, | ||
doc: this.doc, | ||
args: args, | ||
callback: callback | ||
} | ||
|
||
opts.original_callback = opts.callback; | ||
opts.callback = (r) => { | ||
if (!r.exc) this.refresh_fields(); | ||
opts.original_callback && opts.original_callback(r); | ||
} | ||
|
||
return taxpayer_api.call(opts); | ||
} | ||
} | ||
|
||
frappe.ui.form.Form = IndiaComplianceForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters