简体   繁体   English

如何实现Stripe Checkout自定义按钮

[英]How to implement a Stripe Checkout custom button

According to the documentation, Checkout supports two different integrations: Simple and Custom. 根据文档,Checkout支持两种不同的集成:简单和自定义。

The simple way works for me: 简单的方法对我有用:

**<form action="create_subscription.php" method="POST">**
<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-key="asdsdfasd3232"
  data-amount="2000"
  data-name=""
  data-description="2 widgets"
  data-image="https://s3.amazonaws.com/stripe-uploads/acct_19EnQrGHC6pu6Qvdmerchant-icon-1485553962843-logo_stripe.png"
  data-locale="auto">
</script>
</form>

However in the custom way I don't understand how and where should I call the "create_subscription.php" script. 但是在自定义方式中,我不明白应该如何以及在何处调用“create_subscription.php”脚本。 This is the custom integration code: 这是自定义集成代码:

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

<button id="customButton">Purchase</button>

<script>
var handler = StripeCheckout.configure({
  key: 'asdsdfasd3232',
  image: 'https://s3.amazonaws.com/stripe-uploads/acct_19EnQrGHC6pu6Qvdmerchant-icon-1485553962843-logo_stripe.png',
  locale: 'auto',
  token: function(token) {
    // You can access the token ID with `token.id`.
    // Get the token ID to your server-side code for use.
  }
});

document.getElementById('customButton').addEventListener('click', function(e) {
  // Open Checkout with further options:
  handler.open({
    name: '',
    description: '2 widgets',
    amount: 2000
  });
  e.preventDefault();
});

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

I've tried this code, but it is not working. 我试过这段代码,但它不起作用。 Can someone point me in the right direction? 有人能指出我正确的方向吗?

<form action="/create_subscription.php" method="POST">
      <script src="https://checkout.stripe.com/checkout.js"></script>
      <div id="stripe-demo" class="evo-button rounded cele">
      <span>Register</span>
      </div>

      <script>
      var handler = StripeCheckout.configure({
        key: "asdsdfasd3232",
        image: "img/logo.png",
        name: "",
        description: "Subscription for 1 month",
        panelLabel: "Sign Me Up!",
        amount: "2000",
        allowRememberMe: false
      });

      document.getElementById('stripe-demo').addEventListener('click', function(e) {
        handler.open();
        e.preventDefault();
      });

      window.addEventListener('popstate', function() {
        handler.close();
      });
      </script>
    </form>

Thanks to Comdenz This is the way i solve it using existing form and calling my server side code. 感谢Comdenz这是我使用现有表单解决它并调用我的服务器端代码的方式。

<script src="https://checkout.stripe.com/checkout.js"></script>
<script>
var handler = StripeCheckout.configure({
 key: "your testing key",
  locale: 'auto',
  image: "image/directory",
  name: "Name",
  description: "your discription",
  panelLabel: "Click to make payment",
  allowRememberMe: false,


  token: function(token) {
    // Prevent user from leaving page
    window.onbeforeunload = function() {
            return "";
        }

    // Dynamically create a form element to submit the results
    // to your backend server
    var form = document.getElementById("payment-form");
    form.setAttribute('method', "POST");
    form.setAttribute('action', "//localhost/dubb/charge.php");

    // Add the token ID as a hidden field to the form payment-form
    var inputToken = document.createElement("input");
    inputToken.setAttribute('type', "hidden");
    inputToken.setAttribute('name', "stripeToken");
    inputToken.setAttribute('value', token.id);
    form.appendChild(inputToken);

    // Add the email as a hidden field to the form
    var inputEmail = document.createElement("input");
    inputEmail.setAttribute('type', "hidden");
    inputEmail.setAttribute('name', "stripeEmail");
    inputEmail.setAttribute('value', token.email);
    form.appendChild(inputEmail);

        // Finally, submit the form
    document.body.appendChild(form);

    // Artificial 5 second delay for testing
    setTimeout(function() {
        window.onbeforeunload = null;
        document.forms["payment-form"].submit()
    }, 5000);
  }

});

document.getElementById('stripe-demo').addEventListener('click', function(e) {
  handler.open();
  e.preventDefault();
});

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

With this, you done need to make new form, just call your existing form using the javascript 有了这个,您需要创建新表单,只需使用javascript调用现有表单

In the token callback function, you'd need to do whatever is necessary to submit the token to your backend. token回调函数中,您需要执行将令牌提交到后端所需的任何操作。

Typically, this is done by having a form element with a hidden element, and from the callback you'd set the value of the hidden element to the token ID and submit the form. 通常,这是通过使表单元素具有隐藏元素来完成的,并且从回调中您将隐藏元素的值设置为令牌ID并提交表单。

You could also dynamically create the form from scratch, or fire an AJAX request, or any other method that is appropriate for your specific needs. 您还可以从头开始动态创建表单,或者激发AJAX请求或任何其他适合您特定需求的方法。

Here is an example of a custom integration that uses an existing form and sets the value of hidden elements from the callback: https://jsfiddle.net/ywain/g2ufa8xr/ 下面是一个自定义集成的示例,它使用现有表单并设置回调中隐藏元素的值: https//jsfiddle.net/ywain/g2ufa8xr/

Checkout the post on Stripe custom checkout not Posting you have the same issue as me. 查看条纹自定义结帐时的帖子不发布您遇到与我相同的问题。 Hope this helps you out 希望这可以帮助你

I'm just through with all that. 我只是完成了这一切。 So I've found some hints at https://jsfiddle.net/user/ywain/fiddles/1/ . 所以我在https://jsfiddle.net/user/ywain/fiddles/1/上找到了一些提示。 Here you should look at the https://jsfiddle.net/ywain/9zscyyhg/ . 在这里你应该看看https://jsfiddle.net/ywain/9zscyyhg/ At summery you have to get the stripe token at token: function(token) { // You can access the token ID with token.id . 在夏天,您必须在令牌处获取条带令牌:function(token){//您可以使用token.id访问令牌ID。 // Get the token ID to your server-side code for use. //获取服务器端代码的令牌ID以供使用。

The submit of the Purchase only triggers the Stripe modal payment form. 提交仅提供条带模式付款表单。 So from the form there has to be a way back to the site. 所以从形式上必须有回到网站的方式。 This has to be done with addintional javascript code. 这必须通过附加的javascript代码完成。

Hope this one help you. 希望这个能帮助你。 You can refer here https://gist.github.com/ziadoz/5101836 你可以在这里参考https://gist.github.com/ziadoz/5101836

    <input 
        type="submit" 
        value="Pay with Card"
        data-key="PUBLISHABLE STRIPE KEY"
        data-amount="500"
        data-currency="cad"
        data-name="Example Company Inc"
        data-description="Stripe payment for $5"
    />

    <script src="https://checkout.stripe.com/v2/checkout.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <script>
    $(document).ready(function() {
        $(':submit').on('click', function(event) {
            event.preventDefault();
            var $button = $(this),
                $form = $button.parents('form');
            var opts = $.extend({}, $button.data(), {
                token: function(result) {
                    $form.append($('<input>').attr({ type: 'hidden', name: 'stripeToken', value: result.id })).submit();
                }
            });
            StripeCheckout.open(opts);
        });
    });
    </script>

` `

When the form is submitted the content is POSTed to /create_subscription.php . 提交表单时,内容将POST到/create_subscription.php Can we see the contents of /create_subscription.php? 我们能看到/create_subscription.php的内容吗? What is the response when submitting the form, do we get an error return or a status code 200? 提交表单时的响应是什么,我们是否收到错误返回或状态代码200? Are you able to access the php / web server logs? 你能访问php / web服务器日志吗? Any output? 任何输出?

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

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