Skip to content

Commit

Permalink
Merge pull request #30 from Alchez/fix-stripe-price-id
Browse files Browse the repository at this point in the history
  • Loading branch information
vjFaLk authored Jul 12, 2021
2 parents 3f439c2 + 46ecfef commit 150d405
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand All @@ -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",
Expand Down
46 changes: 27 additions & 19 deletions erpnext/erpnext_integrations/stripe_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
return stripe_settings.finalize_request()
1 change: 1 addition & 0 deletions erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,4 @@ 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.v12_0.create_taxable_value_field_in_purchase_invoice
execute:from frappe.model.utils.rename_field import rename_field;rename_field("Subscription Plan", "payment_plan_id", "product_price_id")

0 comments on commit 150d405

Please sign in to comment.