You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 line
2.7 KiB

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