From 19df9a9da3ace3ef451d182b93ace5ffdf4ea2a4 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Mon, 10 Sep 2018 18:50:17 +0530 Subject: [PATCH] Provision to cancel existing subscription --- .../paypal_settings/paypal_settings.py | 22 +++++++++++++++++++ .../razorpay_settings/razorpay_settings.py | 11 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py index e783fca..b56a8c6 100644 --- a/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py +++ b/payments/payment_gateways/doctype/paypal_settings/paypal_settings.py @@ -177,6 +177,26 @@ class PayPalSettings(Document): frappe.throw(_("Looks like something is wrong with this site's Paypal configuration.")) return response + + def cancel_recurring_profile(self, **kwargs): + params, url = self.get_paypal_params_and_url() + + if not kwargs.get('profile_id'): + frappe.throw(_("PayPal Recurring Profile ID is required")) + + params.update({ + "METHOD": "ManageRecurringPaymentsProfileStatus", + "PROFILEID": kwargs['profile_id'], + "ACTION": "Cancel" + }) + + params = urlencode(params) + + response = make_post_request(url, data=params.encode("utf-8")) + if response.get("ACK")[0] != "Success": + frappe.throw(_("Looks like something is wrong with this site's Paypal configuration.")) + + return response @frappe.whitelist(allow_guest=True, xss_safe=True) def get_express_checkout_details(token): @@ -316,6 +336,8 @@ def create_recurring_profile(token, payerid): }, "Completed") if data.get("reference_doctype") and data.get("reference_docname"): + data['subscription_details']['subscription_id'] = response.get("PROFILEID")[0] + frappe.flags.data = data custom_redirect_to = frappe.get_doc(data.get("reference_doctype"), data.get("reference_docname")).run_method("on_payment_authorized", "Completed") diff --git a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py index 3ad9559..920da52 100644 --- a/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py +++ b/payments/payment_gateways/doctype/razorpay_settings/razorpay_settings.py @@ -258,6 +258,17 @@ class RazorpaySettings(Document): }) return settings + + + def cancel_subscription(self, subscription_id): + settings = self.get_settings() + + try: + resp = make_get_request("https://api.razorpay.com/v1/subscriptions/{0}/cancel" + .format(subscription_id), auth=(settings.api_key, + settings.api_secret)) + except Exception: + frappe.log_error(frappe.get_traceback()) def capture_payment(is_sandbox=False, sanbox_response=None): """