【发布时间】:2021-04-15 06:16:48
【问题描述】:
我正在按照本指南将条带结帐添加到我的自制网站。 https://stripe.com/docs/checkout/integration-builder
我有一个数据库和一个 PHP 页面。在这个页面中,我用产品循环了一个 mysql 表,并为每个产品显示信息并添加一个“立即购买”条纹按钮。 我用唯一的名称(标签 ID 和名称)调用每个按钮。跟随示例:
<?php
<button id="checkout-button-<?php $row['productid']; ?>">Buy <?php echo $row['productname']; ?></button>
?>
我的问题在于 javascript:
<!-- Start Stripe Integration -->
<script type="text/javascript">
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("<?php echo $stripe_key_public ?>");
var checkoutButton = document.getElementById("checkout-button **$$Here is the problem$$**");
checkoutButton.addEventListener("click", function() {
fetch("my-stripe-checkout.php", {
method: "POST",
})
.then(function(response) {
return response.json();
})
.then(function(session) {
return stripe.redirectToCheckout({
sessionId: session.id
});
})
.then(function(result) {
// If redirectToCheckout fails due to a browser or network
// error, you should display the localized error message to your
// customer using error.message.
if (result.error) {
alert(result.error.message);
}
})
.catch(function(error) {
console.error("Error:", error);
});
});
</script>
<!-- End Stripe Integration -->
如何为我的 PHP 循环之外的每个按钮添加自定义 eventListener 或参数化 javascript 函数(带有项目描述、价格/金额等)?
还是我错误的方法解决方案?有没有更好的方法为产品列表添加条纹结帐按钮?
谢谢
【问题讨论】:
标签: javascript php stripe-payments