fix for immediate subscriptions
This commit is contained in:
parent
5302af0dd1
commit
f7c5a3ce26
3 changed files with 29 additions and 16 deletions
|
|
@ -289,7 +289,6 @@ def create_recurring_profile(token, payerid):
|
|||
|
||||
addons = data.get("addons")
|
||||
subscription_details = data.get("subscription_details")
|
||||
setup_subscription_amount(data, addons)
|
||||
|
||||
if data['subscription_id'] and addons:
|
||||
manage_recurring_payment_profile_status(data['subscription_id'], 'Cancel', params, url)
|
||||
|
|
@ -337,10 +336,6 @@ def create_recurring_profile(token, payerid):
|
|||
except Exception:
|
||||
frappe.log_error(frappe.get_traceback())
|
||||
|
||||
def setup_subscription_amount(data, addons):
|
||||
for addon in addons:
|
||||
data['subscription_amount'] += addon['item']['amount']
|
||||
|
||||
def update_integration_request_status(token, data, status, error=False, doc=None):
|
||||
if not doc:
|
||||
doc = frappe.get_doc("Integration Request", token)
|
||||
|
|
|
|||
|
|
@ -67,7 +67,8 @@ import json
|
|||
from six.moves.urllib.parse import urlencode
|
||||
from frappe.model.document import Document
|
||||
from frappe.utils import get_url, call_hook_method, cint, get_timestamp
|
||||
from frappe.integrations.utils import make_get_request, make_post_request, create_request_log, create_payment_gateway
|
||||
from frappe.integrations.utils import (make_get_request, make_post_request, create_request_log,
|
||||
create_payment_gateway)
|
||||
|
||||
class RazorpaySettings(Document):
|
||||
supported_currencies = ["INR"]
|
||||
|
|
@ -103,12 +104,13 @@ class RazorpaySettings(Document):
|
|||
"quantity": 1 (The total amount is calculated as item.amount * quantity)
|
||||
}
|
||||
"""
|
||||
|
||||
url = "https://api.razorpay.com/v1/subscriptions/{0}/addons".format(kwargs.get('subscription_id'))
|
||||
|
||||
|
||||
try:
|
||||
if not frappe.conf.converted_rupee_to_paisa:
|
||||
convert_rupee_to_paisa(**kwargs)
|
||||
|
||||
for addon in kwargs.get("addons"):
|
||||
addon['item']['amount'] *= 100 #convert amount to paisa
|
||||
resp = make_post_request(
|
||||
url,
|
||||
auth=(settings.api_key, settings.api_secret),
|
||||
|
|
@ -133,19 +135,28 @@ class RazorpaySettings(Document):
|
|||
"plan_id": kwargs.get('subscription_details').get("plan_id"),
|
||||
"start_at": cint(start_date),
|
||||
"total_count": kwargs.get('subscription_details').get("billing_frequency"),
|
||||
"customer_notify": kwargs.get('subscription_details').get("customer_notify"),
|
||||
"upfront_amount": kwargs.get('subscription_details').get("upfront_amount")
|
||||
"customer_notify": kwargs.get('subscription_details').get("customer_notify")
|
||||
}
|
||||
|
||||
if kwargs.get('addons'):
|
||||
convert_rupee_to_paisa(**kwargs)
|
||||
subscription_details.update({
|
||||
"addons": kwargs.get('addons')
|
||||
})
|
||||
|
||||
try:
|
||||
resp = make_post_request(
|
||||
"https://api.razorpay.com/v1/subscriptions",
|
||||
auth=(settings.api_key, settings.api_secret),
|
||||
data=subscription_details
|
||||
data=json.dumps(subscription_details),
|
||||
headers={
|
||||
"content-type": "application/json"
|
||||
}
|
||||
)
|
||||
|
||||
if resp.get('status') == 'created':
|
||||
kwargs['subscription_id'] = resp.get('id')
|
||||
frappe.flags.status = 'created'
|
||||
return kwargs
|
||||
else:
|
||||
frappe.log_error(str(resp), 'Razorpay Failed while creating subscription')
|
||||
|
|
@ -156,11 +167,10 @@ class RazorpaySettings(Document):
|
|||
pass
|
||||
|
||||
def prepare_subscription_details(self, settings, **kwargs):
|
||||
if kwargs.get('subscription_details'):
|
||||
if not kwargs.get("subscription_id"):
|
||||
kwargs = self.setup_subscription(settings, **kwargs)
|
||||
|
||||
if kwargs.get("subscription_id") and kwargs.get("addons"):
|
||||
self.setup_addon(settings, **kwargs)
|
||||
if frappe.flags.status !='created':
|
||||
kwargs['subscription_id'] = None
|
||||
|
||||
return kwargs
|
||||
|
|
@ -235,7 +245,7 @@ class RazorpaySettings(Document):
|
|||
redirect_to = data.get('notes', {}).get('redirect_to') or None
|
||||
redirect_message = data.get('notes', {}).get('redirect_message') or None
|
||||
|
||||
if self.flags.status_changed_to == "Authorized":
|
||||
if self.flags.status_changed_to in ("Authorized", "Verified", "Completed"):
|
||||
if self.data.reference_doctype and self.data.reference_docname:
|
||||
custom_redirect_to = None
|
||||
try:
|
||||
|
|
@ -318,3 +328,9 @@ def capture_payment(is_sandbox=False, sanbox_response=None):
|
|||
doc.status = "Failed"
|
||||
doc.error = frappe.get_traceback()
|
||||
frappe.log_error(doc.error, '{0} Failed'.format(doc.name))
|
||||
|
||||
def convert_rupee_to_paisa(**kwargs):
|
||||
for addon in kwargs.get('addons'):
|
||||
addon['item']['amount'] *= 100
|
||||
|
||||
frappe.conf.converted_rupee_to_paisa = True
|
||||
|
|
@ -39,6 +39,7 @@ def make_post_request(url, auth=None, headers=None, data=None):
|
|||
try:
|
||||
s = get_request_session()
|
||||
frappe.flags.integration_request = s.post(url, data=data, auth=auth, headers=headers)
|
||||
print(frappe.flags.integration_request.text)
|
||||
frappe.flags.integration_request.raise_for_status()
|
||||
|
||||
if frappe.flags.integration_request.headers.get("content-type") == "text/plain; charset=utf-8":
|
||||
|
|
@ -46,6 +47,7 @@ def make_post_request(url, auth=None, headers=None, data=None):
|
|||
|
||||
return frappe.flags.integration_request.json()
|
||||
except Exception as exc:
|
||||
print(exc)
|
||||
frappe.log_error()
|
||||
raise exc
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue