简体   繁体   English

如何使用 paypal 智能按钮使产品缺货时无法付款

[英]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.

相关问题 如何知道 PayPal 智能支付按钮是否已完成渲染 - How to know if PayPal Smart Payment Button has finished rendering Paypal 智能按钮:付款成功后如何重定向到 PHP 页面? - Paypal Smart Button: how to redirect to a PHP page after successfull payment? 如何获取paypal智能支付按钮响应Flask - How to get paypal smart payment button response to Flask 在“缺货”版本之间切换时,可变产品“添加到购物车”按钮会短暂显示 - Variable product Add to Cart button displays briefly when switching between 'out of stock' variations 贝宝智能支付按钮动态支付金额 - PayPal Smart Payment Buttons Dynamic Payment Amount PayPal 智能按钮 - 如何编辑重定向 URL - PayPal Smart Button - How To Edit Redirect Url 如何根据环境呈现贝宝智能按钮? - How to render Paypal Smart Button based on environment? Paypal 智能支付按钮 — 如何在服务器端创建订单并将订单 ID 传递给 Paypal 按钮设置? - Paypal Smart Payment Buttons — how to create order server-side and pass order ID to Paypal Buttons setup? 如何设置政策/标志以在缺货时停止销售产品 - How to set a policy/flag to stop a product from selling when out of stock 成功 PayPal 付款后,使 HTML 按钮出现 - Make HTML button appear after successful PayPal payment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM