fix: use Stripe's Price API for plan-price information (#27183)

* fix: use Stripe's Price API for plan-price information (#26107)

* fix: use Stripe's new Plan API for price information

* patch: use inbuilt function to rename field

* fix: patch call

Co-authored-by: Ankush Menat <ankush@iwebnotes.com>
Co-authored-by: Nabin Hait <nabinhait@gmail.com>
(cherry picked from commit 16eed07a0f)

# Conflicts:
#	erpnext/patches.txt

* fix: resolve conflicts

Co-authored-by: Rohan <Alchez@users.noreply.github.com>
Co-authored-by: Ankush Menat <ankush@iwebnotes.com>
Co-authored-by: Afshan <33727827+AfshanKhan@users.noreply.github.com>
This commit is contained in:
Frappe PR Bot
2021-08-27 10:45:34 +05:30
committed by GitHub
parent 21d5b3cf4d
commit 03039b9e00
4 changed files with 42 additions and 26 deletions

View File

@@ -21,7 +21,7 @@
"column_break_13", "column_break_13",
"billing_interval_count", "billing_interval_count",
"payment_plan_section", "payment_plan_section",
"payment_plan_id", "product_price_id",
"column_break_16", "column_break_16",
"payment_gateway", "payment_gateway",
"accounting_dimensions_section", "accounting_dimensions_section",
@@ -114,11 +114,6 @@
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Payment Plan" "label": "Payment Plan"
}, },
{
"fieldname": "payment_plan_id",
"fieldtype": "Data",
"label": "Payment Plan"
},
{ {
"fieldname": "column_break_16", "fieldname": "column_break_16",
"fieldtype": "Column Break" "fieldtype": "Column Break"
@@ -144,10 +139,15 @@
"fieldtype": "Link", "fieldtype": "Link",
"label": "Cost Center", "label": "Cost Center",
"options": "Cost Center" "options": "Cost Center"
},
{
"fieldname": "product_price_id",
"fieldtype": "Data",
"label": "Product Price ID"
} }
], ],
"links": [], "links": [],
"modified": "2021-08-09 10:53:44.205774", "modified": "2021-08-13 10:53:44.205774",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Subscription Plan", "name": "Subscription Plan",

View File

@@ -2,11 +2,12 @@
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors # Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt # For license information, please see license.txt
from __future__ import unicode_literals import stripe
import frappe import frappe
from frappe import _ from frappe import _
from frappe.integrations.utils import create_request_log from frappe.integrations.utils import create_request_log
import stripe
def create_stripe_subscription(gateway_controller, data): def create_stripe_subscription(gateway_controller, data):
stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller) stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller)
@@ -23,31 +24,38 @@ def create_stripe_subscription(gateway_controller, data):
except Exception: except Exception:
frappe.log_error(frappe.get_traceback()) frappe.log_error(frappe.get_traceback())
return{ 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 "status": 401
} }
def create_subscription_on_stripe(stripe_settings): def create_subscription_on_stripe(stripe_settings):
items = [] items = []
for payment_plan in stripe_settings.payment_plans: for payment_plan in stripe_settings.payment_plans:
plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "payment_plan_id") plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "product_price_id")
items.append({"plan": plan, "quantity": payment_plan.qty}) items.append({"price": plan, "quantity": payment_plan.qty})
try: try:
customer = stripe.Customer.create(description=stripe_settings.data.payer_name, email=stripe_settings.data.payer_email, source=stripe_settings.data.stripe_token_id) customer = stripe.Customer.create(
subscription = stripe.Subscription.create(customer=customer, items=items) source=stripe_settings.data.stripe_token_id,
description=stripe_settings.data.payer_name,
email=stripe_settings.data.payer_email
)
if subscription.status == "active": subscription = stripe.Subscription.create(customer=customer, items=items)
stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False)
stripe_settings.flags.status_changed_to = "Completed"
else: if subscription.status == "active":
stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False)
frappe.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed') stripe_settings.flags.status_changed_to = "Completed"
except Exception: else:
stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) 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() return stripe_settings.finalize_request()

View File

@@ -303,4 +303,5 @@ erpnext.patches.v13_0.update_recipient_email_digest
erpnext.patches.v13_0.shopify_deprecation_warning erpnext.patches.v13_0.shopify_deprecation_warning
erpnext.patches.v13_0.add_custom_field_for_south_africa #2 erpnext.patches.v13_0.add_custom_field_for_south_africa #2
erpnext.patches.v13_0.rename_discharge_ordered_date_in_ip_record erpnext.patches.v13_0.rename_discharge_ordered_date_in_ip_record
erpnext.patches.v13_0.migrate_stripe_api
erpnext.patches.v13_0.reset_clearance_date_for_intracompany_payment_entries erpnext.patches.v13_0.reset_clearance_date_for_intracompany_payment_entries

View File

@@ -0,0 +1,7 @@
import frappe
from frappe.model.utils.rename_field import rename_field
def execute():
frappe.reload_doc("accounts", "doctype", "subscription_plan")
rename_field("Subscription Plan", "payment_plan_id", "product_price_id")