blob: 30d3948fee7e712e969d9d75a88af73848a5301b [file] [log] [blame]
Chillar Anand915b3432021-09-02 16:44:59 +05301import base64
2import hashlib
3import hmac
Ankush Menatb05287d2022-01-27 20:08:05 +05304from urllib.parse import urlparse
Chillar Anand915b3432021-09-02 16:44:59 +05305
Saurabhd60c0f22018-05-16 11:33:47 +05306import frappe
7from frappe import _
Chillar Anand915b3432021-09-02 16:44:59 +05308
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +05309from erpnext import get_default_company
Saurabhd60c0f22018-05-16 11:33:47 +053010
Chillar Anand915b3432021-09-02 16:44:59 +053011
Saurabhd60c0f22018-05-16 11:33:47 +053012def validate_webhooks_request(doctype, hmac_key, secret_key='secret'):
13 def innerfn(fn):
14 settings = frappe.get_doc(doctype)
15
16 if frappe.request and settings and settings.get(secret_key) and not frappe.flags.in_test:
17 sig = base64.b64encode(
18 hmac.new(
19 settings.get(secret_key).encode('utf8'),
20 frappe.request.data,
21 hashlib.sha256
22 ).digest()
23 )
24
25 if frappe.request.data and \
Saurabhd60c0f22018-05-16 11:33:47 +053026 not sig == bytes(frappe.get_request_header(hmac_key).encode()):
27 frappe.throw(_("Unverified Webhook Data"))
28 frappe.set_user(settings.modified_by)
29
30 return fn
31
32 return innerfn
33
Ankush Menate28165e2021-05-07 20:27:51 +053034def get_webhook_address(connector_name, method, exclude_uri=False, force_https=False):
Saurabhd60c0f22018-05-16 11:33:47 +053035 endpoint = "erpnext.erpnext_integrations.connectors.{0}.{1}".format(connector_name, method)
36
37 if exclude_uri:
38 return endpoint
39
40 try:
41 url = frappe.request.url
42 except RuntimeError:
hrwx5aa87432019-09-13 17:04:01 +000043 url = "http://localhost:8000"
Saurabhd60c0f22018-05-16 11:33:47 +053044
Ankush Menate28165e2021-05-07 20:27:51 +053045 url_data = urlparse(url)
46 scheme = "https" if force_https else url_data.scheme
47 netloc = url_data.netloc
48
49 server_url = f"{scheme}://{netloc}/api/method/{endpoint}"
Saurabhd60c0f22018-05-16 11:33:47 +053050
hrwx5aa87432019-09-13 17:04:01 +000051 return server_url
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053052
Mangesh-Khairnar1bef6a52020-10-04 13:01:11 +053053def create_mode_of_payment(gateway, payment_type="General"):
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053054 payment_gateway_account = frappe.db.get_value("Payment Gateway Account", {
55 "payment_gateway": gateway
56 }, ['payment_account'])
57
Ankush Menat4551d7d2021-08-19 13:41:10 +053058 mode_of_payment = frappe.db.exists("Mode of Payment", gateway)
Deepesh Gargb2af6b42021-07-05 13:46:03 +053059 if not mode_of_payment and payment_gateway_account:
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053060 mode_of_payment = frappe.get_doc({
61 "doctype": "Mode of Payment",
62 "mode_of_payment": gateway,
63 "enabled": 1,
Mangesh-Khairnar1bef6a52020-10-04 13:01:11 +053064 "type": payment_type,
65 "accounts": [{
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053066 "doctype": "Mode of Payment Account",
67 "company": get_default_company(),
68 "default_account": payment_gateway_account
Mangesh-Khairnar1bef6a52020-10-04 13:01:11 +053069 }]
Mangesh-Khairnar27f81e02020-09-15 14:38:06 +053070 })
jbienesdev1c9410e2020-07-13 16:25:09 +080071 mode_of_payment.insert(ignore_permissions=True)
72
Deepesh Gargb2af6b42021-07-05 13:46:03 +053073 return mode_of_payment
Deepesh Garg3ef394c2021-07-05 14:45:33 +053074 elif mode_of_payment:
Deepesh Gargb2af6b42021-07-05 13:46:03 +053075 return frappe.get_doc("Mode of Payment", mode_of_payment)
76
jbienesdev1c9410e2020-07-13 16:25:09 +080077def get_tracking_url(carrier, tracking_number):
78 # Return the formatted Tracking URL.
79 tracking_url = ''
80 url_reference = frappe.get_value('Parcel Service', carrier, 'url_reference')
81 if url_reference:
82 tracking_url = frappe.render_template(url_reference, {'tracking_number': tracking_number})
jbienesdev1c9410e2020-07-13 16:25:09 +080083 return tracking_url