From 38aebf65e2c9a93e3fc0d999668040f471c55b51 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Mon, 25 Sep 2023 11:08:24 +0530 Subject: [PATCH] refactor!: remove `stripe_integration.py` --- .../payment_request/payment_request.py | 4 +- .../stripe_integration.py | 70 ------------------- 2 files changed, 3 insertions(+), 71 deletions(-) delete mode 100644 erpnext/erpnext_integrations/stripe_integration.py diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 11d6d5f433..028efc4d6d 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -20,7 +20,6 @@ from erpnext.accounts.doctype.payment_entry.payment_entry import ( from erpnext.accounts.doctype.subscription_plan.subscription_plan import get_plan_rate from erpnext.accounts.party import get_party_account, get_party_bank_account from erpnext.accounts.utils import get_account_currency -from erpnext.erpnext_integrations.stripe_integration import create_stripe_subscription from erpnext.utilities import payment_app_import_guard @@ -393,6 +392,9 @@ class PaymentRequest(Document): def create_subscription(self, payment_provider, gateway_controller, data): if payment_provider == "stripe": + with payment_app_import_guard(): + from payments.payment_gateways.stripe_integration import create_stripe_subscription + return create_stripe_subscription(gateway_controller, data) diff --git a/erpnext/erpnext_integrations/stripe_integration.py b/erpnext/erpnext_integrations/stripe_integration.py deleted file mode 100644 index 634e5c2e89..0000000000 --- a/erpnext/erpnext_integrations/stripe_integration.py +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors -# For license information, please see license.txt - -import frappe -from frappe import _ -from frappe.integrations.utils import create_request_log - -from erpnext.utilities import payment_app_import_guard - - -def create_stripe_subscription(gateway_controller, data): - with payment_app_import_guard(): - import stripe - - 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: - 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) - - except Exception: - stripe_settings.log_error("Unable to create Stripe subscription") - return { - "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." - ), - ), - "status": 401, - } - - -def create_subscription_on_stripe(stripe_settings): - with payment_app_import_guard(): - import stripe - - 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}) - - try: - customer = stripe.Customer.create( - source=stripe_settings.data.stripe_token_id, - description=stripe_settings.data.payer_name, - email=stripe_settings.data.payer_email, - ) - - subscription = stripe.Subscription.create(customer=customer, items=items) - - if subscription.status == "active": - stripe_settings.integration_request.db_set("status", "Completed", update_modified=False) - stripe_settings.flags.status_changed_to = "Completed" - - else: - stripe_settings.integration_request.db_set("status", "Failed", update_modified=False) - frappe.log_error(f"Stripe Subscription ID {subscription.id}: Payment failed") - except Exception: - stripe_settings.integration_request.db_set("status", "Failed", update_modified=False) - stripe_settings.log_error("Unable to create Stripe subscription") - - return stripe_settings.finalize_request()