From 81e22e738e0a3bf778f1fc0f93312958d616f2d0 Mon Sep 17 00:00:00 2001 From: Rohan Bansal Date: Fri, 18 Jun 2021 18:35:57 +0530 Subject: [PATCH 1/3] fix: use Stripe's new Plan API for price information --- .../subscription_plan/subscription_plan.json | 14 +++--- .../stripe_integration.py | 46 +++++++++++-------- erpnext/patches.txt | 1 + erpnext/patches/v13_0/migrate_stripe_api.py | 16 +++++++ 4 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 erpnext/patches/v13_0/migrate_stripe_api.py diff --git a/erpnext/accounts/doctype/subscription_plan/subscription_plan.json b/erpnext/accounts/doctype/subscription_plan/subscription_plan.json index 9f790662356b..177e6d9b9555 100644 --- a/erpnext/accounts/doctype/subscription_plan/subscription_plan.json +++ b/erpnext/accounts/doctype/subscription_plan/subscription_plan.json @@ -20,7 +20,7 @@ "column_break_13", "billing_interval_count", "payment_plan_section", - "payment_plan_id", + "product_price_id", "column_break_16", "payment_gateway", "accounting_dimensions_section", @@ -112,11 +112,6 @@ "fieldtype": "Section Break", "label": "Payment Plan" }, - { - "fieldname": "payment_plan_id", - "fieldtype": "Data", - "label": "Payment Plan" - }, { "fieldname": "column_break_16", "fieldtype": "Column Break" @@ -136,9 +131,14 @@ { "fieldname": "dimension_col_break", "fieldtype": "Column Break" + }, + { + "fieldname": "product_price_id", + "fieldtype": "Data", + "label": "Product Price ID" } ], - "modified": "2019-07-25 18:35:04.362556", + "modified": "2021-06-18 04:37:19.471221", "modified_by": "Administrator", "module": "Accounts", "name": "Subscription Plan", diff --git a/erpnext/erpnext_integrations/stripe_integration.py b/erpnext/erpnext_integrations/stripe_integration.py index a35ca28e0a3c..820c7405328d 100644 --- a/erpnext/erpnext_integrations/stripe_integration.py +++ b/erpnext/erpnext_integrations/stripe_integration.py @@ -2,11 +2,12 @@ # Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt -from __future__ import unicode_literals +import stripe + import frappe from frappe import _ from frappe.integrations.utils import create_request_log -import stripe + def create_stripe_subscription(gateway_controller, data): stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller) @@ -23,31 +24,38 @@ def create_stripe_subscription(gateway_controller, data): except Exception: frappe.log_error(frappe.get_traceback()) 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.")), + "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): - items = [] - for payment_plan in stripe_settings.payment_plans: - plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "payment_plan_id") - items.append({"plan": plan, "quantity": payment_plan.qty}) + 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(description=stripe_settings.data.payer_name, email=stripe_settings.data.payer_email, source=stripe_settings.data.stripe_token_id) - subscription = stripe.Subscription.create(customer=customer, items=items) + try: + customer = stripe.Customer.create( + source=stripe_settings.data.stripe_token_id, + description=stripe_settings.data.payer_name, + email=stripe_settings.data.payer_email + ) - if subscription.status == "active": - stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False) - stripe_settings.flags.status_changed_to = "Completed" + subscription = stripe.Subscription.create(customer=customer, items=items) - else: - stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) - frappe.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed') + if subscription.status == "active": + stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False) + stripe_settings.flags.status_changed_to = "Completed" - except Exception: + else: stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) - frappe.log_error(frappe.get_traceback()) + frappe.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed') + except Exception: + stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) + frappe.log_error(frappe.get_traceback()) - return stripe_settings.finalize_request() \ No newline at end of file + return stripe_settings.finalize_request() diff --git a/erpnext/patches.txt b/erpnext/patches.txt index aa70e709685c..f46f7bd8a6e7 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -685,3 +685,4 @@ erpnext.patches.v12_0.create_taxable_value_field erpnext.patches.v12_0.purchase_receipt_status erpnext.patches.v12_0.add_company_link_to_einvoice_settings erpnext.patches.v12_0.add_document_type_field_for_italy_einvoicing +erpnext.patches.v13_0.migrate_stripe_api diff --git a/erpnext/patches/v13_0/migrate_stripe_api.py b/erpnext/patches/v13_0/migrate_stripe_api.py new file mode 100644 index 000000000000..29ed5471dfd2 --- /dev/null +++ b/erpnext/patches/v13_0/migrate_stripe_api.py @@ -0,0 +1,16 @@ +import frappe + + +def execute(): + subscription_plans = frappe.get_all( + "Subscription Plan", fields=["name", "payment_plan_id"] + ) + + for plan in subscription_plans: + frappe.db.set_value( + "Subscription Plan", + plan.name, + "product_price_id", + plan.payment_plan_id, + update_modified=False + ) From 054ab0782aac5a8be6150ca1ed1f7497442683ef Mon Sep 17 00:00:00 2001 From: Rohan Bansal Date: Tue, 22 Jun 2021 16:19:09 +0530 Subject: [PATCH 2/3] patch: use inbuilt function to rename field --- erpnext/patches.txt | 2 +- erpnext/patches/v13_0/migrate_stripe_api.py | 16 ---------------- 2 files changed, 1 insertion(+), 17 deletions(-) delete mode 100644 erpnext/patches/v13_0/migrate_stripe_api.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index f46f7bd8a6e7..4ccfc1f83849 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -685,4 +685,4 @@ erpnext.patches.v12_0.create_taxable_value_field erpnext.patches.v12_0.purchase_receipt_status erpnext.patches.v12_0.add_company_link_to_einvoice_settings erpnext.patches.v12_0.add_document_type_field_for_italy_einvoicing -erpnext.patches.v13_0.migrate_stripe_api +execute:frappe.model.utils.rename_field("Subscription Plan", "payment_plan_id", "product_price_id") diff --git a/erpnext/patches/v13_0/migrate_stripe_api.py b/erpnext/patches/v13_0/migrate_stripe_api.py deleted file mode 100644 index 29ed5471dfd2..000000000000 --- a/erpnext/patches/v13_0/migrate_stripe_api.py +++ /dev/null @@ -1,16 +0,0 @@ -import frappe - - -def execute(): - subscription_plans = frappe.get_all( - "Subscription Plan", fields=["name", "payment_plan_id"] - ) - - for plan in subscription_plans: - frappe.db.set_value( - "Subscription Plan", - plan.name, - "product_price_id", - plan.payment_plan_id, - update_modified=False - ) From 631aa6277ab852d3915a54ca9a797be3cd4c1674 Mon Sep 17 00:00:00 2001 From: Rohan Bansal Date: Mon, 28 Jun 2021 16:10:16 +0530 Subject: [PATCH 3/3] fix: patch call --- erpnext/patches.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 4ccfc1f83849..638f35f9d03d 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -685,4 +685,4 @@ erpnext.patches.v12_0.create_taxable_value_field erpnext.patches.v12_0.purchase_receipt_status erpnext.patches.v12_0.add_company_link_to_einvoice_settings erpnext.patches.v12_0.add_document_type_field_for_italy_einvoicing -execute:frappe.model.utils.rename_field("Subscription Plan", "payment_plan_id", "product_price_id") +execute:from frappe.model.utils.rename_field import rename_field;rename_field("Subscription Plan", "payment_plan_id", "product_price_id")