blob: f0315eb7eef3a9d476c36cf8a9b243db5c4acbe6 [file] [log] [blame]
Charles-Henri Decultot30da37f2018-05-23 16:40:41 +00001# -*- coding: utf-8 -*-
2# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
3# For license information, please see license.txt
4
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +00005import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05306import stripe
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +00007from frappe import _
8from frappe.integrations.utils import create_request_log
Rohan16eed072021-08-26 21:47:00 +05309
Charles-Henri Decultot30da37f2018-05-23 16:40:41 +000010
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000011def create_stripe_subscription(gateway_controller, data):
12 stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller)
13 stripe_settings.data = frappe._dict(data)
14
15 stripe.api_key = stripe_settings.get_password(fieldname="secret_key", raise_exception=False)
16 stripe.default_http_client = stripe.http_client.RequestsClient()
17
18 try:
Charles-Henri Decultotcde1f082018-06-27 08:27:21 +000019 stripe_settings.integration_request = create_request_log(stripe_settings.data, "Host", "Stripe")
20 stripe_settings.payment_plans = frappe.get_doc("Payment Request", stripe_settings.data.reference_docname).subscription_plans
21 return create_subscription_on_stripe(stripe_settings)
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000022
23 except Exception:
24 frappe.log_error(frappe.get_traceback())
25 return{
Rohan16eed072021-08-26 21:47:00 +053026 "redirect_to": frappe.redirect_to_message(
27 _('Server Error'),
28 _("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.")
29 ),
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000030 "status": 401
31 }
32
33
34def create_subscription_on_stripe(stripe_settings):
Rohan16eed072021-08-26 21:47:00 +053035 items = []
36 for payment_plan in stripe_settings.payment_plans:
37 plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "product_price_id")
38 items.append({"price": plan, "quantity": payment_plan.qty})
Charles-Henri Decultot0134e132018-06-20 17:38:13 +000039
Rohan16eed072021-08-26 21:47:00 +053040 try:
41 customer = stripe.Customer.create(
42 source=stripe_settings.data.stripe_token_id,
43 description=stripe_settings.data.payer_name,
44 email=stripe_settings.data.payer_email
45 )
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000046
Rohan16eed072021-08-26 21:47:00 +053047 subscription = stripe.Subscription.create(customer=customer, items=items)
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000048
Rohan16eed072021-08-26 21:47:00 +053049 if subscription.status == "active":
50 stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False)
51 stripe_settings.flags.status_changed_to = "Completed"
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000052
Rohan16eed072021-08-26 21:47:00 +053053 else:
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000054 stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False)
Rohan16eed072021-08-26 21:47:00 +053055 frappe.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed')
56 except Exception:
57 stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False)
58 frappe.log_error(frappe.get_traceback())
Charles-Henri Decultotba6f6d92018-06-05 19:49:57 +000059
Rohan16eed072021-08-26 21:47:00 +053060 return stripe_settings.finalize_request()