[英]How to make payment unavailable when product out of stock with paypal smart button
I am using a PayPal smart button to carry out payments for my website.我正在使用 PayPal 智能按钮为我的网站进行付款。 What can I do in order to make payments unavailable when an item is out of stock?
当商品缺货时,我该怎么做才能使付款不可用? It might be helpful to know that the items are singular items so the stock number will always be 1. Here is my current code:
知道这些物品是单数物品可能会有所帮助,因此库存编号将始终为 1。这是我当前的代码:
<?php
include 'pdo_connect.php';
$product_id = $_GET['product'];
$stmt = $db->prepare("SELECT * FROM products Where id = :id");
$stmt->bindParam(':id', $product_id );
$stmt->execute();
while($row = $stmt->fetch()){
$stock = $row['stock'];
if ($stock > 0){
echo
"<script>
paypal.Buttons({
style: {
shape: 'rect',
color: 'blue',
layout: 'vertical',
label: 'paypal',
locale: 'en_CY'
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: ". $row['paypal_price'] .",
currency: 'EUR'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
localStorage.clear();
window.location.href = 'http://localhost/website/thankyou.php';
//alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>";
}
}
?>
Any insight would be highly appreciated.任何见解将不胜感激。
You already have an if statement for displaying the buttons, so you can add an else statement to display the out of stock message.您已经有一个用于显示按钮的 if 语句,因此您可以添加一个 else 语句来显示缺货消息。
A problem with your implementation is that the button could render simultaneously for two different people, and both could initiate the purchase and hence you would get two payments for the same single item, and need to issue a refund.您的实现的一个问题是该按钮可以同时为两个不同的人呈现,并且两个人都可以启动购买,因此您将获得同一个项目的两次付款,并且需要退款。 If this concerns you, you should communicate with your server from the createOrder function, using eg the fetch API.
如果这与您有关,您应该从 createOrder function 与您的服务器通信,例如使用获取 API。 There is an example in this surprisingly similar question: https://stackoverflow.com/a/62961660/2069605
在这个惊人的相似问题中有一个例子: https://stackoverflow.com/a/62961660/2069605
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.