Charles-Henri Decultot | 30da37f | 2018-05-23 16:40:41 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors |
| 3 | # For license information, please see license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 6 | import frappe |
| 7 | from frappe import _ |
| 8 | from frappe.integrations.utils import create_request_log |
| 9 | import stripe |
Charles-Henri Decultot | 30da37f | 2018-05-23 16:40:41 +0000 | [diff] [blame] | 10 | |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 11 | def 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 Decultot | cde1f08 | 2018-06-27 08:27:21 +0000 | [diff] [blame] | 19 | 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 Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 22 | |
| 23 | except Exception: |
| 24 | frappe.log_error(frappe.get_traceback()) |
| 25 | return{ |
| 26 | "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.")), |
| 27 | "status": 401 |
| 28 | } |
| 29 | |
| 30 | |
| 31 | def create_subscription_on_stripe(stripe_settings): |
Charles-Henri Decultot | 0134e13 | 2018-06-20 17:38:13 +0000 | [diff] [blame] | 32 | items = [] |
| 33 | for payment_plan in stripe_settings.payment_plans: |
| 34 | plan = frappe.db.get_value("Subscription Plan", payment_plan.plan, "payment_plan_id") |
| 35 | items.append({"plan": plan, "quantity": payment_plan.qty}) |
| 36 | |
Charles-Henri Decultot | ba6f6d9 | 2018-06-05 19:49:57 +0000 | [diff] [blame] | 37 | try: |
| 38 | customer = stripe.Customer.create(description=stripe_settings.data.payer_name, email=stripe_settings.data.payer_email, source=stripe_settings.data.stripe_token_id) |
| 39 | subscription = stripe.Subscription.create(customer=customer, items=items) |
| 40 | |
| 41 | if subscription.status == "active": |
| 42 | stripe_settings.integration_request.db_set('status', 'Completed', update_modified=False) |
| 43 | stripe_settings.flags.status_changed_to = "Completed" |
| 44 | |
| 45 | else: |
| 46 | stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) |
| 47 | frappe.log_error('Subscription N°: ' + subscription.id, 'Stripe Payment not completed') |
| 48 | |
| 49 | except Exception: |
| 50 | stripe_settings.integration_request.db_set('status', 'Failed', update_modified=False) |
| 51 | frappe.log_error(frappe.get_traceback()) |
| 52 | |
Charles-Henri Decultot | 0134e13 | 2018-06-20 17:38:13 +0000 | [diff] [blame] | 53 | return stripe_settings.finalize_request() |