第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 Ajax 在一頁(yè)上添加多個(gè)條紋結(jié)賬按鈕

使用 Ajax 在一頁(yè)上添加多個(gè)條紋結(jié)賬按鈕

PHP
瀟湘沐 2023-08-19 16:28:47
我為使用 AJAX 提交的三種不同產(chǎn)品設(shè)置了三個(gè) Stripe 按鈕,如下頁(yè)面部分所示:<!-- 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>一個(gè)按鈕可以工作,但很難找到一種方法將每個(gè)按鈕發(fā)送到自己的表單,以便使用類似的 AJAX 方法通過條帶進(jìn)行處理。           <script>var buyBtn = document.getElementById('payButton');var responseContainer = document.getElementById('paymentResponse');// Create a Checkout Session with the selected productvar 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.jsvar 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>我正在努力尋找一種在 AJAX 中為每個(gè)相應(yīng) ID 運(yùn)行此腳本的方法 - 例如,第一個(gè)腳本附加到 id="payButton",下一個(gè)腳本附加到 id="payButton2",最后一個(gè)腳本附加到 id="payButton3 ”
查看完整描述

1 回答

?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

一種選擇是添加 3 個(gè)不同的點(diǎn)擊處理程序,每個(gè)按鈕一個(gè)。然后,當(dāng)單擊按鈕時(shí),您可以向服務(wù)器傳遞對(duì)該按鈕的一些引用和 ID,這將有助于創(chuàng)建正確類型的結(jié)賬會(huì)話。


一個(gè)非?;镜陌姹究赡苋缦滤荆?/p>


// 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,

      });

    }

  });

};

從技術(shù)上講,您可以將其“干燥”一點(diǎn),但我認(rèn)為這應(yīng)該更簡(jiǎn)單一些。


查看完整回答
反對(duì) 回復(fù) 2023-08-19
  • 1 回答
  • 0 關(guān)注
  • 152 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)