From e49643feb5b6fcfdef848f01b958392d23d53cd6 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Tue, 2 Oct 2018 22:11:43 +0530 Subject: [PATCH 1/6] provision to handel paypal and razoapay subscription charge notifications --- .../paypal_settings/paypal_settings.py | 20 ++++++++++++++ .../razorpay_settings/razorpay_settings.py | 26 ++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py index c59a240..80aadfd 100644 --- a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py +++ b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py @@ -363,7 +363,27 @@ def manage_recurring_payment_profile_status(profile_id, action, args, url): }) response = make_post_request(url, data=args) + if response.get("ACK")[0] != "Success": frappe.throw(_("Failed while amending subscription")) +@frappe.whitelist(allow_guest=True) +def ipn_handler(): + data = frappe.local.form_dict + data.update({ + "payment_gateway": "PayPal" + }) + + doc = frappe.get_doc({ + "data": frappe.local.form_dict, + "doctype": "Integration Request", + "status": "Subscription Notification" + }).insert(ignore_permissions=True) + frappe.db.commit() + + frappe.enqueue(method='frappe.integrations.doctype.paypal_settings.paypal_settings.handle_subscription_notification', + queue='long', timeout=600, is_async=True, **{"doctype": "Integration Request", "docname": doc.name}) + +def handle_subscription_notification(doctype, docname): + call_hook_method("handle_subscription_notification", doctype=doctype, docname=docname) diff --git a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py index 064d509..a1029aa 100644 --- a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py +++ b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py @@ -119,7 +119,6 @@ class RazorpaySettings(Document): "content-type": "application/json" } ) - if not resp.get('id'): frappe.log_error(str(resp), 'Razorpay Failed while creating subscription') except: @@ -329,5 +328,26 @@ def capture_payment(is_sandbox=False, sanbox_response=None): def convert_rupee_to_paisa(**kwargs): for addon in kwargs.get('addons'): addon['item']['amount'] *= 100 - - frappe.conf.converted_rupee_to_paisa = True \ No newline at end of file + + frappe.conf.converted_rupee_to_paisa = True + + +@frappe.whitelist(allow_guest=True) +def razorpay_subscription_callback(): + data = frappe.local.form_dict + data.update({ + "payment_gateway": "Razorpay" + }) + + doc = frappe.get_doc({ + "data": json.dumps(frappe.local.form_dict), + "doctype": "Integration Request", + "status": "Subscription Notification" + }).insert(ignore_permissions=True) + frappe.db.commit() + + frappe.enqueue(method='frappe.integrations.doctype.razorpay_settings.razorpay_settings.handle_subscription_notification', + queue='long', timeout=600, is_async=True, **{"doctype": "Integration Request", "docname": doc.name}) + +def handle_subscription_notification(doctype, docname): + call_hook_method("handle_subscription_notification", doctype=doctype, docname=docname) \ No newline at end of file From 0e2cf29f1636568c98fa4c66f041e2658286571e Mon Sep 17 00:00:00 2001 From: Saurabh Date: Fri, 5 Oct 2018 17:16:28 +0530 Subject: [PATCH 2/6] [fix] add validations on payment notification callback --- .../paypal_settings/paypal_settings.py | 55 +++++++++++++----- .../razorpay_settings/razorpay_settings.py | 57 ++++++++++++++----- 2 files changed, 84 insertions(+), 28 deletions(-) diff --git a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py index 80aadfd..9fa3164 100644 --- a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py +++ b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py @@ -68,10 +68,10 @@ import frappe import json from frappe import _ from datetime import datetime -from frappe.utils import get_url, call_hook_method, cint, get_timestamp, cstr, now, date_diff, get_datetime from six.moves.urllib.parse import urlencode from frappe.model.document import Document from frappe.integrations.utils import create_request_log, make_post_request, create_payment_gateway +from frappe.utils import get_url, call_hook_method, cint, get_timestamp, cstr, now, date_diff, get_datetime api_path = '/api/method/frappe.integrations.doctype.paypal_settings.paypal_settings' @@ -363,27 +363,56 @@ def manage_recurring_payment_profile_status(profile_id, action, args, url): }) response = make_post_request(url, data=args) - if response.get("ACK")[0] != "Success": frappe.throw(_("Failed while amending subscription")) @frappe.whitelist(allow_guest=True) def ipn_handler(): - data = frappe.local.form_dict - data.update({ - "payment_gateway": "PayPal" + try: + data = frappe.local.form_dict + + validate_ipn_request(data) + + data.update({ + "payment_gateway": "PayPal" + }) + + doc = frappe.get_doc({ + "data": json.dumps(frappe.local.form_dict), + "doctype": "Integration Request", + "status": "Subscription Notification" + }).insert(ignore_permissions=True) + frappe.db.commit() + + frappe.enqueue(method='frappe.integrations.doctype.paypal_settings.paypal_settings.handle_subscription_notification', + queue='long', timeout=600, is_async=True, **{"doctype": "Integration Request", "docname": doc.name}) + + except frappe.InvalidStatusError: + pass + except Exception as e: + frappe.log(frappe.log_error(title=e)) + +def validate_ipn_request(data): + def _throw(): + frappe.throw(_("In Valid Request"), exc=frappe.InvalidStatusError) + + if not data.get("recurring_payment_id"): + _throw() + + doc = frappe.get_doc("PayPal Settings") + params, url = doc.get_paypal_params_and_url() + + params.update({ + "METHOD": "GetRecurringPaymentsProfileDetails", + "PROFILEID": data.get("recurring_payment_id") }) - doc = frappe.get_doc({ - "data": frappe.local.form_dict, - "doctype": "Integration Request", - "status": "Subscription Notification" - }).insert(ignore_permissions=True) - frappe.db.commit() + params = urlencode(params) + res = make_post_request(url=url, data=params.encode("utf-8")) - frappe.enqueue(method='frappe.integrations.doctype.paypal_settings.paypal_settings.handle_subscription_notification', - queue='long', timeout=600, is_async=True, **{"doctype": "Integration Request", "docname": doc.name}) + if res['ACK'][0] != 'Success': + _throw() def handle_subscription_notification(doctype, docname): call_hook_method("handle_subscription_notification", doctype=doctype, docname=docname) diff --git a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py index a1029aa..0eb41e5 100644 --- a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py +++ b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py @@ -331,23 +331,50 @@ def convert_rupee_to_paisa(**kwargs): frappe.conf.converted_rupee_to_paisa = True - @frappe.whitelist(allow_guest=True) def razorpay_subscription_callback(): - data = frappe.local.form_dict - data.update({ - "payment_gateway": "Razorpay" - }) - - doc = frappe.get_doc({ - "data": json.dumps(frappe.local.form_dict), - "doctype": "Integration Request", - "status": "Subscription Notification" - }).insert(ignore_permissions=True) - frappe.db.commit() - - frappe.enqueue(method='frappe.integrations.doctype.razorpay_settings.razorpay_settings.handle_subscription_notification', - queue='long', timeout=600, is_async=True, **{"doctype": "Integration Request", "docname": doc.name}) + try: + data = frappe.local.form_dict + + validate_payment_callback() + + data.update({ + "payment_gateway": "Razorpay" + }) + + doc = frappe.get_doc({ + "data": json.dumps(frappe.local.form_dict), + "doctype": "Integration Request", + "status": "Subscription Notification" + }).insert(ignore_permissions=True) + frappe.db.commit() + + frappe.enqueue(method='frappe.integrations.doctype.razorpay_settings.razorpay_settings.handle_subscription_notification', + queue='long', timeout=600, is_async=True, **{"doctype": "Integration Request", "docname": doc.name}) + + except frappe.InvalidStatusError: + pass + except Exception as e: + frappe.log(frappe.log_error(title=e)) + +def validate_payment_callback(data): + def _throw(): + frappe.throw(_("Invalid Subscription"), exc=frappe.InvalidStatusError) + + subscription_id = data.get('payload').get("subscription").get("entity").get("id") + + if not(subscription_id): + _throw() + + controller = frappe.get_doc("Razorpay Settings") + + settings = controller.get_settings(data) + + resp = make_get_request("https://api.razorpay.com/v1/subscriptions/{0}".format(subscription_id), + auth=(settings.api_key, settings.api_secret)) + + if resp.get("status") != "active": + _throw() def handle_subscription_notification(doctype, docname): call_hook_method("handle_subscription_notification", doctype=doctype, docname=docname) \ No newline at end of file From 47228bdae18cd28a0b09481a24d1e3668b90d55c Mon Sep 17 00:00:00 2001 From: Saurabh Date: Mon, 8 Oct 2018 13:49:12 +0530 Subject: [PATCH 3/6] [fix] pass request type as Subscription Notification --- .../doctype/paypal_settings/paypal_settings.py | 3 ++- .../doctype/razorpay_settings/razorpay_settings.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py index 9fa3164..3d4be38 100644 --- a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py +++ b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py @@ -381,7 +381,8 @@ def ipn_handler(): doc = frappe.get_doc({ "data": json.dumps(frappe.local.form_dict), "doctype": "Integration Request", - "status": "Subscription Notification" + "integration_type": "Subscription Notification", + "status": "Queued" }).insert(ignore_permissions=True) frappe.db.commit() diff --git a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py index 0eb41e5..3cca99e 100644 --- a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py +++ b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py @@ -345,7 +345,8 @@ def razorpay_subscription_callback(): doc = frappe.get_doc({ "data": json.dumps(frappe.local.form_dict), "doctype": "Integration Request", - "status": "Subscription Notification" + "integration_type": "Subscription Notification", + "status": "Queued" }).insert(ignore_permissions=True) frappe.db.commit() From 69fb2e7768857be6fb2c71cd45622e116ce30128 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Mon, 8 Oct 2018 18:24:07 +0530 Subject: [PATCH 4/6] [fix] pass form dict to validate function --- .../doctype/razorpay_settings/razorpay_settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py index 3cca99e..1848e4d 100644 --- a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py +++ b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py @@ -336,7 +336,7 @@ def razorpay_subscription_callback(): try: data = frappe.local.form_dict - validate_payment_callback() + validate_payment_callback(data) data.update({ "payment_gateway": "Razorpay" From dba5a1e540fc32cd21a5acba1feae4d31f559b31 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Wed, 10 Oct 2018 17:28:19 +0530 Subject: [PATCH 5/6] [fix] do not cache payment success page --- payments/templates/pages/payment_success.py | 1 + 1 file changed, 1 insertion(+) diff --git a/payments/templates/pages/payment_success.py b/payments/templates/pages/payment_success.py index 655767e..4ece359 100644 --- a/payments/templates/pages/payment_success.py +++ b/payments/templates/pages/payment_success.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals import frappe +no_cache = True def get_context(context): token = frappe.local.form_dict.token From 8e138648a505d8509f2cffed3d6be934c93b8cf7 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Wed, 17 Oct 2018 19:50:54 +0530 Subject: [PATCH 6/6] [fix] razorpay subscription cancellation --- .../doctype/razorpay_settings/razorpay_settings.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py index 1848e4d..bca9291 100644 --- a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py +++ b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py @@ -282,13 +282,12 @@ class RazorpaySettings(Document): }) return settings - - + def cancel_subscription(self, subscription_id): - settings = self.get_settings() - + settings = self.get_settings({}) + try: - resp = make_get_request("https://api.razorpay.com/v1/subscriptions/{0}/cancel" + resp = make_post_request("https://api.razorpay.com/v1/subscriptions/{0}/cancel" .format(subscription_id), auth=(settings.api_key, settings.api_secret)) except Exception: