-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: get
GoCardless Templates
from ERPNext
- Loading branch information
Showing
5 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
16 changes: 16 additions & 0 deletions
16
payments/payments/templates/pages/gocardless_checkout.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends "templates/web.html" %} | ||
|
||
{% block title %} Payment {% endblock %} | ||
|
||
{%- block header -%}{% endblock %} | ||
|
||
{% block script %} | ||
<script>{% include "templates/includes/integrations/gocardless_checkout.js" %}</script> | ||
{% endblock %} | ||
|
||
{%- block page_content -%} | ||
<p class='lead text-center'> | ||
<span class='gocardless-loading'>{{ _("Loading Payment System") }}</span> | ||
</p> | ||
|
||
{% endblock %} |
100 changes: 100 additions & 0 deletions
100
payments/payments/templates/pages/gocardless_checkout.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors | ||
# License: GNU General Public License v3. See license.txt | ||
|
||
import json | ||
|
||
import frappe | ||
from frappe import _ | ||
from frappe.utils import flt, get_url | ||
|
||
from erpnext.erpnext_integrations.doctype.gocardless_settings.gocardless_settings import ( | ||
get_gateway_controller, | ||
gocardless_initialization, | ||
) | ||
|
||
no_cache = 1 | ||
|
||
expected_keys = ( | ||
"amount", | ||
"title", | ||
"description", | ||
"reference_doctype", | ||
"reference_docname", | ||
"payer_name", | ||
"payer_email", | ||
"order_id", | ||
"currency", | ||
) | ||
|
||
|
||
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] | ||
|
||
context["amount"] = flt(context["amount"]) | ||
|
||
gateway_controller = get_gateway_controller(context.reference_docname) | ||
context["header_img"] = frappe.db.get_value( | ||
"GoCardless Settings", gateway_controller, "header_img" | ||
) | ||
|
||
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 | ||
|
||
|
||
@frappe.whitelist(allow_guest=True) | ||
def check_mandate(data, reference_doctype, reference_docname): | ||
data = json.loads(data) | ||
|
||
client = gocardless_initialization(reference_docname) | ||
|
||
payer = frappe.get_doc("Customer", data["payer_name"]) | ||
|
||
if payer.customer_type == "Individual" and payer.customer_primary_contact is not None: | ||
primary_contact = frappe.get_doc("Contact", payer.customer_primary_contact) | ||
prefilled_customer = { | ||
"company_name": payer.name, | ||
"given_name": primary_contact.first_name, | ||
} | ||
if primary_contact.last_name is not None: | ||
prefilled_customer.update({"family_name": primary_contact.last_name}) | ||
|
||
if primary_contact.email_id is not None: | ||
prefilled_customer.update({"email": primary_contact.email_id}) | ||
else: | ||
prefilled_customer.update({"email": frappe.session.user}) | ||
|
||
else: | ||
prefilled_customer = {"company_name": payer.name, "email": frappe.session.user} | ||
|
||
success_url = get_url( | ||
"./integrations/gocardless_confirmation?reference_doctype=" | ||
+ reference_doctype | ||
+ "&reference_docname=" | ||
+ reference_docname | ||
) | ||
|
||
try: | ||
redirect_flow = client.redirect_flows.create( | ||
params={ | ||
"description": _("Pay {0} {1}").format(data["amount"], data["currency"]), | ||
"session_token": frappe.session.user, | ||
"success_redirect_url": success_url, | ||
"prefilled_customer": prefilled_customer, | ||
} | ||
) | ||
|
||
return {"redirect_to": redirect_flow.redirect_url} | ||
|
||
except Exception as e: | ||
frappe.log_error("GoCardless Payment Error") | ||
return {"redirect_to": "/integrations/payment-failed"} |
16 changes: 16 additions & 0 deletions
16
payments/payments/templates/pages/gocardless_confirmation.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends "templates/web.html" %} | ||
|
||
{% block title %} Payment {% endblock %} | ||
|
||
{%- block header -%}{% endblock %} | ||
|
||
{% block script %} | ||
<script>{% include "templates/includes/integrations/gocardless_confirmation.js" %}</script> | ||
{% endblock %} | ||
|
||
{%- block page_content -%} | ||
<p class='lead text-center'> | ||
<span class='gocardless-loading'>{{ _("Payment Confirmation") }}</span> | ||
</p> | ||
|
||
{% endblock %} |
106 changes: 106 additions & 0 deletions
106
payments/payments/templates/pages/gocardless_confirmation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors | ||
# License: GNU General Public License v3. See license.txt | ||
|
||
import frappe | ||
from frappe import _ | ||
|
||
from erpnext.erpnext_integrations.doctype.gocardless_settings.gocardless_settings import ( | ||
get_gateway_controller, | ||
gocardless_initialization, | ||
) | ||
|
||
no_cache = 1 | ||
|
||
expected_keys = ("redirect_flow_id", "reference_doctype", "reference_docname") | ||
|
||
|
||
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 | ||
|
||
|
||
@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} | ||
) | ||
|
||
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 | ||
) | ||
|
||
data = { | ||
"mandate": redirect_flow.links.mandate, | ||
"customer": redirect_flow.links.customer, | ||
"redirect_to": confirmation_url, | ||
"redirect_message": "Mandate successfully created", | ||
"reference_doctype": reference_doctype, | ||
"reference_docname": reference_docname, | ||
} | ||
|
||
try: | ||
create_mandate(data) | ||
except Exception as e: | ||
frappe.log_error("GoCardless Mandate Registration Error") | ||
|
||
gateway_controller = get_gateway_controller(reference_docname) | ||
frappe.get_doc("GoCardless Settings", gateway_controller).create_payment_request(data) | ||
|
||
return {"redirect_to": confirmation_url} | ||
|
||
except Exception as e: | ||
frappe.log_error("GoCardless Payment Error") | ||
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 | ||
) | ||
|
||
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: | ||
frappe.log_error("Gocardless: Unable to create mandate") |