简体   繁体   中英

PayPal Checkout: Updating values in database after payment

I am currently working on on my website's store. I am using the smart paypal button to carry out payments. I am selling singular items so the stock number will always be one. What I am doing is when the order is approved, it updates the stock number in the database. When I tested the shop however, nothing changed. Here is my current code.

<?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 + '!');
                    alert('Your order has been processed');

                    $row = 'UPDATE products SET `stock` = 0';

                });
            }
        }).render('#paypal-button-container');
      </script>";
    }
}
?>

What am I doing wrong? Thanks in advance!

onApprove must call an actual route on your server, using a request such as fetch


ideally the capture should happen on your server, and not using action.orders.capture() . See the pattern at https://developer.paypal.com/demo/checkout/#/pattern/server

You can use the Checkout-PHP-SDK. See the guide for 'Set Up Transaction' and 'Capture Transaction' at https://developer.paypal.com/docs/checkout/reference/server-integration/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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