Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

93 wiersze
2.9 KiB

  1. # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: MIT. See LICENSE
  3. import json
  4. import frappe
  5. from frappe import _
  6. from frappe.utils import cint, fmt_money
  7. from payments.payment_gateways.doctype.stripe_settings.stripe_settings import get_gateway_controller
  8. no_cache = 1
  9. expected_keys = (
  10. "amount",
  11. "title",
  12. "description",
  13. "reference_doctype",
  14. "reference_docname",
  15. "payer_name",
  16. "payer_email",
  17. "order_id",
  18. "currency",
  19. )
  20. def get_context(context):
  21. context.no_cache = 1
  22. # all these keys exist in form_dict
  23. if not (set(expected_keys) - set(list(frappe.form_dict))):
  24. for key in expected_keys:
  25. context[key] = frappe.form_dict[key]
  26. gateway_controller = get_gateway_controller(context.reference_doctype, context.reference_docname)
  27. context.publishable_key = get_api_key(context.reference_docname, gateway_controller)
  28. context.image = get_header_image(context.reference_docname, gateway_controller)
  29. context["amount"] = fmt_money(amount=context["amount"], currency=context["currency"])
  30. if is_a_subscription(context.reference_doctype, context.reference_docname):
  31. payment_plan = frappe.db.get_value(
  32. context.reference_doctype, context.reference_docname, "payment_plan"
  33. )
  34. recurrence = frappe.db.get_value("Payment Plan", payment_plan, "recurrence")
  35. context["amount"] = context["amount"] + " " + _(recurrence)
  36. else:
  37. frappe.redirect_to_message(
  38. _("Some information is missing"),
  39. _("Looks like someone sent you to an incomplete URL. Please ask them to look into it."),
  40. )
  41. frappe.local.flags.redirect_location = frappe.local.response.location
  42. raise frappe.Redirect
  43. def get_api_key(doc, gateway_controller):
  44. publishable_key = frappe.db.get_value("Stripe Settings", gateway_controller, "publishable_key")
  45. if cint(frappe.form_dict.get("use_sandbox")):
  46. publishable_key = frappe.conf.sandbox_publishable_key
  47. return publishable_key
  48. def get_header_image(doc, gateway_controller):
  49. header_image = frappe.db.get_value("Stripe Settings", gateway_controller, "header_img")
  50. return header_image
  51. @frappe.whitelist(allow_guest=True)
  52. def make_payment(stripe_token_id, data, reference_doctype=None, reference_docname=None):
  53. data = json.loads(data)
  54. data.update({"stripe_token_id": stripe_token_id})
  55. gateway_controller = get_gateway_controller(reference_doctype, reference_docname)
  56. if is_a_subscription(reference_doctype, reference_docname):
  57. reference = frappe.get_doc(reference_doctype, reference_docname)
  58. data = reference.create_subscription("stripe", gateway_controller, data)
  59. else:
  60. data = frappe.get_doc("Stripe Settings", gateway_controller).create_request(data)
  61. frappe.db.commit()
  62. return data
  63. def is_a_subscription(reference_doctype, reference_docname):
  64. if not frappe.get_meta(reference_doctype).has_field("is_a_subscription"):
  65. return False
  66. return frappe.db.get_value(reference_doctype, reference_docname, "is_a_subscription")