Stripe integration corrections
diff --git a/erpnext/accounts/doctype/payment_request/payment_request.js b/erpnext/accounts/doctype/payment_request/payment_request.js
index dd63a4a..379dc86 100644
--- a/erpnext/accounts/doctype/payment_request/payment_request.js
+++ b/erpnext/accounts/doctype/payment_request/payment_request.js
@@ -56,3 +56,8 @@
}).addClass("btn-primary");
}
});
+
+frappe.ui.form.on("Payment Request", "is_a_subscription", function(frm) {
+ frm.toggle_reqd("payment_gateway_account", frm.doc.is_a_subscription);
+ frm.toggle_reqd("payment_plan", frm.doc.is_a_subscription);
+});
diff --git a/erpnext/config/integrations.py b/erpnext/config/integrations.py
index 14d917f..e27b7cd 100644
--- a/erpnext/config/integrations.py
+++ b/erpnext/config/integrations.py
@@ -9,11 +9,6 @@
"items": [
{
"type": "doctype",
- "name": "Stripe Settings",
- "description": _("Stripe payment gateway settings"),
- },
- {
- "type": "doctype",
"name": "GoCardless Settings",
"description": _("GoCardless payment gateway settings"),
},
diff --git a/erpnext/erpnext_integrations/doctype/payment_plan/payment_plan.py b/erpnext/erpnext_integrations/doctype/payment_plan/payment_plan.py
index ec2954e..0e71fbc 100644
--- a/erpnext/erpnext_integrations/doctype/payment_plan/payment_plan.py
+++ b/erpnext/erpnext_integrations/doctype/payment_plan/payment_plan.py
@@ -4,6 +4,56 @@
from __future__ import unicode_literals
from frappe.model.document import Document
+import frappe
+from frappe import _
+from frappe.integrations.utils import create_request_log
+import stripe
class PaymentPlan(Document):
pass
+
+
+def create_stripe_subscription(gateway_controller, data):
+ 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_plan = frappe.db.get_value("Payment Request", stripe_settings.data.reference_docname, 'payment_plan')
+ return create_subscription_on_stripe(stripe_settings)
+
+ 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.")),
+ "status": 401
+ }
+
+
+def create_subscription_on_stripe(stripe_settings):
+ items = [
+ {
+ "plan": stripe_settings.payment_plan
+ }
+ ]
+
+ 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)
+
+ 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('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()
diff --git a/erpnext/templates/pages/integrations/stripe_checkout.py b/erpnext/templates/pages/integrations/stripe_checkout.py
index f1f86e9..6faf8f1 100644
--- a/erpnext/templates/pages/integrations/stripe_checkout.py
+++ b/erpnext/templates/pages/integrations/stripe_checkout.py
@@ -6,6 +6,7 @@
from frappe.utils import cint, fmt_money
import json
from erpnext.erpnext_integrations.doctype.stripe_settings.stripe_settings import get_gateway_controller
+from erpnext.erpnext_integrations.doctype.payment_plan.payment_plan import create_stripe_subscription
no_cache = 1
no_sitemap = 1
@@ -59,6 +60,11 @@
})
gateway_controller = get_gateway_controller(reference_docname)
- data = frappe.get_doc("Stripe Settings", gateway_controller).create_request(data)
+
+ if frappe.db.get_value("Payment Request", reference_docname, 'is_a_subscription'):
+ data = create_stripe_subscription(gateway_controller, data)
+ else:
+ data = frappe.get_doc("Stripe Settings", gateway_controller).create_request(data)
+
frappe.db.commit()
return data