简体   繁体   English

无法获得Stripe示例正常运行

[英]Can't get Stripe example to function correctly

I'm trying to get a Stripe payment form added to a laravel project. 我正在尝试将Stripe付款表单添加到laravel项目中。 After many things not working I'm now just trying to get one of their examples working. 在许多事情不起作用之后,我现在只是尝试使他们的示例之一起作用。

Here is a link to the examples, I'm using example #2: https://github.com/stripe/elements-examples/#example-2 这是示例的链接,我正在使用示例2: https//github.com/stripe/elements-examples/#example-2

Here is a link to the working examples: https://stripe.github.io/elements-examples/ 这是工作示例的链接: https : //stripe.github.io/elements-examples/

All the JS, CSS, and HTML is taken directly from their github. 所有JS,CSS和HTML均直接取自其github。 For some reason the JS isn't working and all elements are displayed on page load. 由于某种原因,JS无法正常工作,并且所有元素都在页面加载时显示。 I've tried putting the stripe script in multiple places, nothing seems to work, and no errors are being thrown in the console. 我尝试将带区脚本放置在多个位置,似乎没有任何效果,并且在控制台中未引发任何错误。

Any help would be MUCH appreciated! 任何帮助将非常感激!

 $(document).ready(function() { var stripe = Stripe('pk_test_6pRNASCoBOKtIshFeQd4XMUh'); function registerElements(elements, exampleName) { var formClass = '.' + exampleName; var example = document.querySelector(formClass); var form = example.querySelector('form'); var resetButton = example.querySelector('a.reset'); var error = form.querySelector('.error'); var errorMessage = error.querySelector('.message'); function enableInputs() { Array.prototype.forEach.call( form.querySelectorAll( "input[type='text'], input[type='email'], input[type='tel']" ), function(input) { input.removeAttribute('disabled'); } ); } function disableInputs() { Array.prototype.forEach.call( form.querySelectorAll( "input[type='text'], input[type='email'], input[type='tel']" ), function(input) { input.setAttribute('disabled', 'true'); } ); } function triggerBrowserValidation() { // The only way to trigger HTML5 form validation UI is to fake a user submit // event. var submit = document.createElement('input'); submit.type = 'submit'; submit.style.display = 'none'; form.appendChild(submit); submit.click(); submit.remove(); } // Listen for errors from each Element, and show error messages in the UI. var savedErrors = {}; elements.forEach(function(element, idx) { element.on('change', function(event) { if (event.error) { error.classList.add('visible'); savedErrors[idx] = event.error.message; errorMessage.innerText = event.error.message; } else { savedErrors[idx] = null; // Loop over the saved errors and find the first one, if any. var nextError = Object.keys(savedErrors) .sort() .reduce(function(maybeFoundError, key) { return maybeFoundError || savedErrors[key]; }, null); if (nextError) { // Now that they've fixed the current error, show another one. errorMessage.innerText = nextError; } else { // The user fixed the last error; no more errors. error.classList.remove('visible'); } } }); }); // Listen on the form's 'submit' handler... form.addEventListener('submit', function(e) { e.preventDefault(); // Trigger HTML5 validation UI on the form if any of the inputs fail // validation. var plainInputsValid = true; Array.prototype.forEach.call(form.querySelectorAll('input'), function( input ) { if (input.checkValidity && !input.checkValidity()) { plainInputsValid = false; return; } }); if (!plainInputsValid) { triggerBrowserValidation(); return; } // Show a loading screen... example.classList.add('submitting'); // Disable all inputs. disableInputs(); // Gather additional customer data we may have collected in our form. var name = form.querySelector('#' + exampleName + '-name'); var address1 = form.querySelector('#' + exampleName + '-address'); var city = form.querySelector('#' + exampleName + '-city'); var state = form.querySelector('#' + exampleName + '-state'); var zip = form.querySelector('#' + exampleName + '-zip'); var additionalData = { name: name ? name.value : undefined, address_line1: address1 ? address1.value : undefined, address_city: city ? city.value : undefined, address_state: state ? state.value : undefined, address_zip: zip ? zip.value : undefined, }; // Use Stripe.js to create a token. We only need to pass in one Element // from the Element group in order to create a token. We can also pass // in the additional customer data we collected in our form. stripe.createToken(elements[0], additionalData).then(function(result) { // Stop loading! example.classList.remove('submitting'); if (result.token) { // If we received a token, show the token ID. example.querySelector('.token').innerText = result.token.id; example.classList.add('submitted'); } else { // Otherwise, un-disable inputs. enableInputs(); } }); }); resetButton.addEventListener('click', function(e) { e.preventDefault(); // Resetting the form (instead of setting the value to `''` for each input) // helps us clear webkit autofill styles. form.reset(); // Clear each Element. elements.forEach(function(element) { element.clear(); }); // Reset error state as well. error.classList.remove('visible'); // Resetting the form does not un-disable inputs, so we need to do it separately: enableInputs(); example.classList.remove('submitted'); }); } var elements = stripe.elements({ fonts: [{ cssSrc: 'https://fonts.googleapis.com/css?family=Source+Code+Pro', }, ], // Stripe's examples are localized to specific languages, but if // you wish to have Elements automatically detect your user's locale, // use `locale: 'auto'` instead. locale: window.__exampleLocale }); // Floating labels var inputs = document.querySelectorAll('.cell.example.example2 .input'); Array.prototype.forEach.call(inputs, function(input) { input.addEventListener('focus', function() { input.classList.add('focused'); }); input.addEventListener('blur', function() { input.classList.remove('focused'); }); input.addEventListener('keyup', function() { if (input.value.length === 0) { input.classList.add('empty'); } else { input.classList.remove('empty'); } }); }); var elementStyles = { base: { color: '#32325D', fontWeight: 500, fontFamily: 'Source Code Pro, Consolas, Menlo, monospace', fontSize: '16px', fontSmoothing: 'antialiased', '::placeholder': { color: '#CFD7DF', }, ':-webkit-autofill': { color: '#e39f48', }, }, invalid: { color: '#E25950', '::placeholder': { color: '#FFCCA5', }, }, }; var elementClasses = { focus: 'focused', empty: 'empty', invalid: 'invalid', }; var cardNumber = elements.create('cardNumber', { style: elementStyles, classes: elementClasses, }); cardNumber.mount('#example2-card-number'); var cardExpiry = elements.create('cardExpiry', { style: elementStyles, classes: elementClasses, }); cardExpiry.mount('#example2-card-expiry'); var cardCvc = elements.create('cardCvc', { style: elementStyles, classes: elementClasses, }); cardCvc.mount('#example2-card-cvc'); registerElements([cardNumber, cardExpiry, cardCvc], 'example2'); }); 
 .example.example2 { background-color: #fff; } .example.example2 * { font-family: Source Code Pro, Consolas, Menlo, monospace; font-size: 16px; font-weight: 500; } .example.example2 .row { display: -ms-flexbox; display: flex; margin: 0 5px 10px; } .example.example2 .field { position: relative; width: 100%; height: 50px; margin: 0 10px; } .example.example2 .field.half-width { width: 50%; } .example.example2 .field.quarter-width { width: calc(25% - 10px); } .example.example2 .baseline { position: absolute; width: 100%; height: 1px; left: 0; bottom: 0; background-color: #cfd7df; transition: background-color 0.3s cubic-bezier(0.165, 0.84, 0.44, 1); } .example.example2 label { position: absolute; width: 100%; left: 0; bottom: 8px; color: #cfd7df; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; transform-origin: 0 50%; cursor: text; transition-property: color, transform; transition-duration: 0.3s; transition-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1); } .example.example2 .input { position: absolute; width: 100%; left: 0; bottom: 0; padding-bottom: 7px; color: #32325d; background-color: transparent; } .example.example2 .input::-webkit-input-placeholder { color: transparent; transition: color 0.3s cubic-bezier(0.165, 0.84, 0.44, 1); } .example.example2 .input::-moz-placeholder { color: transparent; transition: color 0.3s cubic-bezier(0.165, 0.84, 0.44, 1); } .example.example2 .input:-ms-input-placeholder { color: transparent; transition: color 0.3s cubic-bezier(0.165, 0.84, 0.44, 1); } .example.example2 .input.StripeElement { opacity: 0; transition: opacity 0.3s cubic-bezier(0.165, 0.84, 0.44, 1); will-change: opacity; } .example.example2 .input.focused, .example.example2 .input:not(.empty) { opacity: 1; } .example.example2 .input.focused::-webkit-input-placeholder, .example.example2 .input:not(.empty)::-webkit-input-placeholder { color: #cfd7df; } .example.example2 .input.focused::-moz-placeholder, .example.example2 .input:not(.empty)::-moz-placeholder { color: #cfd7df; } .example.example2 .input.focused:-ms-input-placeholder, .example.example2 .input:not(.empty):-ms-input-placeholder { color: #cfd7df; } .example.example2 .input.focused+label, .example.example2 .input:not(.empty)+label { color: #aab7c4; transform: scale(0.85) translateY(-25px); cursor: default; } .example.example2 .input.focused+label { color: #24b47e; } .example.example2 .input.invalid+label { color: #ffa27b; } .example.example2 .input.focused+label+.baseline { background-color: #24b47e; } .example.example2 .input.focused.invalid+label+.baseline { background-color: #e25950; } .example.example2 input, .example.example2 button { -webkit-appearance: none; -moz-appearance: none; appearance: none; outline: none; border-style: none; } .example.example2 input:-webkit-autofill { -webkit-text-fill-color: #e39f48; transition: background-color 100000000s; -webkit-animation: 1ms void-animation-out; } .example.example2 .StripeElement--webkit-autofill { background: transparent !important; } .example.example2 input, .example.example2 button { -webkit-animation: 1ms void-animation-out; } .example.example2 button { display: block; width: calc(100% - 30px); height: 40px; margin: 40px 15px 0; background-color: #24b47e; border-radius: 4px; color: #fff; text-transform: uppercase; font-weight: 600; cursor: pointer; } .example.example2 input:active { background-color: #159570; } .example.example2 .error svg { margin-top: 0 !important; } .example.example2 .error svg .base { fill: #e25950; } .example.example2 .error svg .glyph { fill: #fff; } .example.example2 .error .message { color: #e25950; } .example.example2 .success .icon .border { stroke: #abe9d2; } .example.example2 .success .icon .checkmark { stroke: #24b47e; } .example.example2 .success .title { color: #32325d; font-size: 16px !important; } .example.example2 .success .message { color: #8898aa; font-size: 13px !important; } .example.example2 .success .reset path { fill: #24b47e; } 
 <h1>stripe form</h1> <!--Example 2--> <div class="cell example example2"> <form> <div data-locale-reversible> <div class="row"> <div class="field"> <input id="example2-address" data-tid="elements_examples.form.address_placeholder" class="input empty" type="text" placeholder="185 Berry St" required="" autocomplete="address-line1"> <label for="example2-address" data-tid="elements_examples.form.address_label">Address</label> <div class="baseline"></div> </div> </div> <div class="row" data-locale-reversible> <div class="field half-width"> <input id="example2-city" data-tid="elements_examples.form.city_placeholder" class="input empty" type="text" placeholder="San Francisco" required="" autocomplete="address-level2"> <label for="example2-city" data-tid="elements_examples.form.city_label">City</label> <div class="baseline"></div> </div> <div class="field quarter-width"> <input id="example2-state" data-tid="elements_examples.form.state_placeholder" class="input empty" type="text" placeholder="CA" required="" autocomplete="address-level1"> <label for="example2-state" data-tid="elements_examples.form.state_label">State</label> <div class="baseline"></div> </div> <div class="field quarter-width"> <input id="example2-zip" data-tid="elements_examples.form.postal_code_placeholder" class="input empty" type="text" placeholder="94107" required="" autocomplete="postal-code"> <label for="example2-zip" data-tid="elements_examples.form.postal_code_label">ZIP</label> <div class="baseline"></div> </div> </div> </div> <div class="row"> <div class="field"> <div id="example2-card-number" class="input empty"></div> <label for="example2-card-number" data-tid="elements_examples.form.card_number_label">Card number</label> <div class="baseline"></div> </div> </div> <div class="row"> <div class="field half-width"> <div id="example2-card-expiry" class="input empty"></div> <label for="example2-card-expiry" data-tid="elements_examples.form.card_expiry_label">Expiration</label> <div class="baseline"></div> </div> <div class="field half-width"> <div id="example2-card-cvc" class="input empty"></div> <label for="example2-card-cvc" data-tid="elements_examples.form.card_cvc_label">CVC</label> <div class="baseline"></div> </div> </div> <button type="submit" data-tid="elements_examples.form.pay_button">Pay $25</button> <div class="error" role="alert"><svg xmlns="http://www.w3.org/2000/svg" width="17" height="17" viewBox="0 0 17 17"> <path class="base" fill="#000" d="M8.5,17 C3.80557963,17 0,13.1944204 0,8.5 C0,3.80557963 3.80557963,0 8.5,0 C13.1944204,0 17,3.80557963 17,8.5 C17,13.1944204 13.1944204,17 8.5,17 Z"></path> <path class="glyph" fill="#FFF" d="M8.5,7.29791847 L6.12604076,4.92395924 C5.79409512,4.59201359 5.25590488,4.59201359 4.92395924,4.92395924 C4.59201359,5.25590488 4.59201359,5.79409512 4.92395924,6.12604076 L7.29791847,8.5 L4.92395924,10.8739592 C4.59201359,11.2059049 4.59201359,11.7440951 4.92395924,12.0760408 C5.25590488,12.4079864 5.79409512,12.4079864 6.12604076,12.0760408 L8.5,9.70208153 L10.8739592,12.0760408 C11.2059049,12.4079864 11.7440951,12.4079864 12.0760408,12.0760408 C12.4079864,11.7440951 12.4079864,11.2059049 12.0760408,10.8739592 L9.70208153,8.5 L12.0760408,6.12604076 C12.4079864,5.79409512 12.4079864,5.25590488 12.0760408,4.92395924 C11.7440951,4.59201359 11.2059049,4.59201359 10.8739592,4.92395924 L8.5,7.29791847 L8.5,7.29791847 Z"></path> </svg> <span class="message"></span></div> </form> <div class="success"> <div class="icon"> <svg width="84px" height="84px" viewBox="0 0 84 84" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <circle class="border" cx="42" cy="42" r="40" stroke-linecap="round" stroke-width="4" stroke="#000" fill="none"></circle> <path class="checkmark" stroke-linecap="round" stroke-linejoin="round" d="M23.375 42.5488281 36.8840688 56.0578969 64.891932 28.0500338" stroke-width="4" stroke="#000" fill="none"></path> </svg> </div> <h3 class="title" data-tid="elements_examples.success.title">Payment successful</h3> <p class="message"><span data-tid="elements_examples.success.message">Thanks for trying Stripe Elements. No money was charged, but we generated a token: </span><span class="token">tok_189gMN2eZvKYlo2CwTBv9KKh</span></p> <a class="reset" href="#"> <svg width="32px" height="32px" viewBox="0 0 32 32" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <path fill="#000000" d="M15,7.05492878 C10.5000495,7.55237307 7,11.3674463 7,16 C7,20.9705627 11.0294373,25 16,25 C20.9705627,25 25,20.9705627 25,16 C25,15.3627484 24.4834055,14.8461538 23.8461538,14.8461538 C23.2089022,14.8461538 22.6923077,15.3627484 22.6923077,16 C22.6923077,19.6960595 19.6960595,22.6923077 16,22.6923077 C12.3039405,22.6923077 9.30769231,19.6960595 9.30769231,16 C9.30769231,12.3039405 12.3039405,9.30769231 16,9.30769231 L16,12.0841673 C16,12.1800431 16.0275652,12.2738974 16.0794108,12.354546 C16.2287368,12.5868311 16.5380938,12.6540826 16.7703788,12.5047565 L22.3457501,8.92058924 L22.3457501,8.92058924 C22.4060014,8.88185624 22.4572275,8.83063012 22.4959605,8.7703788 C22.6452866,8.53809377 22.5780351,8.22873685 22.3457501,8.07941076 L22.3457501,8.07941076 L16.7703788,4.49524351 C16.6897301,4.44339794 16.5958758,4.41583275 16.5,4.41583275 C16.2238576,4.41583275 16,4.63969037 16,4.91583275 L16,7 L15,7 L15,7.05492878 Z M16,32 C7.163444,32 0,24.836556 0,16 C0,7.163444 7.163444,0 16,0 C24.836556,0 32,7.163444 32,16 C32,24.836556 24.836556,32 16,32 Z"></path> </svg> </a> </div> <div class="caption"> <span data-tid="elements_examples.caption.no_charge" class="no-charge">Your card won't be charged</span> <a class="source" href="https://github.com/stripe/elements-examples/#example-2"> <svg width="16px" height="10px" viewBox="0 0 16 10" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <path d="M1,8 L12,8 C12.5522847,8 13,8.44771525 13,9 C13,9.55228475 12.5522847,10 12,10 L1,10 C0.44771525,10 6.76353751e-17,9.55228475 0,9 C-6.76353751e-17,8.44771525 0.44771525,8 1,8 L1,8 Z M1,4 L8,4 C8.55228475,4 9,4.44771525 9,5 C9,5.55228475 8.55228475,6 8,6 L1,6 C0.44771525,6 6.76353751e-17,5.55228475 0,5 C-6.76353751e-17,4.44771525 0.44771525,4 1,4 L1,4 Z M1,0 L15,0 C15.5522847,-1.01453063e-16 16,0.44771525 16,1 L16,1 C16,1.55228475 15.5522847,2 15,2 L1,2 C0.44771525,2 6.76353751e-17,1.55228475 0,1 L0,1 L0,1 C-6.76353751e-17,0.44771525 0.44771525,1.01453063e-16 1,0 L1,0 Z" fill="#AAB7C4"></path> </svg> <span data-tid="elements_examples.caption.view_source">View source on GitHub</span> </a> </div> </div> <script src="https://js.stripe.com/v3/"></script> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"></script> 

I think you experienced a similar issue with me. 我想您也遇到了类似的问题。 The doc fails to point out that you need the JS in index.js 该文档未能指出您需要index.js中的JS

Check this: https://github.com/stripe/elements-examples/blob/master/js/index.js 检查一下: https : //github.com/stripe/elements-examples/blob/master/js/index.js

Especially the part that has createToken() 特别是具有createToken()的部分

It looks like you are missing the base css classes from https://github.com/stripe/elements-examples/blob/master/css/base.css 看起来您似乎缺少https://github.com/stripe/elements-examples/blob/master/css/base.css中的基本CSS类

Note that base.css uses the CSS selector main > in a number of places, so you will either need to edit that out or add <main> and </main> tags in your HTML around the entire Stripe part. 请注意, base.css在许多地方base.css使用CSS选择器main > ,因此您将需要对其进行编辑,或者在整个Stripe部分的HTML中添加<main></main>标签。 You can find the same tag ( <main> ) in Stripe's github html 您可以在Stripe的github html中找到相同的标签( <main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM