简体   繁体   English

从表格中预先在Stripe Checkout中填充电子邮件

[英]Pre populate email in Stripe Checkout from form

I have a form and one of the form fields is as follows: 我有一个表单,表单字段之一如下:

<input type="email" id="email" name="email" required>

I have a submit button at the bottom of the form that calls javascript and loads the Stripe Checkout. 我在表单底部有一个提交按钮,它调用javascript并加载Stripe Checkout。

<input type="button" id="charge-button" class="button radius member_button" value="Continue">

Javascript to call Stripe. 调用Stripe的Javascript。

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

<script>
  var handler = StripeCheckout.configure({
    key: '<?php echo $stripe['publishable_key']; ?>',
    image: '/img/documentation/checkout/marketplace.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.
    $.post("", {token: token, type: 'charge'}, function(res){
        if(res.status){
            $('form').submit();
        }
    },"json");
      console.log(token);
    }
  });

$('#charge-button').on('click', function(e) {
    handler.open({
      image: '/logo.png',
      name: 'Shop.com',
      description: 'Product',
      email: 'example@email.com',
      currency: 'gbp',
      amount: 2000
    });
    e.preventDefault();
  });
 $(window).on('popstate', function() {
    handler.close();
  }); 

</script>

I want to be able to grab the value from the form field and pass it to where I currently have example@email.com. 我希望能够从表单字段中获取值并将其传递到我目前拥有example@email.com的位置。

I am a coding newbie so appreciate your patience. 我是编码新手,非常感谢您的耐心配合。

Many thanks, 非常感谢,

John 约翰

You can do this by storing email address when user submit form. 您可以通过在用户提交表单时存储电子邮件地址来做到这一点。 So your code will look like this - 因此您的代码将如下所示-

 <input type="email" id="email" name="email" required>

    $('#charge-button').on('click', function(e) {

       var userEmail = $('#email').val();

        handler.open({
          image: '/logo.png',
          name: 'Shop.com',
          description: 'Product',
          email: userEmail,
          currency: 'gbp',
          amount: 2000
        });
        e.preventDefault();
      });
     $(window).on('popstate', function() {
        handler.close();
      });

I did it in this way 我是这样做的

<form action="YOUR_URL" method="POST">
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_123eqweqweqweqwe12g"
        data-amount="AMOUNT_YOU_EXPECTED_TO_CHARGE"
        data-email="ECHO_EMAIL_ADDESS_HERE"
        data-name="Hey Title"
        data-description="DESCRIPTION_FOR_FORM"
        data-image="/profilePics/default.jpg"
        data-locale="auto">
    </script>
</form>

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

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