Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | import base64 |
| 2 | import hashlib |
| 3 | import hmac |
| 4 | |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe import _ |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 7 | from six.moves.urllib.parse import urlparse |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
Mangesh-Khairnar | 27f81e0 | 2020-09-15 14:38:06 +0530 | [diff] [blame] | 9 | from erpnext import get_default_company |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 10 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 12 | def 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 \ |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 26 | 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 Menat | e28165e | 2021-05-07 20:27:51 +0530 | [diff] [blame] | 34 | def get_webhook_address(connector_name, method, exclude_uri=False, force_https=False): |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 35 | 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: |
hrwx | 5aa8743 | 2019-09-13 17:04:01 +0000 | [diff] [blame] | 43 | url = "http://localhost:8000" |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 44 | |
Ankush Menat | e28165e | 2021-05-07 20:27:51 +0530 | [diff] [blame] | 45 | 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}" |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 50 | |
hrwx | 5aa8743 | 2019-09-13 17:04:01 +0000 | [diff] [blame] | 51 | return server_url |
Mangesh-Khairnar | 27f81e0 | 2020-09-15 14:38:06 +0530 | [diff] [blame] | 52 | |
Mangesh-Khairnar | 1bef6a5 | 2020-10-04 13:01:11 +0530 | [diff] [blame] | 53 | def create_mode_of_payment(gateway, payment_type="General"): |
Mangesh-Khairnar | 27f81e0 | 2020-09-15 14:38:06 +0530 | [diff] [blame] | 54 | payment_gateway_account = frappe.db.get_value("Payment Gateway Account", { |
| 55 | "payment_gateway": gateway |
| 56 | }, ['payment_account']) |
| 57 | |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 58 | mode_of_payment = frappe.db.exists("Mode of Payment", gateway) |
Deepesh Garg | b2af6b4 | 2021-07-05 13:46:03 +0530 | [diff] [blame] | 59 | if not mode_of_payment and payment_gateway_account: |
Mangesh-Khairnar | 27f81e0 | 2020-09-15 14:38:06 +0530 | [diff] [blame] | 60 | mode_of_payment = frappe.get_doc({ |
| 61 | "doctype": "Mode of Payment", |
| 62 | "mode_of_payment": gateway, |
| 63 | "enabled": 1, |
Mangesh-Khairnar | 1bef6a5 | 2020-10-04 13:01:11 +0530 | [diff] [blame] | 64 | "type": payment_type, |
| 65 | "accounts": [{ |
Mangesh-Khairnar | 27f81e0 | 2020-09-15 14:38:06 +0530 | [diff] [blame] | 66 | "doctype": "Mode of Payment Account", |
| 67 | "company": get_default_company(), |
| 68 | "default_account": payment_gateway_account |
Mangesh-Khairnar | 1bef6a5 | 2020-10-04 13:01:11 +0530 | [diff] [blame] | 69 | }] |
Mangesh-Khairnar | 27f81e0 | 2020-09-15 14:38:06 +0530 | [diff] [blame] | 70 | }) |
jbienesdev | 1c9410e | 2020-07-13 16:25:09 +0800 | [diff] [blame] | 71 | mode_of_payment.insert(ignore_permissions=True) |
| 72 | |
Deepesh Garg | b2af6b4 | 2021-07-05 13:46:03 +0530 | [diff] [blame] | 73 | return mode_of_payment |
Deepesh Garg | 3ef394c | 2021-07-05 14:45:33 +0530 | [diff] [blame] | 74 | elif mode_of_payment: |
Deepesh Garg | b2af6b4 | 2021-07-05 13:46:03 +0530 | [diff] [blame] | 75 | return frappe.get_doc("Mode of Payment", mode_of_payment) |
| 76 | |
jbienesdev | 1c9410e | 2020-07-13 16:25:09 +0800 | [diff] [blame] | 77 | def 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}) |
jbienesdev | 1c9410e | 2020-07-13 16:25:09 +0800 | [diff] [blame] | 83 | return tracking_url |