2018-05-16 06:03:47 +00:00
|
|
|
import base64
|
|
|
|
import hashlib
|
|
|
|
import hmac
|
2022-01-27 14:38:05 +00:00
|
|
|
from urllib.parse import urlparse
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-05-16 06:03:47 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-09-15 09:08:06 +00:00
|
|
|
from erpnext import get_default_company
|
2018-05-16 06:03:47 +00:00
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-05-16 06:03:47 +00:00
|
|
|
def validate_webhooks_request(doctype, hmac_key, secret_key="secret"):
|
|
|
|
def innerfn(fn):
|
|
|
|
settings = frappe.get_doc(doctype)
|
|
|
|
|
|
|
|
if frappe.request and settings and settings.get(secret_key) and not frappe.flags.in_test:
|
|
|
|
sig = base64.b64encode(
|
|
|
|
hmac.new(settings.get(secret_key).encode("utf8"), frappe.request.data, hashlib.sha256).digest()
|
|
|
|
)
|
|
|
|
|
|
|
|
if frappe.request.data and not sig == bytes(frappe.get_request_header(hmac_key).encode()):
|
|
|
|
frappe.throw(_("Unverified Webhook Data"))
|
|
|
|
frappe.set_user(settings.modified_by)
|
|
|
|
|
|
|
|
return fn
|
|
|
|
|
|
|
|
return innerfn
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-05-07 14:57:51 +00:00
|
|
|
def get_webhook_address(connector_name, method, exclude_uri=False, force_https=False):
|
2018-05-16 06:03:47 +00:00
|
|
|
endpoint = "erpnext.erpnext_integrations.connectors.{0}.{1}".format(connector_name, method)
|
|
|
|
|
|
|
|
if exclude_uri:
|
|
|
|
return endpoint
|
|
|
|
|
|
|
|
try:
|
|
|
|
url = frappe.request.url
|
|
|
|
except RuntimeError:
|
2019-09-13 17:04:01 +00:00
|
|
|
url = "http://localhost:8000"
|
2018-05-16 06:03:47 +00:00
|
|
|
|
2021-05-07 14:57:51 +00:00
|
|
|
url_data = urlparse(url)
|
|
|
|
scheme = "https" if force_https else url_data.scheme
|
|
|
|
netloc = url_data.netloc
|
|
|
|
|
|
|
|
server_url = f"{scheme}://{netloc}/api/method/{endpoint}"
|
2018-05-16 06:03:47 +00:00
|
|
|
|
2019-09-13 17:04:01 +00:00
|
|
|
return server_url
|
2020-09-15 09:08:06 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-10-04 07:31:11 +00:00
|
|
|
def create_mode_of_payment(gateway, payment_type="General"):
|
2020-09-15 09:08:06 +00:00
|
|
|
payment_gateway_account = frappe.db.get_value(
|
|
|
|
"Payment Gateway Account", {"payment_gateway": gateway}, ["payment_account"]
|
|
|
|
)
|
|
|
|
|
2021-07-05 08:16:03 +00:00
|
|
|
mode_of_payment = frappe.db.exists("Mode of Payment", gateway)
|
|
|
|
if not mode_of_payment and payment_gateway_account:
|
2020-09-15 09:08:06 +00:00
|
|
|
mode_of_payment = frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "Mode of Payment",
|
|
|
|
"mode_of_payment": gateway,
|
|
|
|
"enabled": 1,
|
2020-10-04 07:31:11 +00:00
|
|
|
"type": payment_type,
|
|
|
|
"accounts": [
|
|
|
|
{
|
2020-09-15 09:08:06 +00:00
|
|
|
"doctype": "Mode of Payment Account",
|
|
|
|
"company": get_default_company(),
|
|
|
|
"default_account": payment_gateway_account,
|
2022-03-28 13:22:46 +00:00
|
|
|
}
|
|
|
|
],
|
2020-10-04 07:31:11 +00:00
|
|
|
}
|
2020-09-15 09:08:06 +00:00
|
|
|
)
|
2020-07-13 08:25:09 +00:00
|
|
|
mode_of_payment.insert(ignore_permissions=True)
|
|
|
|
|
2021-07-05 08:16:03 +00:00
|
|
|
return mode_of_payment
|
2021-07-05 09:15:33 +00:00
|
|
|
elif mode_of_payment:
|
2021-07-05 08:16:03 +00:00
|
|
|
return frappe.get_doc("Mode of Payment", mode_of_payment)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-13 08:25:09 +00:00
|
|
|
def get_tracking_url(carrier, tracking_number):
|
|
|
|
# Return the formatted Tracking URL.
|
|
|
|
tracking_url = ""
|
|
|
|
url_reference = frappe.get_value("Parcel Service", carrier, "url_reference")
|
|
|
|
if url_reference:
|
|
|
|
tracking_url = frappe.render_template(url_reference, {"tracking_number": tracking_number})
|
|
|
|
return tracking_url
|