2018-06-20 17:51:52 +00:00
|
|
|
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-06-20 17:51:52 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-06-20 17:51:52 +00:00
|
|
|
from erpnext.erpnext_integrations.doctype.gocardless_settings.gocardless_settings import (
|
|
|
|
get_gateway_controller,
|
|
|
|
gocardless_initialization,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2018-06-20 17:51:52 +00:00
|
|
|
|
|
|
|
no_cache = 1
|
|
|
|
|
|
|
|
expected_keys = ("redirect_flow_id", "reference_doctype", "reference_docname")
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-20 17:51:52 +00:00
|
|
|
|
|
|
|
def get_context(context):
|
|
|
|
context.no_cache = 1
|
|
|
|
|
|
|
|
# all these keys exist in form_dict
|
|
|
|
if not (set(expected_keys) - set(frappe.form_dict.keys())):
|
|
|
|
for key in expected_keys:
|
|
|
|
context[key] = frappe.form_dict[key]
|
|
|
|
|
|
|
|
else:
|
|
|
|
frappe.redirect_to_message(
|
|
|
|
_("Some information is missing"),
|
|
|
|
_("Looks like someone sent you to an incomplete URL. Please ask them to look into it."),
|
|
|
|
)
|
|
|
|
frappe.local.flags.redirect_location = frappe.local.response.location
|
|
|
|
raise frappe.Redirect
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-20 17:51:52 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def confirm_payment(redirect_flow_id, reference_doctype, reference_docname):
|
|
|
|
|
|
|
|
client = gocardless_initialization(reference_docname)
|
|
|
|
|
|
|
|
try:
|
|
|
|
redirect_flow = client.redirect_flows.complete(
|
|
|
|
redirect_flow_id, params={"session_token": frappe.session.user}
|
|
|
|
)
|
|
|
|
|
2018-07-17 16:15:30 +00:00
|
|
|
confirmation_url = redirect_flow.confirmation_url
|
|
|
|
gocardless_success_page = frappe.get_hooks("gocardless_success_page")
|
|
|
|
if gocardless_success_page:
|
|
|
|
confirmation_url = frappe.get_attr(gocardless_success_page[-1])(
|
|
|
|
reference_doctype, reference_docname
|
|
|
|
)
|
|
|
|
|
2018-06-20 17:51:52 +00:00
|
|
|
data = {
|
|
|
|
"mandate": redirect_flow.links.mandate,
|
|
|
|
"customer": redirect_flow.links.customer,
|
2018-07-17 16:15:30 +00:00
|
|
|
"redirect_to": confirmation_url,
|
2018-06-20 17:51:52 +00:00
|
|
|
"redirect_message": "Mandate successfully created",
|
|
|
|
"reference_doctype": reference_doctype,
|
|
|
|
"reference_docname": reference_docname,
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
create_mandate(data)
|
|
|
|
except Exception as e:
|
2022-05-02 09:34:26 +00:00
|
|
|
frappe.log_error("GoCardless Mandate Registration Error")
|
2018-06-20 17:51:52 +00:00
|
|
|
|
|
|
|
gateway_controller = get_gateway_controller(reference_docname)
|
|
|
|
frappe.get_doc("GoCardless Settings", gateway_controller).create_payment_request(data)
|
|
|
|
|
2018-07-17 16:15:30 +00:00
|
|
|
return {"redirect_to": confirmation_url}
|
2018-06-20 17:51:52 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
2022-05-02 09:34:26 +00:00
|
|
|
frappe.log_error("GoCardless Payment Error")
|
2018-06-20 17:51:52 +00:00
|
|
|
return {"redirect_to": "/integrations/payment-failed"}
|
|
|
|
|
|
|
|
|
|
|
|
def create_mandate(data):
|
|
|
|
data = frappe._dict(data)
|
|
|
|
frappe.logger().debug(data)
|
|
|
|
|
|
|
|
mandate = data.get("mandate")
|
|
|
|
|
|
|
|
if frappe.db.exists("GoCardless Mandate", mandate):
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
reference_doc = frappe.db.get_value(
|
|
|
|
data.get("reference_doctype"),
|
|
|
|
data.get("reference_docname"),
|
|
|
|
["reference_doctype", "reference_name"],
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
erpnext_customer = frappe.db.get_value(
|
|
|
|
reference_doc.reference_doctype, reference_doc.reference_name, ["customer_name"], as_dict=1
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2018-06-20 17:51:52 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "GoCardless Mandate",
|
|
|
|
"mandate": mandate,
|
|
|
|
"customer": erpnext_customer.customer_name,
|
|
|
|
"gocardless_customer": data.get("customer"),
|
|
|
|
}
|
|
|
|
).insert(ignore_permissions=True)
|
|
|
|
|
|
|
|
except Exception:
|
2022-05-02 09:34:26 +00:00
|
|
|
frappe.log_error("Gocardless: Unable to create mandate")
|