2018-05-23 16:40:41 +00:00
|
|
|
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
2018-06-05 19:49:57 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
from frappe.integrations.utils import create_request_log
|
2021-08-26 16:17:00 +00:00
|
|
|
|
2022-12-08 11:10:13 +00:00
|
|
|
from erpnext.utilities import payment_app_import_guard
|
|
|
|
|
2018-05-23 16:40:41 +00:00
|
|
|
|
2018-06-05 19:49:57 +00:00
|
|
|
def create_stripe_subscription(gateway_controller, data):
|
2022-12-08 11:10:13 +00:00
|
|
|
with payment_app_import_guard():
|
|
|
|
import stripe
|
|
|
|
|
2018-06-05 19:49:57 +00:00
|
|
|
stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller)
|
|
|
|
stripe_settings.data = frappe._dict(data)
|
|
|
|
|
|
|
|
stripe.api_key = stripe_settings.get_password(fieldname="secret_key", raise_exception=False)
|
|
|
|
stripe.default_http_client = stripe.http_client.RequestsClient()
|
|
|
|
|
|
|
|
try:
|
2018-06-27 08:27:21 +00:00
|
|
|
stripe_settings.integration_request = create_request_log(stripe_settings.data, "Host", "Stripe")
|
|
|
|
stripe_settings.payment_plans = frappe.get_doc(
|
|
|
|
"Payment Request", stripe_settings.data.reference_docname
|
|
|
|
).subscription_plans
|
|
|
|
return create_subscription_on_stripe(stripe_settings)
|
2018-06-05 19:49:57 +00:00
|
|
|
|
|
|
|
except Exception:
|
2022-05-02 09:34:26 +00:00
|
|
|
stripe_settings.log_error("Unable to create Stripe subscription")
|
2018-06-05 19:49:57 +00:00
|
|
|
return {
|
2021-08-26 16:17:00 +00:00
|
|
|
"redirect_to": frappe.redirect_to_message(
|
|
|
|
_("Server Error"),
|
|
|
|
_(
|
|
|
|
"It seems that there is an issue with the server's stripe configuration. In case of failure, the amount will get refunded to your account."
|
2022-03-28 13:22:46 +00:00
|
|
|
),
|
2021-08-26 16:17:00 +00:00
|
|
|
),
|
2018-06-05 19:49:57 +00:00
|
|
|
"status": 401,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def create_subscription_on_stripe(stripe_settings):
|
2022-12-08 11:10:13 +00:00
|
|
|
with payment_app_import_guard():
|
|
|
|
import stripe
|
|
|
|
|
2021-08-26 16:17:00 +00:00
|
|
|
items = []
|
|
|
|
for payment_plan in stripe_settings.payment_plans:
|
|
|
|
plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "product_price_id")
|
|
|
|
items.append({"price": plan, "quantity": payment_plan.qty})
|
2018-06-20 17:38:13 +00:00
|
|
|
|
2021-08-26 16:17:00 +00:00
|
|
|
try:
|
|
|
|
customer = stripe.Customer.create(
|
|
|
|
source=stripe_settings.data.stripe_token_id,
|
|
|
|
description=stripe_settings.data.payer_name,
|
|
|
|
email=stripe_settings.data.payer_email,
|
|
|
|
)
|
2018-06-05 19:49:57 +00:00
|
|
|
|
2021-08-26 16:17:00 +00:00
|
|
|
subscription = stripe.Subscription.create(customer=customer, items=items)
|
2018-06-05 19:49:57 +00:00
|
|
|
|
2021-08-26 16:17:00 +00:00
|
|
|
if subscription.status == "active":
|
|
|
|
stripe_settings.integration_request.db_set("status", "Completed", update_modified=False)
|
|
|
|
stripe_settings.flags.status_changed_to = "Completed"
|
2018-06-05 19:49:57 +00:00
|
|
|
|
2021-08-26 16:17:00 +00:00
|
|
|
else:
|
2018-06-05 19:49:57 +00:00
|
|
|
stripe_settings.integration_request.db_set("status", "Failed", update_modified=False)
|
2022-05-02 09:34:26 +00:00
|
|
|
frappe.log_error(f"Stripe Subscription ID {subscription.id}: Payment failed")
|
2021-08-26 16:17:00 +00:00
|
|
|
except Exception:
|
|
|
|
stripe_settings.integration_request.db_set("status", "Failed", update_modified=False)
|
2022-05-02 09:34:26 +00:00
|
|
|
stripe_settings.log_error("Unable to create Stripe subscription")
|
2018-06-05 19:49:57 +00:00
|
|
|
|
2021-08-26 16:17:00 +00:00
|
|
|
return stripe_settings.finalize_request()
|