Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1.  
  2. @{
  3. Layout = string.Empty;
  4. }
  5.  
  6. <meta name="viewport" content="width=device-width, initial-scale=1" />
  7.  
  8. <script src="https://js.stripe.com/v3/"></script>
  9. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  10.  
  11. <div class="row">
  12. <input id="cardholder-name" placeholder="Ditt navn" class="my-input" type="text">
  13. </div>
  14.  
  15. <div class="row">
  16. <!-- placeholder for Elements -->
  17. <div class="card-input" style="margin-top: 10px;">
  18. <div id="card-element"/>
  19. </div>
  20. </div>
  21.  
  22. <div class="row">
  23. <!-- Used to display form errors. -->
  24. <div id="card-errors" role="alert"></div>
  25. <button id="card-button"
  26. data-secret='@ViewData["ClientSecret"]'>
  27. Legg til
  28. </button>
  29. </div>
  30.  
  31.  
  32. <script type="text/javascript">
  33. var stripePublishableKey = '@Html.Raw(ViewData["publishableKey"] as string)';
  34. var accessToken = '@Html.Raw(ViewData["AccessToken"] as string)';
  35.  
  36. var stripe = Stripe(stripePublishableKey);
  37.  
  38. var style = {
  39. base: {
  40. color: '#fff',
  41. fontSize: '20px',
  42. '::placeholder': {
  43. color: '#fff'
  44. },
  45. cardNumber: {
  46. textAlign: 'middle'
  47. }
  48. },
  49. invalid: {
  50. iconColor: '#f44336',
  51. color: '#f44336'
  52. }
  53. };
  54.  
  55. // Create an instance of Elements.
  56. var elements = stripe.elements();
  57. var cardElement = elements.create('card', { style: style });
  58. cardElement.mount('#card-element');
  59.  
  60. var cardholderName = document.getElementById('cardholder-name');
  61. var cardButton = document.getElementById('card-button');
  62. var clientSecret = cardButton.dataset.secret;
  63.  
  64. // Handle real-time validation errors from the card Element.
  65. cardElement.addEventListener('change', function(event) {
  66. var displayError = document.getElementById('card-errors');
  67. if (event.error) {
  68. displayError.textContent = event.error.message;
  69. } else {
  70. displayError.textContent = '';
  71. }
  72. });
  73.  
  74. cardButton.addEventListener('click', function (ev) {
  75. stripe.handleCardSetup(
  76. clientSecret, cardElement, {
  77. payment_method_data: {
  78. billing_details: {
  79. name: cardholderName.value
  80. }
  81. }
  82. }
  83. ).then(function(result) {
  84. if (result.error) {
  85. // Display error.message in your UI.
  86. var errorElement = document.getElementById('card-errors');
  87. errorElement.textContent = result.error.message;
  88. } else {
  89. // The setup has succeeded. Display a success message.
  90. console.log(result);
  91. saveCard(result, accessToken);
  92. }
  93. });
  94. });
  95.  
  96. function saveCard(jsonData, accessToken) {
  97. $.ajax({
  98. type: 'POST',
  99. headers: {
  100. "Authorization": accessToken
  101. // Includes Bearer
  102. },
  103. // Note the difference in url (this works if you're actually in Index page)
  104. url: '/api/v2.1/user/save-card',
  105. data: JSON.stringify(jsonData.setupIntent),
  106. contentType: 'application/json',
  107. success: function(data) {
  108. window.location.href = "https://www.example.com/success";
  109. },
  110. error: function (error) {
  111. window.location.href = "https://www.example.com/failure";
  112. }
  113. });
  114. }
  115. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement