简体   繁体   English

使用Stripe引导网站付款

[英]Bootstrap website payments with Stripe

I'm completely new to using Stripe for payments, and as it's a Bootstrap site & I'm using Stripe.js v2. 我对使用Stripe进行付款完全陌生,因为它是一个Bootstrap网站,而且我正在使用Stripe.js v2。

From my understanding of how Stripe works, my HTML form needs to initially communicate with Stripe with the credit card num, cvc & expirary using Javascript, which will return a token (or an error) - and I then submit this token, and other payment information like the amount etc.. to the PHP script on my Server (which then sends this Stripe). 根据我对Stripe工作原理的了解,我的HTML表单首先需要使用Javascript与信用卡号,信用卡号和到期日与Stripe通信,这将返回令牌(或错误)-然后我提交此令牌和其他付款信息(例如金额等)到我服务器上的PHP脚本(然后发送此Stripe)。

My problem is, my JavaScript is never executed first - and instead my page tries to run submit.php first. 我的问题是,我的JavaScript永远不会首先执行-相反,我的页面会首先尝试运行submit.php

What should I do to correct this - and have my JavaScript create the token, and then have the token passed to my submit.php code? 我该怎么做才能更正此问题-让我的JavaScript创建令牌,然后将令牌传递给我的Submit.php代码?

*Note - my HTML form does contain more than what's listed here (such as asking the user for Name, Address, State, Phone, Amount etc), but i shortened it, so it was easier to read. *注意-我的HTML表单确实包含的内容超出了此处列出的内容(例如询问用户的姓名,地址,州,电话,金额等),但我将其缩短了,因此更易于阅读。

HTML Code: HTML代码:

<form action="/PHP/submit.php" method="POST" class="contact-form" id="payment-form">
<div id="creditcard">
  <span class="payment-errors"></span>
    <div class="form-group has-feedback row">
      <label for="cardnumber" class="col-sm-2 form-control-sm">Card Number:</label>
        <div class="col-sm-5">
                        <!--<input type="text" autocomplete="off" class="form-control form-control-sm card-number" value="" pattern="[0-9]{10}" data-stripe="number">-->
                        <input type="text" autocomplete="off" class="form-control form-control-sm card-number" data-stripe="number">
                      </div>
                      <label for="cvc" class="col-sm-1 form-control-sm">CVC:</label>
                      <div class="col-sm-4">
                        <!--<input type="text" autocomplete="off" class="form-control form-control-sm card-cvc" maxlength="3" value="" pattern="[0-9]{3}" data-stripe="cvc">-->
                        <input type="text" autocomplete="off" class="form-control form-control-sm card-cvc" data-stripe="cvc">
                      </div>
                    </div>
                    <div class="form-group has-feedback row">
                        <label for="expiration" class="col-sm-2 form-control-sm">Expiration Date </label>
                        <div class="col-sm-2">
                          <select class="card-expiry-month form-control form-control-sm" data-stripe="exp-month">
                            <option value="01" selected>01</option>
                            <option value="02">02</option>
                            <option value="03">03</option>
                            <option value="04">04</option>
                            <option value="05">05</option>
                            <option value="06">06</option>
                            <option value="07">07</option>
                            <option value="08">08</option>
                            <option value="09">09</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                          </select>
                        </div>
                        <div class="col-sm-2">
                          <select class="card-expiry-year form-control form-control-sm" data-stripe="exp-year">
                            <option value="2018" selected>2018</option>
                            <option value="2019">2019</option>
                            <option value="2020">2020</option>
                            <option value="2021">2021</option>
                            <option value="2022">2022</option>
                            <option value="2023">2023</option>
                            <option value="2024">2024</option>
                            <option value="2025">2025</option>
                        </select>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label for="cardname" class="col-sm-2 form-control-sm">Name on Card:</label>
                      <div class="col-sm-10">
                        <input type="text" class="form-control form-control-sm" autocomplete="off" name="cardname" id="cardname">
                      </div>
                    </div>
                    <div class="form-row form-submit">
                        <button type="submit" class="btn btn-default submit-button">Submit Donation</button>
                    </div>
                  </div>
                </form> 

And my Javascript: 而我的Javascript:

<script src="https://js.stripe.com/v2/"></script>
    <script>
      (function() {
        Stripe.setPublishableKey('pk_test_xxxxx');
      })();
      </script>
    <script>
    $(document).ready(function() {
    $('#payment-form').on('submit', generateToken);

    var generateToken = function(e) {
      var form = $(this);

      //No pressing the buy now button more than Once
      form.find('button').prop('disabled', true);

      //Create the token, based on the form object
      Stripe.create(form, stripeResponseHandler);

      //Prevent the form from submitting
      e.preventDefault();
    });
  });

    var stripeResponseHandler = function(status, response) {
      var form = $('#payment-form');

      //Any validation errors?
      if (response.error) {
        form.find('.payment-errors').text(response.error.message);
        alert(result.error.message);
        //Make the submit button clickable again
        form.find('button').prop('disabled', false);
      } else {
        //Otherwise, we're good to go! Submit the form.
        //Insert the unique token into the form
        $('<input>', {
          'type': 'hidden',
          'name': 'stripeToken',
          'value': response.id
        }).appendTo(form);
        alert(result.token.id);
        //Call tge native submit method on the form
        //to keep the submission from being cancelled
        form.get(0).submit();
      }
    };
    </script>

You should define the generateToken function before the $('#payment-form').on('submit', generateToken); 您应该在$('#payment-form').on('submit', generateToken);之前定义generateToken函数$('#payment-form').on('submit', generateToken); . Otherwise the submit event has no handler, and e.preventDefault(); 否则, submit事件没有处理程序,并且e.preventDefault(); is never reached. 永远不会到达。

$(document).ready(function() {
    $('#payment-form').on('submit', generateToken);

    var generateToken = function(e) {
      var form = $(this);

      //No pressing the buy now button more than Once
      form.find('button').prop('disabled', true);

      //Create the token, based on the form object
      Stripe.create(form, stripeResponseHandler);

      //Prevent the form from submitting
      e.preventDefault();
    });
});

Demo: https://www.codeply.com/go/wRcqjxfVmf 演示: https//www.codeply.com/go/wRcqjxfVmf

I ended up going a slightly different direction, using an 'onsubmit' event on the form, to trigger the javascript before the PHP; 最后,我在表单上使用了“ onsubmit”事件来触发一个稍微不同的方向,以便在PHP之前触发javascript。

<form action="/PHP/submit.php" method="POST" class="contact-form" id="payment-form" onsubmit="return onSubmitDo()">

I also completely changed the Javascript so it looked like this: 我还完全更改了Javascript,因此它看起来像这样:

Stripe.setPublishableKey('pk_test_******');

function onSubmitDo () {

  Stripe.card.createToken( document.getElementById('payment-form'), myStripeResponseHandler );

  return false;

};
function myStripeResponseHandler ( status, response ) {

  console.log( status );
  console.log( response );

  if ( response.error ) {
    document.getElementById('payment-error').innerHTML = response.error.message;
  } else {
    var tokenInput = document.createElement("input");
    tokenInput.type = "hidden";
    tokenInput.name = "stripeToken";
    tokenInput.value = response.id;
    var paymentForm = document.getElementById('payment-form');
    paymentForm.appendChild(tokenInput);
    paymentForm.submit();
  }
     };

The actual javascript code I used here, i found on this github account which has some Stripe payment samples; 我在这里使用的实际javascript代码是在这个github帐户上找到的,该帐户上有一些Stripe付款示例; https://github.com/wsmoak/stripe/blob/master/php/test-custom-form.html https://github.com/wsmoak/stripe/blob/master/php/test-custom-form.html

Now the form just needs to integrate jquery.payment (to format & validate card details), and it should all be complete. 现在,该表格仅需要集成jquery.payment(以格式化和验证卡详细信息),并且都应该完整。 https://github.com/stripe/jquery.payment https://github.com/stripe/jquery.payment

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

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