forked from frappe/payments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
210 lines (186 loc) · 5.69 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from contextlib import contextmanager
import click
import frappe
from frappe import _
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
def validate_integration_request(docname: str | None):
if frappe.db.get_value("Integration Request", docname, "status") == "Cancelled":
frappe.throw(_("Expired Token"))
def get_payment_gateway_controller(payment_gateway):
"""Return payment gateway controller"""
gateway = frappe.get_doc("Payment Gateway", payment_gateway)
if gateway.gateway_controller is None:
try:
return frappe.get_doc(f"{payment_gateway} Settings")
except Exception:
frappe.throw(_("{0} Settings not found").format(payment_gateway))
else:
try:
return frappe.get_doc(gateway.gateway_settings, gateway.gateway_controller)
except Exception:
frappe.throw(_("{0} Settings not found").format(payment_gateway))
@frappe.whitelist(allow_guest=True, xss_safe=True)
def get_checkout_url(**kwargs):
try:
if kwargs.get("payment_gateway"):
doc = frappe.get_doc("{} Settings".format(kwargs.get("payment_gateway")))
return doc.get_payment_url(**kwargs)
else:
raise Exception
except Exception:
frappe.respond_as_web_page(
_("Something went wrong"),
_(
"Looks like something is wrong with this site's payment gateway configuration. No payment has been made."
),
indicator_color="red",
http_status_code=frappe.ValidationError.http_status_code,
)
def create_payment_gateway(gateway, settings=None, controller=None):
# NOTE: we don't translate Payment Gateway name because it is an internal doctype
if not frappe.db.exists("Payment Gateway", gateway):
payment_gateway = frappe.get_doc(
{
"doctype": "Payment Gateway",
"gateway": gateway,
"gateway_settings": settings,
"gateway_controller": controller,
}
)
payment_gateway.insert(ignore_permissions=True)
def make_custom_fields():
if not frappe.get_meta("Web Form").has_field("payments_tab"):
click.secho("* Installing Payment Custom Fields in Web Form")
create_custom_fields(
{
"Web Form": [
{
"fieldname": "payments_tab",
"fieldtype": "Tab Break",
"label": "Payments",
"insert_after": "custom_css",
},
{
"default": "0",
"fieldname": "accept_payment",
"fieldtype": "Check",
"label": "Accept Payment",
"insert_after": "payments",
},
{
"depends_on": "accept_payment",
"fieldname": "payment_gateway",
"fieldtype": "Link",
"label": "Payment Gateway",
"options": "Payment Gateway",
"insert_after": "accept_payment",
},
{
"default": "Buy Now",
"depends_on": "accept_payment",
"fieldname": "payment_button_label",
"fieldtype": "Data",
"label": "Button Label",
"insert_after": "payment_gateway",
},
{
"depends_on": "accept_payment",
"fieldname": "payment_button_help",
"fieldtype": "Text",
"label": "Button Help",
"insert_after": "payment_button_label",
},
{
"fieldname": "payments_cb",
"fieldtype": "Column Break",
"insert_after": "payment_button_help",
},
{
"default": "0",
"depends_on": "accept_payment",
"fieldname": "amount_based_on_field",
"fieldtype": "Check",
"label": "Amount Based On Field",
"insert_after": "payments_cb",
},
{
"depends_on": "eval:doc.accept_payment && doc.amount_based_on_field",
"fieldname": "amount_field",
"fieldtype": "Select",
"label": "Amount Field",
"insert_after": "amount_based_on_field",
},
{
"depends_on": "eval:doc.accept_payment && !doc.amount_based_on_field",
"fieldname": "amount",
"fieldtype": "Currency",
"label": "Amount",
"insert_after": "amount_field",
},
{
"depends_on": "accept_payment",
"fieldname": "currency",
"fieldtype": "Link",
"label": "Currency",
"options": "Currency",
"insert_after": "amount",
},
]
}
)
frappe.clear_cache(doctype="Web Form")
if "erpnext" in frappe.get_installed_apps():
custom_fields = {
"GoCardless Mandate": [
{
"fieldname": "customer",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Customer",
"options": "Customer",
"reqd": 1,
"insert_after": "disabled",
}
]
}
create_custom_fields(custom_fields)
def delete_custom_fields():
if frappe.get_meta("Web Form").has_field("payments_tab"):
click.secho("* Uninstalling Payment Custom Fields from Web Form")
fieldnames = (
"payments_tab",
"accept_payment",
"payment_gateway",
"payment_button_label",
"payment_button_help",
"payments_cb",
"amount_field",
"amount_based_on_field",
"amount",
"currency",
)
for fieldname in fieldnames:
frappe.db.delete("Custom Field", {"name": "Web Form-" + fieldname})
frappe.clear_cache(doctype="Web Form")
def before_install():
# TODO: remove this
# This is done for erpnext CI patch test
#
# Since we follow a flow like install v14 -> restore v10 site
# -> migrate to v12, v13 and then v14 again
#
# This app fails installing when the site is restored to v10 as
# a lot of apis don;t exist in v10 and this is a (at the moment) required app for erpnext.
if not frappe.get_meta("Module Def").has_field("custom"):
return False
@contextmanager
def erpnext_app_import_guard():
marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/erpnext">Marketplace</a>'
github_link = '<a href="https://github.com/frappe/erpnext">GitHub</a>'
msg = _("erpnext app is not installed. Please install it from {} or {}").format(
marketplace_link, github_link
)
try:
yield
except ImportError:
frappe.throw(msg, title=_("Missing ERPNext App"))