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.
 
 
 
 

86 lines
1.9 KiB

  1. var stripe = Stripe("{{ publishable_key }}");
  2. var elements = stripe.elements();
  3. var style = {
  4. base: {
  5. color: '#32325d',
  6. lineHeight: '18px',
  7. fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
  8. fontSmoothing: 'antialiased',
  9. fontSize: '16px',
  10. '::placeholder': {
  11. color: '#aab7c4'
  12. }
  13. },
  14. invalid: {
  15. color: '#fa755a',
  16. iconColor: '#fa755a'
  17. }
  18. };
  19. var card = elements.create('card', {
  20. hidePostalCode: true,
  21. style: style
  22. });
  23. card.mount('#card-element');
  24. function setOutcome(result) {
  25. if (result.token) {
  26. $('#submit').prop('disabled', true)
  27. $('#submit').html(__('Processing...'))
  28. frappe.call({
  29. method:"payments.templates.pages.stripe_checkout.make_payment",
  30. freeze:true,
  31. headers: {"X-Requested-With": "XMLHttpRequest"},
  32. args: {
  33. "stripe_token_id": result.token.id,
  34. "data": JSON.stringify({{ frappe.form_dict|json }}),
  35. "reference_doctype": "{{ reference_doctype }}",
  36. "reference_docname": "{{ reference_docname }}"
  37. },
  38. callback: function(r) {
  39. if (r.message.status == "Completed") {
  40. $('#submit').hide()
  41. $('.success').show()
  42. setTimeout(function() {
  43. window.location.href = r.message.redirect_to
  44. }, 2000);
  45. } else {
  46. $('#submit').hide()
  47. $('.error').show()
  48. setTimeout(function() {
  49. window.location.href = r.message.redirect_to
  50. }, 2000);
  51. }
  52. }
  53. });
  54. } else if (result.error) {
  55. $('.error').html(result.error.message);
  56. $('.error').show()
  57. }
  58. }
  59. card.on('change', function(event) {
  60. var displayError = document.getElementById('card-errors');
  61. if (event.error) {
  62. displayError.textContent = event.error.message;
  63. } else {
  64. displayError.textContent = '';
  65. }
  66. });
  67. frappe.ready(function() {
  68. $('#submit').off("click").on("click", function(e) {
  69. e.preventDefault();
  70. var extraDetails = {
  71. name: $('input[name=cardholder-name]').val(),
  72. email: $('input[name=cardholder-email]').val()
  73. }
  74. stripe.createToken(card, extraDetails).then(setOutcome);
  75. })
  76. });