Support for razorpay subscription and addons
This commit is contained in:
parent
10534276e5
commit
2749f0ebbf
4 changed files with 78 additions and 3 deletions
|
|
@ -82,6 +82,59 @@ class RazorpaySettings(Document):
|
|||
if currency not in self.supported_currencies:
|
||||
frappe.throw(_("Please select another payment method. Razorpay does not support transactions in currency '{0}'").format(currency))
|
||||
|
||||
def before_get_payment_url(self, **kwargs):
|
||||
if not kwargs.get('subscription_details') and not kwargs.get('subscription_id'):
|
||||
return
|
||||
|
||||
settings = self.get_settings(kwargs)
|
||||
if kwargs.get('subscription_id') and kwargs.get('addons'):
|
||||
return self.setup_addon(settings, **kwargs)
|
||||
else:
|
||||
return self.setup_subscription(settings, **kwargs)
|
||||
|
||||
def setup_addon(self, settings, **kwargs):
|
||||
url = "https://api.razorpay.com/v1/subscriptions/{0}/addons".format(kwargs.get('subscription_id'))
|
||||
|
||||
for addon in kwargs.get("addons"):
|
||||
try:
|
||||
resp = make_post_request(
|
||||
url,
|
||||
auth=(settings.api_key, settings.api_secret),
|
||||
data=json.dumps(addon),
|
||||
headers={
|
||||
"content-type": "application/json"
|
||||
}
|
||||
)
|
||||
|
||||
if not resp.get('id'):
|
||||
frappe.log_error(str(resp), 'Razorpay Failed while creating subscription')
|
||||
|
||||
except:
|
||||
frappe.log_error(frappe.get_traceback())
|
||||
# failed
|
||||
pass
|
||||
|
||||
return {}
|
||||
|
||||
def setup_subscription(self, settings, **kwargs):
|
||||
try:
|
||||
resp = make_post_request(
|
||||
"https://api.razorpay.com/v1/subscriptions",
|
||||
auth=(settings.api_key, settings.api_secret),
|
||||
data=kwargs.get('subscription_details')
|
||||
)
|
||||
|
||||
if resp.get('status') == 'created':
|
||||
kwargs['subscription_details']['subscription_id'] = resp.get('id')
|
||||
else:
|
||||
frappe.log_error(str(resp), 'Razorpay Failed while creating subscription')
|
||||
|
||||
return kwargs
|
||||
except:
|
||||
frappe.log_error(frappe.get_traceback())
|
||||
# failed
|
||||
pass
|
||||
|
||||
def get_payment_url(self, **kwargs):
|
||||
integration_request = create_request_log(kwargs, "Host", "Razorpay")
|
||||
return get_url("./integrations/razorpay_checkout?token={0}".format(integration_request.name))
|
||||
|
|
@ -119,6 +172,15 @@ class RazorpaySettings(Document):
|
|||
self.integration_request.update_status(data, 'Authorized')
|
||||
self.flags.status_changed_to = "Authorized"
|
||||
|
||||
elif data.get('subscription_details') and data.get('subscription_details').get('subscription_id'):
|
||||
if resp.get("status") == "refunded":
|
||||
self.integration_request.update_status(data, 'Refunded')
|
||||
self.flags.status_changed_to = "Authorized"
|
||||
|
||||
if resp.get("status") == "captured":
|
||||
self.integration_request.update_status(data, 'Completed')
|
||||
self.flags.status_changed_to = "Authorized"
|
||||
|
||||
else:
|
||||
frappe.log_error(str(resp), 'Razorpay Payment not authorized')
|
||||
|
||||
|
|
@ -136,8 +198,10 @@ class RazorpaySettings(Document):
|
|||
if self.data.reference_doctype and self.data.reference_docname:
|
||||
custom_redirect_to = None
|
||||
try:
|
||||
frappe.flags.data = data
|
||||
custom_redirect_to = frappe.get_doc(self.data.reference_doctype,
|
||||
self.data.reference_docname).run_method("on_payment_authorized", self.flags.status_changed_to)
|
||||
|
||||
except Exception:
|
||||
frappe.log_error(frappe.get_traceback())
|
||||
|
||||
|
|
@ -164,7 +228,7 @@ class RazorpaySettings(Document):
|
|||
"api_secret": self.get_password(fieldname="api_secret", raise_exception=False)
|
||||
})
|
||||
|
||||
if cint(data.get('notes', {}).get('use_sandbox')):
|
||||
if cint(data.get('notes', {}).get('use_sandbox')) or data.get("use_sandbox"):
|
||||
settings.update({
|
||||
"api_key": frappe.conf.sandbox_api_key,
|
||||
"api_secret": frappe.conf.sandbox_api_secret,
|
||||
|
|
@ -201,4 +265,4 @@ def capture_payment(is_sandbox=False, sanbox_response=None):
|
|||
doc = frappe.get_doc("Integration Request", doc.name)
|
||||
doc.status = "Failed"
|
||||
doc.error = frappe.get_traceback()
|
||||
frappe.log_error(doc.error, '{0} Failed'.format(doc.name))
|
||||
frappe.log_error(doc.error, '{0} Failed'.format(doc.name))
|
||||
|
|
|
|||
|
|
@ -90,6 +90,10 @@ def get_checkout_url(**kwargs):
|
|||
try:
|
||||
if kwargs.get('payment_gateway'):
|
||||
doc = frappe.get_doc("{0} Settings".format(kwargs.get('payment_gateway')))
|
||||
resp = doc.run_method("before_get_payment_url", **kwargs)
|
||||
|
||||
if isinstance(resp, dict):
|
||||
kwargs.update(resp)
|
||||
return doc.get_payment_url(**kwargs)
|
||||
else:
|
||||
raise Exception
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ $(document).ready(function(){
|
|||
"amount": cint({{ amount }} * 100), // 2000 paise = INR 20
|
||||
"name": "{{ title }}",
|
||||
"description": "{{ description }}",
|
||||
"subscription_id": "{{ subscription_id }}",
|
||||
"handler": function (response){
|
||||
razorpay.make_payment_log(response, options, "{{ reference_doctype }}", "{{ reference_docname }}", "{{ token }}");
|
||||
},
|
||||
|
|
|
|||
|
|
@ -27,7 +27,13 @@ def get_context(context):
|
|||
context['token'] = frappe.form_dict['token']
|
||||
context['amount'] = flt(context['amount'])
|
||||
|
||||
except Exception:
|
||||
if payment_details.get('subscription_details'):
|
||||
context['subscription_id'] = payment_details.get("subscription_details", {}).get('subscription_id', '')
|
||||
else:
|
||||
context['subscription_id'] = ''
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
frappe.redirect_to_message(_('Invalid Token'),
|
||||
_('Seems token you are using is invalid!'),
|
||||
http_status_code=400, indicator_color='red')
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue