From a7e8a11a13b7c4b04bd63b0fd97828f3a54ee931 Mon Sep 17 00:00:00 2001 From: Felipe Orellana Date: Mon, 5 Aug 2019 12:10:28 -0400 Subject: [PATCH 1/2] Hotfix payment method (#41) * fix: Adds payment method to gateway proxy for backend processing * fix: removed console logs * fix: codacy --- .../gateway_selector_proxy.json | 58 +++++++++++++++++-- .../gateway_selector_proxy.py | 37 +++++++++++- .../test_gateway_selector_proxy.js | 23 ++++++++ .../gateway_selector_settings.py | 13 +++++ gateway_selector/public/js/base.js | 54 +++++++++++++++-- .../pages/integrations/gateway_selector.py | 4 +- 6 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 gateway_selector/gateway_selector/doctype/gateway_selector_proxy/test_gateway_selector_proxy.js diff --git a/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.json b/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.json index f6974e6..7ad327e 100644 --- a/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.json +++ b/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.json @@ -1,5 +1,6 @@ { "allow_copy": 0, + "allow_guest_to_view": 0, "allow_import": 0, "allow_rename": 0, "beta": 0, @@ -12,6 +13,7 @@ "engine": "InnoDB", "fields": [ { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -22,6 +24,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Title", @@ -40,6 +43,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -50,6 +54,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Description", @@ -68,6 +73,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -78,6 +84,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Amount", @@ -96,6 +103,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -106,6 +114,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Currency", @@ -125,6 +134,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -135,6 +145,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Reference Type", @@ -154,6 +165,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -164,6 +176,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Reference", @@ -183,6 +196,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -193,6 +207,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Payer Email", @@ -211,6 +226,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -221,6 +237,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Payer Name", @@ -239,6 +256,7 @@ "unique": 0 }, { + "allow_bulk_edit": 0, "allow_on_submit": 0, "bold": 0, "collapsible": 0, @@ -249,6 +267,7 @@ "ignore_user_permissions": 0, "ignore_xss_filter": 0, "in_filter": 0, + "in_global_search": 0, "in_list_view": 0, "in_standard_filter": 0, "label": "Order ID", @@ -265,20 +284,50 @@ "search_index": 0, "set_only_once": 0, "unique": 0 + }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "gateway_service", + "fieldtype": "Data", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "Gateway Service", + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 } ], + "has_web_view": 0, "hide_heading": 0, "hide_toolbar": 0, "idx": 0, "image_view": 0, "in_create": 0, - "in_dialog": 0, "is_submittable": 0, "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2017-03-09 13:23:48.998563", - "modified_by": "Administrator", + "modified": "2019-08-01 00:01:24.314337", + "modified_by": "forellana@digithinkit.com", "module": "Gateway Selector", "name": "Gateway Selector Proxy", "name_case": "", @@ -294,7 +343,6 @@ "export": 1, "if_owner": 0, "import": 0, - "is_custom": 0, "permlevel": 0, "print": 1, "read": 1, @@ -309,8 +357,10 @@ "quick_entry": 0, "read_only": 0, "read_only_onload": 0, + "show_name_in_global_search": 0, "sort_field": "modified", "sort_order": "DESC", "title_field": "title", + "track_changes": 1, "track_seen": 0 } \ No newline at end of file diff --git a/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.py b/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.py index 708cb60..5c8bcd9 100644 --- a/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.py +++ b/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/gateway_selector_proxy.py @@ -6,12 +6,43 @@ import frappe from frappe.model.document import Document + +def call_hook(hook_name, **kwargs): + hooks = frappe.get_hooks(hook_name) or [] + for hook in hooks: + # don't allow hooks to break processing + try: + frappe.call(hook, **kwargs) + except Exception: + # Hook inception, pass exception to hook listening for exception reporting(sentry) + error_hooks = frappe.get_hooks("error_capture_log") or [] + if len(error_hooks) > 0: + for error_hook in error_hooks: + frappe.call(error_hook, async=True) + else: + log("Error calling hook method: {}->{}".format(hook_name, hook)) + log(frappe.get_traceback()) + class GatewaySelectorProxy(Document): def on_payment_authorized(self, payment_status): - result = frappe.get_doc( + + reference_doc = frappe.get_doc( self.reference_doctype, - self.reference_docname).run_method("on_payment_authorized", - payment_status) + self.reference_docname) + + # This may be a quotation, sales order or sales invoice + order_doc = frappe.get_doc( + reference_doc.reference_doctype, + reference_doc.get("reference_docname", reference_doc.get("reference_name")) + ) + + call_hook("gateway_selector_on_payment_authorized", transaction=self, order=order_doc, pr_result=None) + + result = reference_doc.run_method( + "on_payment_authorized", + payment_status) + + frappe.db.commit() return result diff --git a/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/test_gateway_selector_proxy.js b/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/test_gateway_selector_proxy.js new file mode 100644 index 0000000..0d36836 --- /dev/null +++ b/gateway_selector/gateway_selector/doctype/gateway_selector_proxy/test_gateway_selector_proxy.js @@ -0,0 +1,23 @@ +/* eslint-disable */ +// rename this file from _test_[name] to test_[name] to activate +// and remove above this line + +QUnit.test("test: Gateway Selector Proxy", function (assert) { + let done = assert.async(); + + // number of asserts + assert.expect(1); + + frappe.run_serially([ + // insert a new Gateway Selector Proxy + () => frappe.tests.make('Gateway Selector Proxy', [ + // values to be set + {key: 'value'} + ]), + () => { + assert.equal(cur_frm.doc.key, 'value'); + }, + () => done() + ]); + +}); diff --git a/gateway_selector/gateway_selector/doctype/gateway_selector_settings/gateway_selector_settings.py b/gateway_selector/gateway_selector/doctype/gateway_selector_settings/gateway_selector_settings.py index 57d28fb..6f9a5f7 100755 --- a/gateway_selector/gateway_selector/doctype/gateway_selector_settings/gateway_selector_settings.py +++ b/gateway_selector/gateway_selector/doctype/gateway_selector_settings/gateway_selector_settings.py @@ -41,6 +41,14 @@ def build_proxy(self, **kwargs): "doctype": "Gateway Selector Proxy" } data.update(kwargs) + + # When a PR request is built system references order_id as the PR itself + # here we are checking if we have a Payment Request to update the proxy's + # order_id field to match the PR doctype's name + if kwargs.get("reference_doctype") == 'Payment Request': + pr = frappe.get_doc("Payment Request", kwargs.get("reference_docname")) + data.update({ "order_id": pr.reference_name }) + proxy = frappe.get_doc(data) proxy.flags.ignore_permissions = 1 @@ -173,3 +181,8 @@ def build_embed_context(context, is_backend=False): if gateway.get('embed_form'): context["gateway_scripts"].append(gateway.get('embed_form').get("script_url")) context["gateway_styles"].append(gateway.get('embed_form').get("style_url")) + +@frappe.whitelist() +def update_proxy_gateway(name, gateway_service): + frappe.db.set_value('Gateway Selector Proxy', name, 'gateway_service', gateway_service) + frappe.db.commit() \ No newline at end of file diff --git a/gateway_selector/public/js/base.js b/gateway_selector/public/js/base.js index f113cdc..36bd715 100644 --- a/gateway_selector/public/js/base.js +++ b/gateway_selector/public/js/base.js @@ -5,8 +5,6 @@ frappe.gateway_selector.AddressFormProvider = Class.extend({ init: function($form) { this.data = {}; this.$form = $form; - - console.log("address form initialized") }, form: function() { @@ -157,7 +155,6 @@ frappe.gateway_selector._generic_embed = Class.extend({ var _server_messages = JSON.parse(xhr.responseJSON._server_messages); } - var errors = []; if ( _server_messages ) { try { for(var i = 0; i < _server_messages.length; i++) { @@ -196,8 +193,55 @@ frappe.integration_service.gateway_selector_gateway = Class.extend({ process: function(overrides, callback) { if ( this.current_gateway ) { - this.current_gateway.collect(); - this.current_gateway.process(overrides, callback); + let processCall = () => { + this.current_gateway.collect(); + this.current_gateway.process(overrides, callback); + }; + + // fuggly way of updating gateway proxy record with gateway selection. + // we first update the record, wait, then do the actual processing. + if ( this.request_data.proxy_name ) { + frappe.call({ + method: "gateway_selector.gateway_selector.doctype.gateway_selector_settings.gateway_selector_settings.update_proxy_gateway", + args: { + name: this.request_data.proxy_name, + gateway_service: this.current_gateway_name + } + }) + .done(() => processCall()) + .fail((xhr, textStatus) => { + if(typeof data === "string") data = JSON.parse(data); + let status = xhr.statusCode().status; + let _server_messages = false; + let errors = []; + if (xhr.responseJSON && xhr.responseJSON._server_messages) { + _server_messages = JSON.parse(xhr.responseJSON._server_messages); + } + + if ( _server_messages ) { + try { + for(let i = 0; i < _server_messages.length; i++) { + errors.push("Server Error: " + JSON.parse(_server_messages[i]).message); + } + } catch(ex) { + errors.push(_server_messages); + errors.push(ex); + } + } + + callback({ + errors: errors, + status: status, + recoverable: 0, + xhr: xhr, + textStatus: textStatus + }, null); + + }); + } else { + processCall(); + } + } }, diff --git a/gateway_selector/templates/pages/integrations/gateway_selector.py b/gateway_selector/templates/pages/integrations/gateway_selector.py index 24aa36d..f5e27d6 100644 --- a/gateway_selector/templates/pages/integrations/gateway_selector.py +++ b/gateway_selector/templates/pages/integrations/gateway_selector.py @@ -59,7 +59,6 @@ def get_context(context): raise frappe.Redirect if not pr_access and frappe.session.user == 'Guest': - print("Guest user") frappe.throw(_("You need to be logged in to access this page"), frappe.PermissionError) context["is_backend"]=1 @@ -67,6 +66,9 @@ def get_context(context): if proxy_name and proxy: context["data"] = { key: proxy.get(key) for key in expected_keys } + context["data"]["proxy_name"] = proxy.name + context["data"]["reference_doctype"] = "Gateway Selector Proxy" + context["data"]["reference_docname"] = proxy.name context["billing_countries"] = [ x for x in frappe.get_list("Country", fields=["country_name", "name"], ignore_permissions=1) ] From 8cc6140318c9ff9d70baf9d4c468d66e12887bd2 Mon Sep 17 00:00:00 2001 From: Release Bot Date: Wed, 7 Aug 2019 14:25:26 +0000 Subject: [PATCH 2/2] bumped to version 1.0.5 --- gateway_selector/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway_selector/__init__.py b/gateway_selector/__init__.py index ff9dab4..2805cf3 100644 --- a/gateway_selector/__init__.py +++ b/gateway_selector/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -__version__ = '1.0.4' +__version__ = '1.0.5'