From e49643feb5b6fcfdef848f01b958392d23d53cd6 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Tue, 2 Oct 2018 22:11:43 +0530 Subject: [PATCH] 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