Skip to content

Commit

Permalink
Merge pull request #44 from s-aga-r/GET-PAYMENTS-GATEWAYS
Browse files Browse the repository at this point in the history
refactor: get payment gateways from ERPNext
  • Loading branch information
s-aga-r authored Oct 14, 2023
2 parents 5ea14b2 + f9cd282 commit 3fc3f68
Show file tree
Hide file tree
Showing 31 changed files with 2,087 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ jobs:
- name: Install
working-directory: /home/runner/frappe-bench
run: |
bench get-app https://github.com/frappe/erpnext --branch "develop" --resolve-deps
bench get-app payments $GITHUB_WORKSPACE
bench setup requirements --dev
bench new-site --db-root-password root --admin-password admin test_site
bench start &> bench_start.log &
bench --site test_site install-app payments
bench --site test_site install-app erpnext payments
bench build
env:
CI: 'Yes'
Expand Down
2 changes: 1 addition & 1 deletion payments/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
# Testing
# -------

# before_tests = "pay.install.before_tests"
before_tests = "erpnext.setup.utils.before_tests" # To setup company and accounts

# Overriding Methods
# ------------------------------
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2018, Frappe Technologies and contributors
// For license information, please see license.txt

frappe.ui.form.on('GoCardless Mandate', {
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"actions": [],
"autoname": "field:mandate",
"creation": "2018-02-08 11:33:15.721919",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"disabled",
"mandate",
"gocardless_customer"
],
"fields": [
{
"default": "0",
"fieldname": "disabled",
"fieldtype": "Check",
"label": "Disabled"
},
{
"fieldname": "mandate",
"fieldtype": "Data",
"label": "Mandate",
"read_only": 1,
"reqd": 1,
"unique": 1
},
{
"fieldname": "gocardless_customer",
"fieldtype": "Data",
"in_list_view": 1,
"label": "GoCardless Customer",
"read_only": 1,
"reqd": 1
}
],
"links": [],
"modified": "2023-09-27 10:33:44.453462",
"modified_by": "Administrator",
"module": "Payment Gateways",
"name": "GoCardless Mandate",
"naming_rule": "By fieldname",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2018, Frappe Technologies and contributors
# For license information, please see license.txt


from frappe.model.document import Document


class GoCardlessMandate(Document):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2018, Frappe Technologies and Contributors
# See license.txt

import unittest


class TestGoCardlessMandate(unittest.TestCase):
pass
89 changes: 89 additions & 0 deletions payments/payment_gateways/doctype/gocardless_settings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright (c) 2018, Frappe Technologies and contributors
# For license information, please see license.txt


import hashlib
import hmac
import json

import frappe


@frappe.whitelist(allow_guest=True)
def webhooks():
r = frappe.request
if not r:
return

if not authenticate_signature(r):
raise frappe.AuthenticationError

gocardless_events = json.loads(r.get_data()) or []
for event in gocardless_events["events"]:
set_status(event)

return 200


def set_status(event):
resource_type = event.get("resource_type", {})

if resource_type == "mandates":
set_mandate_status(event)


def set_mandate_status(event):
mandates = []
if isinstance(event["links"], (list,)):
for link in event["links"]:
mandates.append(link["mandate"])
else:
mandates.append(event["links"]["mandate"])

if (
event["action"] == "pending_customer_approval"
or event["action"] == "pending_submission"
or event["action"] == "submitted"
or event["action"] == "active"
):
disabled = 0
else:
disabled = 1

for mandate in mandates:
frappe.db.set_value("GoCardless Mandate", mandate, "disabled", disabled)


def authenticate_signature(r):
"""Returns True if the received signature matches the generated signature"""
received_signature = frappe.get_request_header("Webhook-Signature")

if not received_signature:
return False

for key in get_webhook_keys():
computed_signature = hmac.new(key.encode("utf-8"), r.get_data(), hashlib.sha256).hexdigest()
if hmac.compare_digest(str(received_signature), computed_signature):
return True

return False


def get_webhook_keys():
def _get_webhook_keys():
webhook_keys = [
d.webhooks_secret
for d in frappe.get_all(
"GoCardless Settings",
fields=["webhooks_secret"],
)
if d.webhooks_secret
]

return webhook_keys

return frappe.cache().get_value("gocardless_webhooks_secret", _get_webhook_keys)


def clear_cache():
frappe.cache().delete_value("gocardless_webhooks_secret")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2018, Frappe Technologies and contributors
// For license information, please see license.txt

// frappe.ui.form.on('GoCardless Settings', {
// refresh(frm) {

// },
// });
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"actions": [],
"autoname": "field:gateway_name",
"creation": "2018-02-06 16:11:10.028249",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"gateway_name",
"section_break_2",
"access_token",
"webhooks_secret",
"use_sandbox"
],
"fields": [
{
"fieldname": "gateway_name",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Payment Gateway Name",
"reqd": 1,
"unique": 1
},
{
"fieldname": "section_break_2",
"fieldtype": "Section Break"
},
{
"fieldname": "access_token",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Access Token",
"reqd": 1
},
{
"fieldname": "webhooks_secret",
"fieldtype": "Data",
"label": "Webhooks Secret"
},
{
"default": "0",
"fieldname": "use_sandbox",
"fieldtype": "Check",
"label": "Use Sandbox"
}
],
"links": [],
"modified": "2023-09-22 13:33:42.225243",
"modified_by": "Administrator",
"module": "Payment Gateways",
"name": "GoCardless Settings",
"naming_rule": "By fieldname",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
}
Loading

0 comments on commit 3fc3f68

Please sign in to comment.