【问题标题】:Custom Stripe Button does not trigger the POST method自定义条纹按钮不会触发 POST 方法
【发布时间】:2015-06-27 02:03:19
【问题描述】:

我目前正在实施 Stripe,但遇到了一些问题。

简单的集成效果很好,这个:

<form action="step4" method="POST">
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_mA3Wo4wGNDNEQ5rRHaUKTVOZ"
        data-image="/img/documentation/checkout/marketplace.png"
        data-name="Demo Site"
        data-description="2 widgets"
        data-currency="eur"
        data-amount="2000">
    </script>
</form>

但是当我想使用来自here 的自定义集成时,不会调用指定的方法。

目前我只是在我的 POST 方法上使用了一个闭包,它会死掉并转储输入。在简单的集成中,我取回了令牌。在自定义集成中我没有。这是闭包(Laravel 5):

Route::post('step4', function() {
    dd(Input::all());
});

这是我的自定义集成代码:

<form action="step4" method="POST">
    <button id="stripeButton" class="btn btn-lg btn-block btn-primary">Pay with Stripe</button>
</form>

    <script src="https://checkout.stripe.com/checkout.js"></script>

    <script>
        var handler = StripeCheckout.configure({
            key: "{{$stripe_public_key}}",
            image: "{{$data_image}}",
            token: function(token) {
              // Use the token to create the charge with a server-side script.
              // You can access the token ID with `token.id`
            }
        });

        $('#stripeButton').on('click', function(e) {
        // Open Checkout with further options
        handler.open({
            name: "{{$data_name}}",
            description: "{{$data_description}}",
            currency: "USD",
            amount: "{{$data_amount}}",
            @if(Session::has('email'))
                email: "{{$email}}",
            @endif
            allowRememberMe: false,
        });
            e.preventDefault();
        });

        // Close Checkout on page navigation
        $(window).on('popstate', function() {
            handler.close();
        });
    </script>

但该方法似乎没有被调用,因为我没有得到输入的 die&dump。我没有收到错误消息,似乎返回了条带令牌,而没有调用该方法。

自定义条带集成中如何调用方法?

【问题讨论】:

    标签: php html laravel stripe-payments


    【解决方案1】:

    您的表单没有“提交”按钮,#stripeButton 也没有。

    如果您的表单没有提交,那么您的方法将永远不会被调用。

    如果您想调用 routes.php 文件中的方法,请在 form 中添加 &lt;input type="submit" name="Submit Stripe" /&gt;

    【讨论】:

      【解决方案2】:

      答案是,我的处理程序需要附加以下输入表单:

              var handler = StripeCheckout.configure({
                  key: "{{$stripe_public_key}}",
                  image: "{{$data_image}}",
                  // to this when a token is generated
                  token: function(token) {
                      // Use the token to create the charge with a server-side script.
                      // You can access the token ID with `token.id`
                      $form.append($('<input type="hidden" name="stripeToken" />').val(token.id));
                      $form.append($('<input type="hidden" name="stripeEmail" />').val(token.email));
      
                      // submit the form, uncomment to make this happen
                      $form.get(0).submit();
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 2023-04-02
        • 1970-01-01
        • 2014-04-28
        • 1970-01-01
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        • 2017-07-03
        • 2017-01-14
        相关资源
        最近更新 更多