简体   繁体   English

使用 Ajax 在一个页面上添加多个条带结帐按钮

[英]Add Multiple Stripe Checkout Buttons on one page using Ajax

I have three Stripe buttons for three different products that submit using AJAX, as shown below in the portion of the page below:对于使用 AJAX 提交的三种不同产品,我有三个 Stripe 按钮,如下页面部分所示:

<!-- key stripe settings such as stripe account and product variables -->
<?php require_once 'config.php';   ?>

 <div id="buynow">
 <button class="stripe-button stripebutton1" id="payButton">Buy Now</button>
 </div>
 <div id="buynow2"> 
 <button class="stripe-button stripebutton1" id="payButton2">Buy Now</button> 
 </div> 
  <div id="buynow3">
 <button class="stripe-button stripebutton1" id="payButton3">Buy Now</button>
  </div>
<script src="https://js.stripe.com/v3/"></script>

One button works, but having trouble finding a way send each button to its own form for processing by stripe using a similar AJAX approach.一个按钮可以工作,但很难找到一种方法将每个按钮发送到其自己的表单,以便使用类似的 AJAX 方法通过条带进行处理。

           <script>
var buyBtn = document.getElementById('payButton');
var responseContainer = document.getElementById('paymentResponse');

// Create a Checkout Session with the selected product
var createCheckoutSession = function (stripe) {
    return fetch("stripe_charge.php", {
         method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
        checkoutSession: 1,
    }),
}).then(function (result) {
        return result.json();
});
};

// Handle any errors returned from Checkout
 var handleResult = function (result) {
if (result.error) {
    responseContainer.innerHTML = '<p>'+result.error.message+'</p>';
}
buyBtn.disabled = false;
buyBtn.textContent = 'Buy Now';
};

// Specify Stripe publishable key to initialize Stripe.js
var stripe = Stripe('<?php echo STRIPE_PUBLISHABLE_KEY; ?>');

buyBtn.addEventListener("click", function (evt) {
    buyBtn.disabled = true;
buyBtn.textContent = 'Please wait...';

createCheckoutSession().then(function (data) {
    if(data.sessionId){
        stripe.redirectToCheckout({
            sessionId: data.sessionId,
        }).then(handleResult);
    }else{
        handleResult(data);
    }
});
});
</script>

I am struggling a bit to find am an approach in AJAX to run this script for each corresponding ID - for example a first script attached to id="payButton", the next to id="payButton2" and the last to id="payButton3"我正在努力寻找 AJAX 中的一种方法来为每个相应的 ID 运行此脚本 - 例如附加到 id="payButton" 的第一个脚本,id="payButton2" 的旁边和 id="payButton3 的最后一个脚本"

One option would be to add 3 different click handlers, one for each button.一种选择是添加 3 个不同的点击处理程序,每个按钮一个。 Then, when a button is clicked, you can pass some reference to that button and an ID to the server which would help create the correct type of Checkout Session.然后,当单击按钮时,您可以将对该按钮的一些引用和一个 ID 传递给服务器,这将有助于创建正确类型的 Checkout Session。

Here's what a very basic version might look like:这是一个非常基本的版本可能的样子:

// initialize Stripe, first.
var stripe = Stripe('<?php echo STRIPE_PUBLISHABLE_KEY; ?>');

// grab reference to the buttons
var buyBtn = document.getElementById('payButton');
var buyBtn2 = document.getElementById('payButton2');
var buyBtn3 = document.getElementById('payButton3');

// register click handlers for each button
buyBtn.addEventListener("click", function (evt) {
  buyBtn.disabled = true;
  buyBtn.textContent = 'Please wait...';
  // each click handler will pass some
  // ID that will be passed to the server
  // which would use that identifier to determine
  // what parameters to use to create the
  // Checkout Session.
  createCheckoutSession(1, buyBtn);
});
buyBtn2.addEventListener("click", function (evt) {
  buyBtn2.disabled = true;
  buyBtn2.textContent = 'Please wait...';
  createCheckoutSession(2, buyBtn2);
});
buyBtn3.addEventListener("click", function (evt) {
  buyBtn3.disabled = true;
  buyBtn3.textContent = 'Please wait...';
  createCheckoutSession(3, buyBtn3);
});

// no need to pass in `stripe` here, as it's
// in the parent scope, instead we're passing
// the identifier down.
function createCheckoutSession(identifier, btn) {
  return fetch("stripe_charge.php", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      checkoutSession: identifier, // passing button identifier to server
    }),
  }).then(function (result) {
    return result.json();
  }).then(function (result) {
    // if the stripe_charge.php call fails
    // re-enable the button.
    if (result.error) {
      responseContainer.innerHTML = '<p>'+result.error.message+'</p>';
      btn.disabled = false;
      btn.textContent = 'Buy Now';
    } else {
      // if the call to server was successful, redirect to Checkout.
      stripe.redirectToCheckout({
        sessionId: data.sessionId,
      });
    }
  });
};

You could technically "dry" this up a bit, but I think this should be a little more straightforward.你可以在技术上“干”一点,但我认为这应该更简单一点。

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

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