带Stripe JS v3的Vue组件

用户名

我将Vue组件用于结帐表格。

标头部分中包含条纹js(v3)文件。

表格在组件中

该组件有两个部分。一种是从用户那里获得付款详细信息,另一种是提交卡详细信息。

<template>

    <div class="payment_form">
       <div id="payment_details" v-if="showPaymentDetails">
        <!-- User input goes here. Like username phone email -->
       </div>
        <div id="stripe-form" v-if="showStripeForm">
            <form action="/charge" method="post" id="payment-form" @submit.prevent="createStripeToken()">
               <div class="form-row">
                    <label for="card-element">
                        Credit or debit card
                    </label>
                    <div id="card-element">
                        <!-- a Stripe Element will be inserted here. -->
                    </div>

                    <!-- Used to display Element errors -->
                    <div id="card-errors" role="alert"></div>
                </div>

                <button>Submit Payment</button>
            </form>
        </div>
    </div>

</template>
<script>
     import { Validator } from 'vee-validate';
        export default {

        data() {
            return {
                stripeToken: '',
                showPaymentDetails: true,
                showStripeForm: true,
            }
        },
        created() {
        },
        methods: {
            validateForm() {
                self = this;
                this.$validator.validateAll().then(result => {
                    if (result) {
                        // eslint-disable-next-line
                        alert('From Submitted!');
                        console.log(this.$data);
                        axios.post('/data',{
                            name:this.name,
                        })
                        .then(function (response) {
                            self.showStripeForm = true;
                            console.log(response);
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                        return;
                    }

                });
            },

            createStripeToken(){
                var form = document.getElementById('payment-form');
                form.addEventListener('submit', function(event) {
                    event.preventDefault();

                    window.stripe.createToken(card).then(function(result) {
                        if (result.error) {
                            // Inform the user if there was an error
                            var errorElement = document.getElementById('card-errors');
                            errorElement.textContent = result.error.message;
                        } else {
                            // Send the token to your server
                            console.log(result.token);
                        }
                    });
                });
            },
            initStripe(){
                window.stripe = Stripe('stripe_test_key_here');
                var elements = stripe.elements();
                var style = {
                    base: {
                        // Add your base input styles here. For example:
                        fontSize: '16px',
                        lineHeight: '24px'
                    }
                };

                // Create an instance of the card Element
                window.card = elements.create('card', {style: style});

                // Add an instance of the card Element into the `card-element` <div>
                window.card.mount('#card-element');

            }

        },
        mounted() {
            this.initStripe();
            setTimeout(function () {

                this.showStripeForm = false;
            },2000);
        }
    }
</script>

我尝试在页面加载时加载条形窗体,并尝试通过showStripeForm禁用元素。

但是vue取消了从条带服务器加载的条带卡表单的设置,并将dom保存到其原始状态。

所以我无法在axios回调上触发条纹形式。

我不想使用用户带区检出和带区js v1(此版本后不建议使用您自己的表单获取输入)。

伊克贝尔

在中mountedsetTimeout回调更改为箭头函数,否则this将指向Window而不是Vue

mounted() {
  setTimeout(() => {
     this.showStripeForm = false
  }, 2000)
}

另外,访问DOM的方式也不是Vue式的。您可以ref在要在代码中使用的DOM元素上使用。例如:

<form action="/charge" method="post" ref="payment-form" @submit.prevent="createStripeToken()">

然后$refs像这样访问它

var form = this.$refs['payment-form']
/* 
  Same result as document.getElementById('payment-form')
  but without using an id attribute.
*/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章