選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

71 行
2.4 KiB

  1. # Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
  2. # For license information, please see license.txt
  3. import frappe
  4. from frappe import _
  5. from frappe.integrations.utils import create_request_log
  6. from erpnext.utilities import payment_app_import_guard
  7. def create_stripe_subscription(gateway_controller, data):
  8. with payment_app_import_guard():
  9. import stripe
  10. stripe_settings = frappe.get_doc("Stripe Settings", gateway_controller)
  11. stripe_settings.data = frappe._dict(data)
  12. stripe.api_key = stripe_settings.get_password(fieldname="secret_key", raise_exception=False)
  13. stripe.default_http_client = stripe.http_client.RequestsClient()
  14. try:
  15. stripe_settings.integration_request = create_request_log(stripe_settings.data, "Host", "Stripe")
  16. stripe_settings.payment_plans = frappe.get_doc(
  17. "Payment Request", stripe_settings.data.reference_docname
  18. ).subscription_plans
  19. return create_subscription_on_stripe(stripe_settings)
  20. except Exception:
  21. stripe_settings.log_error("Unable to create Stripe subscription")
  22. return {
  23. "redirect_to": frappe.redirect_to_message(
  24. _("Server Error"),
  25. _(
  26. "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. ),
  28. ),
  29. "status": 401,
  30. }
  31. def create_subscription_on_stripe(stripe_settings):
  32. with payment_app_import_guard():
  33. import stripe
  34. 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})
  38. try:
  39. customer = stripe.Customer.create(
  40. source=stripe_settings.data.stripe_token_id,
  41. description=stripe_settings.data.payer_name,
  42. email=stripe_settings.data.payer_email,
  43. )
  44. subscription = stripe.Subscription.create(customer=customer, items=items)
  45. if subscription.status == "active":
  46. stripe_settings.integration_request.db_set("status", "Completed", update_modified=False)
  47. stripe_settings.flags.status_changed_to = "Completed"
  48. else:
  49. stripe_settings.integration_request.db_set("status", "Failed", update_modified=False)
  50. frappe.log_error(f"Stripe Subscription ID {subscription.id}: Payment failed")
  51. except Exception:
  52. stripe_settings.integration_request.db_set("status", "Failed", update_modified=False)
  53. stripe_settings.log_error("Unable to create Stripe subscription")
  54. return stripe_settings.finalize_request()