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
                }))
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}))
Beispiel #3
0
def make_payment(razorpay_payment_id, options, reference_doctype,
                 reference_docname):
    try:
        razorpay_payment = frappe.get_doc({
            "doctype":
            "Razorpay Payment",
            "razorpay_payment_id":
            razorpay_payment_id,
            "data":
            options,
            "reference_doctype":
            reference_doctype,
            "reference_docname":
            reference_docname
        })

        razorpay_payment.insert(ignore_permissions=True)

        if frappe.db.get_value("Razorpay Payment", razorpay_payment.name,
                               "status") == "Authorized":
            return {
                "redirect_to": razorpay_payment.flags.redirect_to
                or "razorpay-payment-success",
                "status": 200
            }

    except AuthenticationError, e:
        make_log_entry(e.message, options)
        return {
            "redirect_to":
            frappe.redirect_to_message(
                _('Server Error'),
                _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account."
                  )),
            "status":
            401
        }
def make_payment(razorpay_payment_id, options, reference_doctype, reference_docname):
	try:
		razorpay_payment = frappe.get_doc({
			"doctype": "Razorpay Payment",
			"razorpay_payment_id": razorpay_payment_id,
			"data": options,
			"reference_doctype": reference_doctype,
			"reference_docname": reference_docname
		})

		razorpay_payment.insert(ignore_permissions=True)

		if frappe.db.get_value("Razorpay Payment", razorpay_payment.name, "status") == "Authorized":
			return {
				"redirect_to": razorpay_payment.flags.redirect_to or "razorpay-payment-success",
				"status": 200
			}

	except AuthenticationError, e:
		make_log_entry(e.message, options)
		return{
			"redirect_to": frappe.redirect_to_message(_('Server Error'), _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account.")),
			"status": 401
		}
		})

		razorpay_payment.insert(ignore_permissions=True)

		if frappe.db.get_value("Razorpay Payment", razorpay_payment.name, "status") == "Authorized":
			return {
				"redirect_to": razorpay_payment.flags.redirect_to or "razorpay-payment-success",
				"status": 200
			}

	except AuthenticationError, e:
		make_log_entry(e.message, options)
		return{
			"redirect_to": frappe.redirect_to_message(_('Server Error'), _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account.")),
			"status": 401
		}

	except InvalidRequest, e:
		make_log_entry(e.message, options)
		return {
			"redirect_to": frappe.redirect_to_message(_('Server Error'), _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account.")),
			"status": 400
		}

	except GatewayError, e:
		make_log_entry(e.message, options)
		return {
			"redirect_to": frappe.redirect_to_message(_('Server Error'), _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account.")),
			"status": 500
		}
        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
                }))

        except GatewayError, 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
                }))


def capture_missing_payments():
    settings = get_razorpay_settings()

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

    for payment in resp.get("items"):
		})

		razorpay_payment.insert(ignore_permissions=True)

		if dataent.db.get_value("Razorpay Payment", razorpay_payment.name, "status") == "Authorized":
			return {
				"redirect_to": razorpay_payment.flags.redirect_to or "razorpay-payment-success",
				"status": 200
			}

	except AuthenticationError, e:
		make_log_entry(e.message, options)
		return{
			"redirect_to": dataent.redirect_to_message(_('Server Error'), _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account.")),
			"status": 401
		}

	except InvalidRequest, e:
		make_log_entry(e.message, options)
		return {
			"redirect_to": dataent.redirect_to_message(_('Server Error'), _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account.")),
			"status": 400
		}

	except GatewayError, e:
		make_log_entry(e.message, options)
		return {
			"redirect_to": dataent.redirect_to_message(_('Server Error'), _("Seems issue with server's razorpay config. Don't worry, in case of failure amount will get refunded to your account.")),
			"status": 500
		}
					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}))

		except GatewayError, 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}))

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"],