Charles-Henri Decultot | 30da37f | 2018-05-23 16:40:41 +0000 | [diff] [blame] | 1 | # Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors |
| 2 | # For license information, please see license.txt |
| 3 | |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 4 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | import stripe |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 6 | from frappe import _ |
| 7 | from frappe.integrations.utils import create_request_log |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 8 | |
Charles-Henri Decultot | 30da37f | 2018-05-23 16:40:41 +0000 | [diff] [blame] | 9 | |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 10 | def 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 Decultot | cde1f08 | 2018-06-27 08:27:21 +0000 | [diff] [blame] | 18 | stripe_settings.integration_request = create_request_log(stripe_settings.data, "Host", "Stripe") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | stripe_settings.payment_plans = frappe.get_doc( |
| 20 | "Payment Request", stripe_settings.data.reference_docname |
| 21 | ).subscription_plans |
Charles-Henri Decultot | cde1f08 | 2018-06-27 08:27:21 +0000 | [diff] [blame] | 22 | return create_subscription_on_stripe(stripe_settings) |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 23 | |
| 24 | except Exception: |
| 25 | frappe.log_error(frappe.get_traceback()) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | return { |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 27 | "redirect_to": frappe.redirect_to_message( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 28 | _("Server Error"), |
| 29 | _( |
| 30 | "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." |
| 31 | ), |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 32 | ), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 33 | "status": 401, |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | |
| 37 | def create_subscription_on_stripe(stripe_settings): |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 38 | items = [] |
| 39 | for payment_plan in stripe_settings.payment_plans: |
| 40 | plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "product_price_id") |
| 41 | items.append({"price": plan, "quantity": payment_plan.qty}) |
Charles-Henri Decultot | 0134e13 | 2018-06-20 17:38:13 +0000 | [diff] [blame] | 42 | |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 43 | try: |
| 44 | customer = stripe.Customer.create( |
| 45 | source=stripe_settings.data.stripe_token_id, |
| 46 | description=stripe_settings.data.payer_name, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 47 | email=stripe_settings.data.payer_email, |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 48 | ) |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 49 | |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 50 | subscription = stripe.Subscription.create(customer=customer, items=items) |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 51 | |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 52 | if subscription.status == "active": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 53 | stripe_settings.integration_request.db_set("status", "Completed", update_modified=False) |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 54 | stripe_settings.flags.status_changed_to = "Completed" |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 55 | |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 56 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | stripe_settings.integration_request.db_set("status", "Failed", update_modified=False) |
| 58 | frappe.log_error("Subscription N°: " + subscription.id, "Stripe Payment not completed") |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 59 | except Exception: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 60 | stripe_settings.integration_request.db_set("status", "Failed", update_modified=False) |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 61 | frappe.log_error(frappe.get_traceback()) |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 62 | |
Rohan | 16eed07 | 2021-08-26 21:47:00 +0530 | [diff] [blame] | 63 | return stripe_settings.finalize_request() |