Skip to content

Commit

Permalink
refactor: make payments app a soft dependency (#33245)
Browse files Browse the repository at this point in the history
refactor: make payment app a soft dependency
  • Loading branch information
ankush authored Dec 8, 2022
1 parent 07c815f commit 0b86b1b
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/helper/site_config_mariadb.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"root_login": "root",
"root_password": "root",
"host_name": "http://test_site:8000",
"install_apps": ["erpnext"],
"install_apps": ["payments", "erpnext"],
"throttle_user_limit": 100
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

frappe.ui.form.on('Payment Gateway Account', {
refresh(frm) {
erpnext.utils.check_payments_app();
if(!frm.doc.__islocal) {
frm.set_df_property('payment_gateway', 'read_only', 1);
}
Expand Down
15 changes: 11 additions & 4 deletions erpnext/accounts/doctype/payment_request/payment_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from frappe.model.document import Document
from frappe.utils import flt, get_url, nowdate
from frappe.utils.background_jobs import enqueue
from payments.utils import get_payment_gateway_controller

from erpnext.accounts.doctype.payment_entry.payment_entry import (
get_company_defaults,
Expand All @@ -19,6 +18,14 @@
from erpnext.accounts.party import get_party_account, get_party_bank_account
from erpnext.accounts.utils import get_account_currency
from erpnext.erpnext_integrations.stripe_integration import create_stripe_subscription
from erpnext.utilities import payment_app_import_guard


def _get_payment_gateway_controller(*args, **kwargs):
with payment_app_import_guard():
from payments.utils import get_payment_gateway_controller

return get_payment_gateway_controller(*args, **kwargs)


class PaymentRequest(Document):
Expand Down Expand Up @@ -107,7 +114,7 @@ def on_submit(self):
self.request_phone_payment()

def request_phone_payment(self):
controller = get_payment_gateway_controller(self.payment_gateway)
controller = _get_payment_gateway_controller(self.payment_gateway)
request_amount = self.get_request_amount()

payment_record = dict(
Expand Down Expand Up @@ -156,7 +163,7 @@ def make_invoice(self):

def payment_gateway_validation(self):
try:
controller = get_payment_gateway_controller(self.payment_gateway)
controller = _get_payment_gateway_controller(self.payment_gateway)
if hasattr(controller, "on_payment_request_submission"):
return controller.on_payment_request_submission(self)
else:
Expand Down Expand Up @@ -189,7 +196,7 @@ def get_payment_url(self):
)
data.update({"company": frappe.defaults.get_defaults().company})

controller = get_payment_gateway_controller(self.payment_gateway)
controller = _get_payment_gateway_controller(self.payment_gateway)
controller.validate_transaction_currency(self.currency)

if hasattr(controller, "validate_minimum_transaction_amount"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ frappe.ui.form.on('Subscription Plan', {
price_determination: function(frm) {
frm.toggle_reqd("cost", frm.doc.price_determination === 'Fixed rate');
frm.toggle_reqd("price_list", frm.doc.price_determination === 'Based on price list');
}
},

subscription_plan: function (frm) {
erpnext.utils.check_payments_app();
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@ frappe.ui.form.on("E Commerce Settings", {
frm.set_value('default_customer_group', '');
frm.set_value('quotation_series', '');
}
},

enable_checkout: function(frm) {
if (frm.doc.enable_checkout) {
erpnext.utils.check_payments_app();
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
// For license information, please see license.txt

frappe.ui.form.on('GoCardless Settings', {
refresh: function(frm) {
erpnext.utils.check_payments_app();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 0,
"modified": "2018-02-12 14:18:47.209114",
"modified": "2022-02-12 14:18:47.209114",
"modified_by": "Administrator",
"module": "ERPNext Integrations",
"name": "GoCardless Settings",
Expand Down Expand Up @@ -201,7 +201,6 @@
"write": 1
}
],
"quick_entry": 1,
"read_only": 0,
"read_only_onload": 0,
"show_name_in_global_search": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from frappe.integrations.utils import create_request_log
from frappe.model.document import Document
from frappe.utils import call_hook_method, cint, flt, get_url
from payments.utils import create_payment_gateway

from erpnext.utilities import payment_app_import_guard


class GoCardlessSettings(Document):
Expand All @@ -30,6 +31,9 @@ def initialize_client(self):
frappe.throw(e)

def on_update(self):
with payment_app_import_guard():
from payments.utils import create_payment_gateway

create_payment_gateway(
"GoCardless-" + self.gateway_name, settings="GoCardLess Settings", controller=self.gateway_name
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ frappe.ui.form.on('Mpesa Settings', {
},

refresh: function(frm) {
erpnext.utils.check_payments_app();

frappe.realtime.on("refresh_mpesa_dashboard", function(){
frm.reload_doc();
frm.events.setup_account_balance_html(frm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from frappe.integrations.utils import create_request_log
from frappe.model.document import Document
from frappe.utils import call_hook_method, fmt_money, get_request_site_address
from payments.utils import create_payment_gateway

from erpnext.erpnext_integrations.doctype.mpesa_settings.mpesa_connector import MpesaConnector
from erpnext.erpnext_integrations.doctype.mpesa_settings.mpesa_custom_fields import (
create_custom_pos_fields,
)
from erpnext.erpnext_integrations.utils import create_mode_of_payment
from erpnext.utilities import payment_app_import_guard


class MpesaSettings(Document):
Expand All @@ -30,6 +30,9 @@ def validate_transaction_currency(self, currency):
)

def on_update(self):
with payment_app_import_guard():
from payments.utils import create_payment_gateway

create_custom_pos_fields()
create_payment_gateway(
"Mpesa-" + self.payment_gateway_name,
Expand Down
9 changes: 8 additions & 1 deletion erpnext/erpnext_integrations/stripe_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
# For license information, please see license.txt

import frappe
import stripe
from frappe import _
from frappe.integrations.utils import create_request_log

from erpnext.utilities import payment_app_import_guard


def create_stripe_subscription(gateway_controller, data):
with payment_app_import_guard():
import stripe

stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller)
stripe_settings.data = frappe._dict(data)

Expand Down Expand Up @@ -35,6 +39,9 @@ def create_stripe_subscription(gateway_controller, data):


def create_subscription_on_stripe(stripe_settings):
with payment_app_import_guard():
import stripe

items = []
for payment_plan in stripe_settings.payment_plans:
plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "product_price_id")
Expand Down
1 change: 0 additions & 1 deletion erpnext/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
app_license = "GNU General Public License (v3)"
source_link = "https://github.com/frappe/erpnext"
app_logo_url = "/assets/erpnext/images/erpnext-logo.svg"
required_apps = ["payments"]


develop_version = "14.x.x-develop"
Expand Down
12 changes: 11 additions & 1 deletion erpnext/public/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,18 @@ $.extend(erpnext.utils, {
}
frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
});
}
},

// check if payments app is installed on site, if not warn user.
check_payments_app: () => {
if (frappe.boot.versions && !frappe.boot.versions.payments) {
const marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/payments">Marketplace</a>'
const github_link = '<a href="https://github.com/frappe/payments/">GitHub</a>'
const msg = __("payments app is not installed. Please install it from {0} or {1}", [marketplace_link, github_link])
frappe.msgprint(msg);
}

},
});

erpnext.utils.select_alternate_items = function(opts) {
Expand Down
16 changes: 16 additions & 0 deletions erpnext/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## temp utility

from contextlib import contextmanager

import frappe
from frappe import _
from frappe.utils import cstr

from erpnext.utilities.activation import get_level
Expand Down Expand Up @@ -35,3 +38,16 @@ def get_site_info(site_info):
domain = frappe.get_cached_value("Company", cstr(company), "domain")

return {"company": company, "domain": domain, "activation": get_level()}


@contextmanager
def payment_app_import_guard():
marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/payments">Marketplace</a>'
github_link = '<a href="https://github.com/frappe/payments/">GitHub</a>'
msg = _("payments app is not installed. Please install it from {} or {}").format(
marketplace_link, github_link
)
try:
yield
except ImportError:
frappe.throw(msg, title=_("Missing Payments App"))

0 comments on commit 0b86b1b

Please sign in to comment.