简体   繁体   中英

Stripe custom checkout not Posting

Can anyone assist as to why this is not posting to booking/charge upon completion of input to the checkout pop up window?

The simple checkout example posts fine, I am new to js so I don't quite understand the flow of the commands.

<form action="/booking/charge" method="post">
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Purchase</button>
 <script>
var handler = StripeCheckout.configure({
  key: 'pk_test_xxxxxxxxxxx',
  image: 'https://stripe.com/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.
  }
});

document.getElementById('customButton').addEventListener('click', function(e) {
  // Open Checkout with further options:
  handler.open({
    name: 'xxxx',
    email: "test@test.com",
    description: '2 widgets',
    currency: 'gbp',
    amount: 350
  });
  e.preventDefault();
});

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

If you use the custom Checkout integration, you need to do a little more work. You write your own code to handle the token returned by Stripe. This is all done within token callback.

Here's a traditional form submission example, it uses JQuery, appends the token and user's email as values to hidden form elements, then submits the form.

function (token) {
  // Use the token to create the charge with a server-side script.
  $("#stripeToken").val(token.id);
  $("#stripeEmail").val(token.email);
  $("#myForm").submit();
}

Full Example: https://jsfiddle.net/osrLsc8m/

Alternatively, you could submit the data to your backend with an AJAX request.

function (token) {        
  var myData = {
   token: token.id,
   email: token.email
   };

   /* 
    Make an AJAX post request using JQuery,
    change the first parameter to your charge script
   */
   $.post("/echo/html/",myData,function(data){ ... });
}

Full Example: http://jsfiddle.net/742tety5/

Code that has worked for me (must include script for jQuery in header not footer)

<script src="https://checkout.stripe.com/checkout.js"></script>
<form id="myForm">
    <input type="hidden" id="message" value="Hello, world"/></p>
    <input type="hidden" id="amount" value="10.00" /></p>
   <p><button type="submit" id="customButton">Pay</button></p>
</form>
<script> 
// checkout handler
var handler = StripeCheckout.configure({
    key: '<YOUR PUBLIC KEY>',
    image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
    token: function(token) {
        /* Use the token to create the charge with a server-side script.
         You can access the token ID with `token.id`
         Pass along various parameters you get from the token response
         and your form.*/                    
        var myData = {
                token: token.id,
                email: token.email,
                amount: Math.round($("#amount").val() * 100),
                message: $("#message").val()
        };
        /* Make an AJAX post request using JQuery,
           change the first parameter to your charge script*/
        $.post("<YOUR ROUTE TO charge.php", myData,function (data) {
            // if you get some results back update results
            $("#myForm").hide();
            $(".results").html("Your charge was successful");
        }).fail(function () {
            // if things fail, tell us
            $(".results").html("I'm sorry something went wrong");
        })
    }
});
$('#myForm').on('submit', function (e) {
    // Open Checkout with further options
    handler.open({
        name: 'Stripe.com',
        email: 'test@test.com',
        description: '2 widgets',
        amount: Math.round($("#amount").val() * 100)
    });
    e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function () {
    handler.close();
});
</script> 

Hope this is of help to someone, experiencing same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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