blob: 502cb5f00a4467e1c19252e4b9475ade783f5d55 [file] [log] [blame]
Charles-Henri Decultot30da37f2018-05-23 16:40:41 +00001# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
2# For license information, please see license.txt
3
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +00004import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05305import stripe
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +00006from frappe import _
7from frappe.integrations.utils import create_request_log
Rohan16eed072021-08-26 21:47:00 +05308
Charles-Henri Decultot30da37f2018-05-23 16:40:41 +00009
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000010def create_stripe_subscription(gateway_controller, data):
11 stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller)
12 stripe_settings.data = frappe._dict(data)
13
14 stripe.api_key = stripe_settings.get_password(fieldname="secret_key", raise_exception=False)
15 stripe.default_http_client = stripe.http_client.RequestsClient()
16
17 try:
Charles-Henri Decultotcde1f082018-06-27 08:27:21 +000018 stripe_settings.integration_request = create_request_log(stripe_settings.data, "Host", "Stripe")
19 stripe_settings.payment_plans = frappe.get_doc("Payment Request", stripe_settings.data.reference_docname).subscription_plans
20 return create_subscription_on_stripe(stripe_settings)
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000021
22 except Exception:
23 frappe.log_error(frappe.get_traceback())
24 return{
Rohan16eed072021-08-26 21:47:00 +053025 "redirect_to": frappe.redirect_to_message(
26 _('Server Error'),
27 _("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.")
28 ),
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000029 "status": 401
30 }
31
32
33def create_subscription_on_stripe(stripe_settings):
Rohan16eed072021-08-26 21:47:00 +053034 items = []
35 for payment_plan in stripe_settings.payment_plans:
36 plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "product_price_id")
37 items.append({"price": plan, "quantity": payment_plan.qty})
Charles-Henri Decultot0134e132018-06-20 17:38:13 +000038
Rohan16eed072021-08-26 21:47:00 +053039 try:
40 customer = stripe.Customer.create(
41 source=stripe_settings.data.stripe_token_id,
42 description=stripe_settings.data.payer_name,
43 email=stripe_settings.data.payer_email
44 )
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000045
Rohan16eed072021-08-26 21:47:00 +053046 subscription = stripe.Subscription.create(customer=customer, items=items)
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000047
Rohan16eed072021-08-26 21:47:00 +053048 if subscription.status == "active":
49 stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False)
50 stripe_settings.flags.status_changed_to = "Completed"
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000051
Rohan16eed072021-08-26 21:47:00 +053052 else:
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000053 stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False)
Rohan16eed072021-08-26 21:47:00 +053054 frappe.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed')
55 except Exception:
56 stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False)
57 frappe.log_error(frappe.get_traceback())
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000058
Rohan16eed072021-08-26 21:47:00 +053059 return stripe_settings.finalize_request()