从输入字段输入的PHP贝宝酒店预订不会仅保存贝宝付款

时间:2019-04-30 09:40:27

标签: php mysql mysqli paypal paypal-sandbox

在输入字段中输入“ PHP Paypal酒店预订”将不会仅保存PayPal付款

我想从结帐中保存我的输入信息,但只会保存PayPal信息

checkout.php

<?php
// Redirect to the home page if id parameter not found in URL
if(empty($_GET['id'])){
    header("Location: reservation.php");
}

// Include and initialize database class
include 'DB.class.php';
$db = new DB;

// Include and initialize paypal class
include 'PaypalExpress.class.php';
$paypal = new PaypalExpress;

// Get product ID from URL
$productID = $_GET['id'];

// Get product details
$conditions = array(
    'where' => array('id' => $productID),
    'return_type' => 'single'
);
$productData = $db->getRows('products', $conditions);

// Redirect to the home page if product not found
if(empty($productData)){
    header("Location: index.php");
}

?>

<div class="item">
    <form method="post">

                  <label>Name</label>
                  <input type="text" name="name" required>
                  <label>Phone</label>
                  <input type="text" name="contactnum" required>
                  <label>Email</label>
                  <input type="email"\ name="email" required>
                  <label>Address</label>
                  <input type="text" name="address" required>
                  <label>Date Check In</label>
                  <input type="date" name="checkin" required>
                  <label>Date Check Out</label>
                  <input type="date" name="checkout" required>
                  <label>Room Type</label>
                  <select name="roomtype" required>
                    <option value="Standard">Standard</option>
                    <option value="Deluxe">Deluxe</option>
                  </select>

    <!-- Product details -->
                  <p>Name: <?php echo $productData['name']; ?></p>
                  <p>Price: <?php echo $productData['price']; ?></p>
    <!-- Checkout button -->
    <div id="paypal-button"></div>
</form>
</div>

<script src="https://www.paypalobjects.com/api/checkout.js"></script>

<!--
JavaScript code to render PayPal checkout button
and execute payment
-->
<script>
paypal.Button.render({
    // Configure environment
    env: '<?php echo $paypal->paypalEnv; ?>',
    client: {
        sandbox: '<?php echo $paypal->paypalClientID; ?>',
        production: '<?php echo $paypal->paypalClientID; ?>'
    },
    // Customize button (optional)
    locale: 'en_US',
    style: {
        size: 'small',
        color: 'gold',
        shape: 'pill',
    },
    // Set up a payment
    payment: function (data, actions) {
        return actions.payment.create({
            transactions: [{
                amount: {
                    total: '<?php echo $productData['price']; ?>',
                    currency: 'USD'
                }
            }]
      });
    },
    // Execute the payment
    onAuthorize: function (data, actions) {
        return actions.payment.execute()
        .then(function () {
            // Show a confirmation message to the buyer
            //window.alert('Thank you for your purchase!');

            // Redirect to the payment process page
            window.location = "process.php?paymentID="+data.paymentID+"&token="+data.paymentToken+"&payerID="+data.payerID+"&pid=<?php echo $productData['id']; ?>";
        });
    }
}, '#paypal-button');
</script>

process.php

<?php
            $checkin = $_POST['checkin'];
            $checkout = $_POST['checkout'];
            $roomtype = $_POST['roomtype'];
            $name = $_POST['name'];
            $contactnum = $_POST['contactnum'];
            $address = $_POST['address'];
            $email = $_POST['email'];
$redirectStr = '';
if(!empty($_GET['paymentID']) && !empty($_GET['token']) && !empty($_GET['payerID']) && !empty($_GET['pid']) ){
    // Include and initialize database class
    include 'DB.class.php';
    $db = new DB;

    // Include and initialize paypal class
    include 'PaypalExpress.class.php';
    $paypal = new PaypalExpress;

    // Get payment info from URL
    $paymentID = $_GET['paymentID'];
    $token = $_GET['token'];
    $payerID = $_GET['payerID'];
    $productID = $_GET['pid'];

    // Validate transaction via PayPal API
    $paymentCheck = $paypal->validate($paymentID, $token, $payerID, $productID);

    // If the payment is valid and approved
    if($paymentCheck && $paymentCheck->state == 'approved'){

        // Get the transaction data
        $id = $paymentCheck->id;
        $state = $paymentCheck->state;
        $payerFirstName = $paymentCheck->payer->payer_info->first_name;
        $payerLastName = $paymentCheck->payer->payer_info->last_name;
        $payerName = $payerFirstName.' '.$payerLastName;
        $payerEmail = $paymentCheck->payer->payer_info->email;
        $payerID = $paymentCheck->payer->payer_info->payer_id;
        $payerCountryCode = $paymentCheck->payer->payer_info->country_code;
        $paidAmount = $paymentCheck->transactions[0]->amount->details->subtotal;
        $currency = $paymentCheck->transactions[0]->amount->currency;

        // Get product details
        $conditions = array(
            'where' => array('id' => $productID),
            'return_type' => 'single'
        );
        $productData = $db->getRows('products', $conditions);
        // If payment price is valid
        if($productData['price'] >= $paidAmount){

            $checkin = $checkin;
            $checkout = $checkout;
            $roomtype = $roomtype;
            $name = $name;
            $contactnum = $contactnum;
            $address = $address;
            $email = $email;
            // Insert transaction data in the database
            $data = array(
                'product_id' => $productID,
                'txn_id' => $id,
                'payment_gross' => $paidAmount,
                'currency_code' => $currency,
                'payer_id' => $payerID,
                'payer_name' => $payerName,
                'payer_email' => $payerEmail,
                'payer_country' => $payerCountryCode,
                'name' => $name,
                'contactnum' => $contactnum,
                'email' => $email,
                'address' => $address,
                'checkin' => $checkin,
                'checkout' => $checkout,
                'roomtype' => $roomtype,
                'payment_status' => $state
            );
            $insert = $db->insert('payments', $data);

            // Add insert id to the URL
            $redirectStr = '?id='.$insert;
        }
    }

    // Redirect to payment status page
    header("Location:payment-status.php".$redirectStr);
}else{
    // Redirect to the home page
    header("Location:index.php");
}
?>

每当我结帐时,它只会保存Paypal信息,而不保存用户信息

0 个答案:

没有答案