blob: bb5c0c2dd1022d7386b806d82715a63036aeb6c0 [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05302
3import base64
4import hashlib
5import hmac
6
Saurabhd60c0f22018-05-16 11:33:47 +05307import frappe
8from frappe import _
Saurabhd60c0f22018-05-16 11:33:47 +05309from six.moves.urllib.parse import urlparse
Chillar Anand915b3432021-09-02 16:44:59 +053010
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053011from erpnext import get_default_company
Saurabhd60c0f22018-05-16 11:33:47 +053012
Chillar Anand915b3432021-09-02 16:44:59 +053013
Saurabhd60c0f22018-05-16 11:33:47 +053014def validate_webhooks_request(doctype, hmac_key, secret_key='secret'):
15 def innerfn(fn):
16 settings = frappe.get_doc(doctype)
17
18 if frappe.request and settings and settings.get(secret_key) and not frappe.flags.in_test:
19 sig = base64.b64encode(
20 hmac.new(
21 settings.get(secret_key).encode('utf8'),
22 frappe.request.data,
23 hashlib.sha256
24 ).digest()
25 )
26
27 if frappe.request.data and \
28 frappe.get_request_header(hmac_key) and \
29 not sig == bytes(frappe.get_request_header(hmac_key).encode()):
30 frappe.throw(_("Unverified Webhook Data"))
31 frappe.set_user(settings.modified_by)
32
33 return fn
34
35 return innerfn
36
Ankush Menate28165e2021-05-07 20:27:51 +053037def get_webhook_address(connector_name, method, exclude_uri=False, force_https=False):
Saurabhd60c0f22018-05-16 11:33:47 +053038 endpoint = "erpnext.erpnext_integrations.connectors.{0}.{1}".format(connector_name, method)
39
40 if exclude_uri:
41 return endpoint
42
43 try:
44 url = frappe.request.url
45 except RuntimeError:
hrwx5aa87432019-09-13 17:04:01 +000046 url = "http://localhost:8000"
Saurabhd60c0f22018-05-16 11:33:47 +053047
Ankush Menate28165e2021-05-07 20:27:51 +053048 url_data = urlparse(url)
49 scheme = "https" if force_https else url_data.scheme
50 netloc = url_data.netloc
51
52 server_url = f"{scheme}://{netloc}/api/method/{endpoint}"
Saurabhd60c0f22018-05-16 11:33:47 +053053
hrwx5aa87432019-09-13 17:04:01 +000054 return server_url
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053055
Mangesh-Khairnar1bef6a52020-10-04 13:01:11 +053056def create_mode_of_payment(gateway, payment_type="General"):
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053057 payment_gateway_account = frappe.db.get_value("Payment Gateway Account", {
58 "payment_gateway": gateway
59 }, ['payment_account'])
60
Ankush Menat4551d7d2021-08-19 13:41:10 +053061 mode_of_payment = frappe.db.exists("Mode of Payment", gateway)
Deepesh Gargb2af6b42021-07-05 13:46:03 +053062 if not mode_of_payment and payment_gateway_account:
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053063 mode_of_payment = frappe.get_doc({
64 "doctype": "Mode of Payment",
65 "mode_of_payment": gateway,
66 "enabled": 1,
Mangesh-Khairnar1bef6a52020-10-04 13:01:11 +053067 "type": payment_type,
68 "accounts": [{
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053069 "doctype": "Mode of Payment Account",
70 "company": get_default_company(),
71 "default_account": payment_gateway_account
Mangesh-Khairnar1bef6a52020-10-04 13:01:11 +053072 }]
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053073 })
jbienesdev1c9410e2020-07-13 16:25:09 +080074 mode_of_payment.insert(ignore_permissions=True)
75
Deepesh Gargb2af6b42021-07-05 13:46:03 +053076 return mode_of_payment
Deepesh Garg3ef394c2021-07-05 14:45:33 +053077 elif mode_of_payment:
Deepesh Gargb2af6b42021-07-05 13:46:03 +053078 return frappe.get_doc("Mode of Payment", mode_of_payment)
79
jbienesdev1c9410e2020-07-13 16:25:09 +080080def get_tracking_url(carrier, tracking_number):
81 # Return the formatted Tracking URL.
82 tracking_url = ''
83 url_reference = frappe.get_value('Parcel Service', carrier, 'url_reference')
84 if url_reference:
85 tracking_url = frappe.render_template(url_reference, {'tracking_number': tracking_number})
jbienesdev1c9410e2020-07-13 16:25:09 +080086 return tracking_url