Esempio n. 1
0
def capture_missing_payments():
    settings = get_razorpay_settings()

    resp = get_request("https://api.razorpay.com/v1/payments",
                       auth=frappe._dict({
                           "api_key": settings.api_key,
                           "api_secret": settings.api_secret
                       }))

    for payment in resp.get("items"):
        if payment.get("status") == "authorized" and not frappe.db.exists(
                "Razorpay Payment", payment.get("id")):
            razorpay_payment = frappe.get_doc({
                "doctype":
                "Razorpay Payment",
                "razorpay_payment_id":
                payment.get("id"),
                "data": {
                    "amount": payment["amount"],
                    "description": payment["description"],
                    "email": payment["email"],
                    "contact": payment["contact"],
                    "payment_request": payment["notes"]["payment_request"],
                    "reference_doctype": payment["notes"]["reference_doctype"],
                    "reference_docname": payment["notes"]["reference_docname"]
                },
                "status":
                "Authorized",
                "reference_doctype":
                "Payment Request",
                "reference_docname":
                payment["notes"]["payment_request"]
            })

            razorpay_payment.insert(ignore_permissions=True)
def capture_missing_payments():
	settings = get_razorpay_settings()

	resp = get_request("https://api.razorpay.com/v1/payments",
		auth=frappe._dict({"api_key": settings.api_key, "api_secret": settings.api_secret}))

	for payment in resp.get("items"):
		if payment.get("status") == "authorized" and not frappe.db.exists("Razorpay Payment", payment.get("id")):
			razorpay_payment = frappe.get_doc({
				"doctype": "Razorpay Payment",
				"razorpay_payment_id": payment.get("id"),
				"data": {
					"amount": payment["amount"],
					"description": payment["description"],
					"email": payment["email"],
					"contact": payment["contact"],
					"payment_request": payment["notes"]["payment_request"],
					"reference_doctype": payment["notes"]["reference_doctype"],
					"reference_docname": payment["notes"]["reference_docname"]
				},
				"status": "Authorized",
				"reference_doctype": "Payment Request",
				"reference_docname": payment["notes"]["payment_request"]
			})

			razorpay_payment.insert(ignore_permissions=True)
def authorise_payment():
	settings = get_razorpay_settings()
	for doc in frappe.get_all("Razorpay Payment", filters={"status": "Created"},
		fields=["name", "data", "reference_doctype", "reference_docname"]):

		confirm_payment(doc, settings.api_key, settings.api_secret)
		set_redirect(doc)
def capture_payment(razorpay_payment_id=None,
                    is_sandbox=False,
                    sanbox_response=None):
    """
		Verifies the purchase as complete by the merchant.
		After capture, the amount is transferred to the merchant within T+3 days
		where T is the day on which payment is captured.

		Note: Attempting to capture a payment whose status is not authorized will produce an error.
	"""
    settings = get_razorpay_settings()

    filters = {"status": "Authorized"}

    if is_sandbox:
        filters.update({"razorpay_payment_id": razorpay_payment_id})

    for doc in dataent.get_all("Razorpay Payment",
                               filters=filters,
                               fields=["name", "data"]):

        try:
            if is_sandbox and sanbox_response:
                resp = sanbox_response

            else:
                resp = post_request(
                    "https://api.razorpay.com/v1/payments/{0}/capture".format(
                        doc.name),
                    data={"amount": json.loads(doc.data).get("amount")},
                    auth=dataent._dict({
                        "api_key": settings.api_key,
                        "api_secret": settings.api_secret
                    }))

            if resp.get("status") == "captured":
                dataent.db.set_value("Razorpay Payment", doc.name, "status",
                                     "Captured")

        except AuthenticationError, e:
            make_log_entry(
                e.message,
                json.dumps({
                    "api_key": settings.api_key,
                    "api_secret": settings.api_secret,
                    "doc_name": doc.name,
                    "status": doc.status
                }))

        except InvalidRequest, e:
            make_log_entry(
                e.message,
                json.dumps({
                    "api_key": settings.api_key,
                    "api_secret": settings.api_secret,
                    "doc_name": doc.name,
                    "status": doc.status
                }))
Esempio n. 5
0
def authorise_payment():
    settings = get_razorpay_settings()
    for doc in frappe.get_all(
            "Razorpay Payment",
            filters={"status": "Created"},
            fields=["name", "data", "reference_doctype", "reference_docname"]):

        confirm_payment(doc, settings.api_key, settings.api_secret)
        set_redirect(doc)
def validate_razorpay_credentials(doc=None, method=None, razorpay_settings=None):
	if not razorpay_settings:
		razorpay_settings = get_razorpay_settings()

	try:
		get_request(url="https://api.razorpay.com/v1/payments", auth=frappe._dict({
			"api_key": razorpay_settings.api_key,
			"api_secret": razorpay_settings.api_secret
		}))
	except AuthenticationError, e:
		frappe.throw(_(e.message))
Esempio n. 7
0
def get_context(context):
    context.no_cache = 1
    context.api_key = get_razorpay_settings().api_key

    context.brand_image = (frappe.db.get_value("Razorpay Settings", None,
                                               "brand_image")
                           or './assets/erpnext/images/erp-icon.svg')

    if frappe.form_dict.payment_request:
        payment_req = frappe.get_doc('Payment Request',
                                     frappe.form_dict.payment_request)
        validate_transaction_currency(payment_req.currency)

        if payment_req.status == "Paid":
            frappe.redirect_to_message(
                _('Already Paid'), _('You have already paid for this order'))
            return

        reference_doc = frappe.get_doc(payment_req.reference_doctype,
                                       payment_req.reference_name)

        context.amount = payment_req.grand_total
        context.title = reference_doc.company
        context.description = payment_req.subject
        context.doctype = payment_req.doctype
        context.name = payment_req.name
        context.payer_name = reference_doc.customer_name
        context.payer_email = reference_doc.get(
            'email_to') or frappe.session.user
        context.order_id = payment_req.name
        context.reference_doctype = payment_req.reference_doctype
        context.reference_name = payment_req.reference_name

    # all these keys exist in form_dict
    elif not (set(expected_keys) - set(frappe.form_dict.keys())):
        for key in expected_keys:
            context[key] = frappe.form_dict[key]

        context['amount'] = flt(context['amount'])
        context['reference_doctype'] = context['reference_name'] = None

    else:
        frappe.redirect_to_message(
            _('Some information is missing'),
            _('Looks like someone sent you to an incomplete URL. Please ask them to look into it.'
              ))
        frappe.local.flags.redirect_location = frappe.local.response.location
        raise frappe.Redirect
def validate_razorpay_credentials(doc=None,
                                  method=None,
                                  razorpay_settings=None):
    if not razorpay_settings:
        razorpay_settings = get_razorpay_settings()

    try:
        get_request(url="https://api.razorpay.com/v1/payments",
                    auth=dataent._dict({
                        "api_key":
                        razorpay_settings.api_key,
                        "api_secret":
                        razorpay_settings.api_secret
                    }))
    except AuthenticationError, e:
        dataent.throw(_(e.message))
def get_context(context):
	context.no_cache = 1
	context.api_key = get_razorpay_settings().api_key

	context.brand_image = (frappe.db.get_value("Razorpay Settings", None, "brand_image")
		or './assets/erpnext/images/erp-icon.svg')

	if frappe.form_dict.payment_request:
		payment_req = frappe.get_doc('Payment Request', frappe.form_dict.payment_request)
		validate_transaction_currency(payment_req.currency)

		if payment_req.status == "Paid":
			frappe.redirect_to_message(_('Already Paid'), _('You have already paid for this order'))
			return

		reference_doc = frappe.get_doc(payment_req.reference_doctype, payment_req.reference_name)

		context.amount = payment_req.grand_total
		context.title = reference_doc.company
		context.description = payment_req.subject
		context.doctype = payment_req.doctype
		context.name = payment_req.name
		context.payer_name = reference_doc.customer_name
		context.payer_email = reference_doc.get('email_to') or frappe.session.user
		context.order_id = payment_req.name
		context.reference_doctype = payment_req.reference_doctype
		context.reference_name = payment_req.reference_name

	# all these keys exist in form_dict
	elif not (set(expected_keys) - set(frappe.form_dict.keys())):
		for key in expected_keys:
			context[key] = frappe.form_dict[key]

		context['amount'] = flt(context['amount'])
		context['reference_doctype'] = context['reference_name'] = None

	else:
		frappe.redirect_to_message(_('Some information is missing'), _('Looks like someone sent you to an incomplete URL. Please ask them to look into it.'))
		frappe.local.flags.redirect_location = frappe.local.response.location
		raise frappe.Redirect
def capture_payment(razorpay_payment_id=None, is_sandbox=False, sanbox_response=None):
	"""
		Verifies the purchase as complete by the merchant.
		After capture, the amount is transferred to the merchant within T+3 days
		where T is the day on which payment is captured.

		Note: Attempting to capture a payment whose status is not authorized will produce an error.
	"""
	settings = get_razorpay_settings()

	filters = {"status": "Authorized"}

	if is_sandbox:
		filters.update({
			"razorpay_payment_id": razorpay_payment_id
		})

	for doc in frappe.get_all("Razorpay Payment", filters=filters,
		fields=["name", "data"]):

		try:
			if is_sandbox and sanbox_response:
				resp = sanbox_response

			else:
				resp = post_request("https://api.razorpay.com/v1/payments/{0}/capture".format(doc.name),
					data={"amount": json.loads(doc.data).get("amount")},
					auth=frappe._dict({"api_key": settings.api_key, "api_secret": settings.api_secret}))

			if resp.get("status") == "captured":
				frappe.db.set_value("Razorpay Payment", doc.name, "status", "Captured")

		except AuthenticationError, e:
			make_log_entry(e.message, json.dumps({"api_key": settings.api_key, "api_secret": settings.api_secret,
				"doc_name": doc.name, "status": doc.status}))

		except InvalidRequest, e:
			make_log_entry(e.message, json.dumps({"api_key": settings.api_key, "api_secret": settings.api_secret,
				"doc_name": doc.name, "status": doc.status}))
Esempio n. 11
0
 def on_update(self):
     settings = get_razorpay_settings()
     if self.status != "Authorized":
         confirm_payment(self, settings.api_key, settings.api_secret,
                         self.flags.is_sandbox)
     set_redirect(self)
	def on_update(self):
		settings = get_razorpay_settings()
		if self.status != "Authorized":
			confirm_payment(self, settings.api_key, settings.api_secret, self.flags.is_sandbox)
		set_redirect(self)