简体   繁体   English

从令牌发行创建Stripe源

[英]Create Stripe source from token issue

I have a Javascript file of which I am trying to use, to create a Stripe source from a Stripe token. 我有一个Javascript文件,试图从Stripe令牌创建Stripe源。 To then be able to submit that source to my form, submit the form and then create a charge from the .php file of which the form submits to. 为了能够将该源提交到我的表单,请提交表单,然后从提交表单的.php文件中创建费用。

I am just having some difficulty doing this, I am not getting any errors in my console and it creates the token and source as supposed. 我在执行此操作时遇到了一些困难,我的控制台没有出现任何错误,它按预期方式创建了令牌和源。 However the data is never submitted to my form, as the page never redirects and no charge is ever made. 但是,数据永远不会提交到我的表单,因为页面不会重定向,也不会产生任何费用。

I am unsure of what exactly I am missing to make it work properly. 我不确定要使它正常工作到底缺少了什么。

Below is an attached image of the console output. 下面是控制台输出的附件图像。

在此处输入图片说明

The code I have so far is the one seen below. 到目前为止,我拥有的代码如下所示。

    Stripe.card.createToken({
            number: document.getElementById('cardNumId').value,
            cvc: document.getElementById('cvcID').value,
            exp_month: document.getElementById('expMonthId').value,
            exp_year: document.getElementById('expYearId').value
    }, stripeResponseHandler);

    var stripeResponseHandler = function(status, response) {

    if (response.error) {

        alert('Something went wrong, sorry!' + error);

    } else {

    var token = response.id;

    form.addEventListener('submit', function(event) {
        event.preventDefault();
    stripe.createSource(source, token).then(function(result) {
    if (result.error) {
      // Inform the user if there was an error
      alert('Something went wrong, sorry!' + error);
    } else {
      // Send the source to your server
      stripeSourceHandler(result.source);

    }
  });
  });

    function stripeSourceHandler(source) {

        // Insert the source ID into the form so it gets submitted to the server
        var form = document.getElementById('payment-form');
        var hiddenInput = document.createElement('input');
        hiddenInput.setAttribute('type', 'hidden');
        hiddenInput.setAttribute('name', 'stripeSource');
        hiddenInput.setAttribute('value', source.id);
        form.appendChild(hiddenInput);

        // Submit the form
        form.submit();

            }
        }
    };

我认为您应该在Stripe文档中阅读: https : //stripe.com/docs/stripe-js/elements/quickstart

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

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