付款成功后保存到数据库(paypal)

2024-05-19

我试图找出在客户使用 paypal 支付商品费用后将数据(之前以表单提交)保存到数据库的最佳方法。沿着这个过程的一些事情:

1) 在实际网站上填写表格 --> 2) 登录 Paypal --> 3) 立即付款 (PayPal) --> 4) 数据已插入数据库 --> 5) 返回起点?

我已经弄清楚如何执行步骤 1 到 3 和 5,但是在执行步骤 4 时需要一些帮助。据我所知,我需要以某种方式存储数据,然后根据需要保存或丢弃存储的数据。最好的方法是什么?

The Form

 <form action="" method="post" target="" id="bookstay">
      <input type="hidden" name="cmd" value="_xclick" />
      <input type="hidden" name="unitprice" value="40" />
      <input type="hidden" name="apt_name" value="Apartment1" />
      <input type="hidden" name="no_note" value=""/>
      <input type="hidden" name="lc" value="MT" />
      <input type="hidden" name="currency_code" value="EUR" />
      <input type="hidden" name="bn" value="BuyNowBF:btn_buynow_LG.gif:NonHostedGuest" />
      <input type="hidden" name="apartment" value="1"/>
      <input name='first_name' class="short-input" id='name' type="text" value="Name" onFocus="this.value = ''" />
      <input  name= 'last_name' class="short-input" id='name' type="text" value="surname" onFocus="this.value = ''" />
      <input  name='payer_email' class="long-input" type="text" value="Email" onFocus="this.value = ''"  />
      <input name='address' class="long-input" type="text" value="Address" onFocus="this.value = ''" />
      <input name='mobile'  class="short-input" type="text" value="mobile" onFocus="this.value = ''"  />
      <div class='select' id='peopletostay'>
           <select name='pax' class='short-input'>
                <option value='0'>people to stay</option>
                <option value='1'>1</option>
                <option value='2'>2</option>
                <option value='3'>3</option>
                <option value='4'>4</option>
           </select>
      </div>                         
      <div id="dateofarrival">
           date of arrival<br>                 
           <div class='select' id='date'>
                <select class="short-input day-from" name="day_from">   
                       <option value= "01" >01</option> 
                          ...
                        <option value= "31" >31</option>
                </select>
           </div>
           <div class='select' id='month'>
            <select class="short-input month-from" name="month_from" size="1">
                  <option value="01" >January</option>
                  ....                    
                  <option value="12" >December</option>
           </select>
           </div>
           <div class='select' id='year'>
                <select class="short-input year-from" name='year_from'>
                      <option value= 2015 > 2015</option>
                       ....
                       <option value= 2025 > 2025</option>
                 </select>
           </div>
      </div>
      <div id="dateodeparture">
           date of arrival<br>                 
           <div class='select' id='date'>
                <select class="short-input day-from" name="day_to">   
                       <option value= "01" >01</option> 
                          ...
                        <option value= "31" >31</option>
                </select>
           </div>
           <div class='select' id='month'>
            <select class="short-input month-from" name="month_to" size="1">
                  <option value="01" >January</option>
                  ....                    
                  <option value="12" >December</option>
           </select>
           </div>
           <div class='select' id='year'>
                <select class="short-input year-from" name='year_to'>
                      <option value= 2015 > 2015</option>
                       ....
                       <option value= 2025 > 2025</option>
                 </select>
           </div>
      </div>
      <textarea name='remarks'>Extra Remarks</textarea>
      <button type="submit" name="proceedtopaypal" id="proceedtopaypal">make booking (proceed to paypal)</button>

      </form>

付款码

<?php
if ($_POST) {
if (isset($_POST['proceedtopaypal'])){

include 'connect.php';

    $apartment = mysqli_real_escape_string($conn, $_POST['apartment']);
    $unitprice = mysqli_real_escape_string($conn, $_POST['unitprice']);
    $first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
    $last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
    $payer_email = mysqli_real_escape_string($conn, $_POST['payer_email']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $apt_name = mysqli_real_escape_string($conn, $_POST['apt_name']);
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
    $pax = mysqli_real_escape_string($conn, $_POST['pax']);
    $remarks = mysqli_real_escape_string($conn, $_POST['remarks']);
    $day_from = mysqli_real_escape_string($conn, $_POST['day_from']);
    $month_from = mysqli_real_escape_string($conn, $_POST['month_from']);
    $year_from = mysqli_real_escape_string($conn, $_POST['year_from']);
    $booking_from = $year_from."-".$month_from."-".$day_from;
    $day_to = mysqli_real_escape_string($conn, $_POST['day_to']);
    $month_to = mysqli_real_escape_string($conn, $_POST['month_to']);
    $year_to = mysqli_real_escape_string($conn, $_POST['year_to']);
    $booking_to = $year_to."-".$month_to."-".$day_to;
    $no_of_nights = abs(strtotime($booking_to) - strtotime($booking_from)); 
    $quantity = floor($no_of_nights / (60*60*24));

    // paypal settings 
    $paypal_email = '[email protected] /cdn-cgi/l/email-protection';
    $return_url = 'http://localhost/Webdevelopment/V18/apartments.php';
    $cancel_url = 'http://localhost/Webdevelopment/V18/apartments.php';
    $notify_url = 'http://localhost/Webdevelopment/V18/paypal/payments.php';

    $item_amount = $unitprice * $quantity;
    $item_name = "Booking at ".$apt_name." from " .$booking_from ." to " .$booking_to;
    $validdate = false;
    $buttonpressed = false;
    $checkin='<p>Check in date is invalid.</p>';
    $checkout='<p>Check out date is invalid</p>';
    $larger = '<p>Check in date is after check out date</p>';
    $noinfo='<p>please fill in the missing information.</p>';
    $booked='<p>The dates selected are already booked for this apartment</p>';
    $equal = '<p>You need to spend a minimum of 1 night in these apartment</p>';
    $thankyou = '<h5>Thank you</h5><p>thank you for booking an apartment with V18-apartments.</p>';
    $window = '';

        function IsInjected($str) {
          $injections = array('(\n+)',
                      '(\r+)',
                      '(\t+)',
                      '(%0A+)',
                      '(%0D+)',
                      '(%08+)',
                      '(%09+)'
                      );
          $inject = join('|', $injections);
          $inject = "/$inject/i";
          if(preg_match($inject,$str))
            {
            return true;
          }
          else
            {
            return false;
            }
        }

        if (!checkdate($month_from, $day_from, $year_from)) {
            $window = $checkin;
            echo $window;
            $validate = true;
        }
        else if (!checkdate($month_to, $day_to, $year_to)) {
            $window = $checkout;
            $validate = true;
            echo $window;
            //echo "Check out date is invalid";
        }
        else if ($booking_from > $booking_to) {
                $window = $larger;
                $validate = true;
                echo $window;
                // echo "Check in date is after check out date";
        }
        else if ($booking_from == $booking_to) {
            $window = $equal;
            $validate = true;
            echo $window;
        }   
    // check if all info is filled in 
        else if (($first_name == "Name") || ($last_name == "surname") || ($payer_email == "Email") || ($mobile == "mobile") || ($address == "Address")) {
            $window = $noinfo;
            echo $window;
            $validate = true;
            // echo "Please fill in the missing information";
        }
        else if (IsInjected($payer_email)) {
            echo "Not an email";
        }
        else if ($validdate == false) {
            $final = true;
            $sql = "SELECT COUNT(*)  FROM room_nights WHERE apartmentID= '$apartment' AND dates >= '$booking_from' AND dates <= '$booking_to'";
            $result = mysqli_query($conn, $sql);
            $result = mysqli_query($conn, $sql);
            $row=mysqli_fetch_row($result);

            if ($row[0] > 0) {
                $window = $booked;
                echo $window;
            }

        else if ($final == true)  {
            // save to database 
                include 'insertdata.php';   // code below

                echo $item_name;
                // include functions
                include ("pay_functions.php");
                // Check if paypal request or response
                if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){
                // Firstly Append paypal account to querystring
                    $querystring .= "?business=".urlencode($paypal_email)."&";  
                    // Append amount& currency (£) to quersytring so it cannot be edited in html
                    //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
                    $querystring .= "item_name=".urlencode($item_name)."&";
                    $querystring .= "amount=".urlencode($item_amount)."&";
                        //loop for posted values and append to querystring
                        foreach($_POST as $key => $value){
                            $value = urlencode(stripslashes($value));
                            $querystring .= "$key=$value&";
                        }
                    // Append paypal return addresses
                    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
                    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
                    $querystring .= "notify_url=".urlencode($notify_url);
                    // Append querystring with custom field
                    //$querystring .= "&custom=".USERID;
                    // Redirect to paypal IPN
                    header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
                    exit();

                } 
            else {
                    // Response from paypal
                    $req = 'cmd=_notify-validate';
                    foreach ($_POST as $key => $value) {
                        $value = urlencode(stripslashes($value));
                        $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
                        $req .= "&$key=$value";
                    }

                    // assign posted variables to locate variables
                    $data['item_name'] = $_POST['item_name'];
                    $data['item_number'] = $_POST['item_number'];
                    $data['payment_status'] = $_POST['payment_statis'];
                    $data['payment_amount'] = $_POST['mc_gross'];
                    $data['payment_currency'] = $_POST['mc_currency'];
                    $data['txn_id'] = $_POST['txn_id'];
                    $data['receiver_email'] = $_POST['receiver_email'];
                    $data['payer_email'] = $_POST['payer_email'];
                    $data['custom'] = $_POST['custom'];

                    // post back to paypal system and validate

                    $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
                    $header .= "Content-Type : application/x-www-form-urlencoded\r\n";
                    $header .= "Content-Lenght: " .strlen($req) . "\r\n\r\n";

                    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

                if (!$fp) {
                // HTTP error
                } else {
                    mail('[email protected] /cdn-cgi/l/email-protection', '0', '0');
                    fputs ($fp, $header . $req);
                    while (!feof($fp)) {
                        $res = fgets($fp, 1024);
                        if (strcmp ($res, "VERIFIED") == 0) {

                             // validate payment (check unique txnid & correct price) 
                             $valid_txnid = check_txnid($data['txn_id']);
                             $valid_price = check_price($data['payment_amount'], $data['item_number']);
                             // Payment validated and verified
                            if ($valid_price && $valid_price) {
                                 $orderid = updatePayments($data);
                                if ($orderid){
                                     // payment has been made and inserted into db
                                } else {
                                     echo "Error";
                                }
                            } 
                            else if (strcmp($res, "INVALID") == 0) {
                                    echo "Payment invalid";
                            }
                        }
                             fclose($fp);
                    }
                }
            }    
        } 
    }
}

 }

?>

插入数据.PHP

 <?php

    $apartment = mysqli_real_escape_string($conn, $_POST['apartment']);
    $unitprice = mysqli_real_escape_string($conn, $_POST['unitprice']);
    $first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
    $last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
    $payer_email = mysqli_real_escape_string($conn, $_POST['payer_email']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $apt_name = mysqli_real_escape_string($conn, $_POST['apt_name']);
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
    $pax = mysqli_real_escape_string($conn, $_POST['pax']);
    $remarks = mysqli_real_escape_string($conn, $_POST['remarks']);
    $day_from = mysqli_real_escape_string($conn, $_POST['day_from']);
    $month_from = mysqli_real_escape_string($conn, $_POST['month_from']);
    $year_from = mysqli_real_escape_string($conn, $_POST['year_from']);
    $booking_from = $year_from."-".$month_from."-".$day_from;
    $day_to = mysqli_real_escape_string($conn, $_POST['day_to']);
    $month_to = mysqli_real_escape_string($conn, $_POST['month_to']);
    $year_to = mysqli_real_escape_string($conn, $_POST['year_to']);
    $booking_to = $year_to."-".$month_to."-".$day_to;
    $no_of_nights = abs(strtotime($booking_to) - strtotime($booking_from)); 
    $quantity = floor($no_of_nights / (60*60*24));
    $reason = "Booked by ".$first_name." ".$last_name." for ".$pax ." people";

function daterange($booking_from, $booking_to, $step = '+1 day', $output_format = 'Y-m-d') {
  $dates = array();
  $first = new DateTime($booking_from);
  $last = new DateTime($booking_to);
  $last = $last->modify('+ 1 day');
  $interval = DateInterval::createFromDateString($step);
  $period = new DatePeriod($first, $interval, $last);


  foreach ($period as $date) {
      $dates[] = $date->format($output_format);
  } 

  return $dates;
}

$dates = daterange($booking_from, $booking_to);
include 'connect.php';

 if (!$conn->autocommit(FALSE)) {
    printf("Errormessage: %s\n", $conn->error);
 }

 if (!$conn->query("INSERT INTO client_details (clientID, name, email, address, mobile) VALUES ('', '$first_name $last_name', '$payer_email', '$address', '$mobile')")) {
     printf("Errormessage: %s\n", $conn->error);
 }


 if (!$conn->query("INSERT INTO bookings (bookingID, apartmentID, clientID, date_from, date_to, nights, pax, remarks) VALUES ('', '$apartment', LAST_INSERT_ID(), '$booking_from', '$booking_to', '$days', '$pax', '$remarks')")) {
     printf("Errormessage: %s\n", $conn->error);
 }

 foreach ($dates as $date) {
 if (!$conn->query("INSERT INTO room_nights (bookingID, apartmentID, dates, reason) VALUES (LAST_INSERT_ID(), '$apartment', '$date', '$reason')")) {
      printf("Errormessage: %s\n", $conn->error);
 }
 }

 if (!$conn->commit()) {
     printf("Errormessage: %s\n", $conn->error);
 }
 $conn->close();

?>

你的步骤错了

1) 在实际网站上填写表格 --> 2) 登录 Paypal --> 3) 立即付款 (PayPal) --> 4) 数据已插入数据库 --> 5) 返回起点?

步骤 3 之后的原因,您将如何找到在步骤 1 中填写的表单数据,当用户单击“提交”并离开实际网站并登录 Paypal 时,您将丢失表单数据,并且用户也可以提出虚假声明从您的网站购买或根据您销售的产品或服务付款。

处理 Paypal 时应采取的步骤

  1. 实际网站填写From
  2. 提交表单时验证表单数据,如果验证成功,则将数据保存到数据库并将用户重定向到 Paypal
  3. 用户登录 Paypal 并付款
  4. 通过 Paypal 即时付款通知 (IPN) 获取付款详细信息(当用户仍在 Paypal 网站上时)IPN 详细信息 https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNSetup/在你的情况下是payments.php(在 IPN 中,Paypal 发布交易详细信息、金额详细信息等,您需要在第 5 步中更新数据库,否则您将无法弄清楚用户针对哪个产品付款)
  5. 根据Paypal即时支付通知(支付成功、支付失败、支付待处理)更新数据库中的数据
  6. 使用返回 URL 将用户重定向回实际网站返回 URL 详细信息 https://www.paypal.com/az/cgi-bin/webscr?cmd=p/mer/express_return_summary-outside return.php
  7. 在实际网站的返回 URL 上显示付款详细信息和其他详细信息。

旁注:第 7 步,在此步骤中,您可以提供一个唯一的参考号(仅在成功付款时生成),并将该参考号提供给从您的网站购买的用户,否则您最终可能会遇到声称自己已付款的用户针对任何产品的付款。

(在与 Paypal 打交道时,请记住,PayPal 总是更喜欢消费者而不是商家,因此您必须小心,否则如果出现太多欺诈性投诉,他们将冻结您的帐户)

就您的代码而言,只需使用以下命令转义表单值mysqli_real_escape_string like mysqli_real_escape_string($_POST['apartment']);并且无需使用您已经在使用的 PDOMySQLi转义字符串并在服务器端验证表单输入足以避免SQL Injection vulnerabilities

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

付款成功后保存到数据库(paypal) 的相关文章

  • PHP - Filter_var 替代方案?

    我构建了一个 php 脚本来输出以表单形式发布的数据 但遇到了问题 网站将运行的服务器运行 PHP 5 1 6 此版本的 PHP 不支持 filter var 我需要知道短期内的替代方案 最好是昨天 但在 Google 或 Stack Ov
  • 自定义 WP 主题时,我应该将导航栏放在“”标签之前还是之后?

    我正在通过制作子主题来自定义 WP 主题 我将 Bootstrap 中的导航栏放入子主题目录中的 header php 文件中 但是 我不确定在哪里放置导航栏代码 我可以把它都放在前面and之后标记成功 例如 无论我选择哪一个 导航栏都显示
  • 为什么我不能在 TCPDF 表中使用 č,ć,đ 图表?

    我正在为我的网站构建一个 tcpdf 文件 该 tcpdf 文件中有一个包含一些数据的表格 但我无法使该章程正常工作 对于编码 我使用 windows 1250 宪章女巫不起作用 我已经尝试过 utf 8 但仍然没有得到这个章程 tcpdf
  • WooCommerce 使用 AJAX 设置购物车数量?

    我已经为此绞尽脑汁好几天了 需要一些指导 我正在为 WooCommerce 网站完全从头开始制作自定义主题 现在我正在尝试让购物车功能正常工作 我一直试图使用按钮 来更新购物车中产品的数量 对我来说问题似乎是WC 我在functions p
  • PHP - 获取base64图像字符串解码并保存为jpg(生成空图像)

    嗨 我实际上是通过 ajax 发送一个 base64 图像字符串到一个 php 脚本 该脚本只是解码字符串并将内容保存为 jpg 文件 但结果是一张空图像 这怎么可能 PHP脚本 uploadedPhotos array photo 1 p
  • Yii2 异常:ApcCache 需要加载 PHP apc 扩展

    在高级模板前端的主配置中配置缓存组件时 我收到异常 在我的 php ini 上启用了扩展 rsults 如何解决此问题 前端 config main php cache gt class gt yii caching ApcCache ke
  • PHP 或 WAMP 不确定是什么

    我已经安装了 WAMP 服务器 2 0 PHP 5 4 3 安装WAMP后我已经重新启动了所有服务并且可以打开 phpinfo 显示良好 phpmyadmin 它也显示得很好 我可以使用数据库 然而 当在 Chrome 中运行简单的 php
  • .htaccess 异常导致主目录出现问题

    这是我的目录结构 localhost or livehost app bootstrap public vendor code demo 这是我的 htaccess
  • 如何将 HTML 转换为 Markdown?

    我有一个类似 stackoverflow 的网站 有一个文本区域 人们可以在其中写答案 我用这个 PHP 库 http parsedown org 转换降价 我的意思是我使用该函数来转换 italic to i italic i inclu
  • 当sql连接中存在两个同名列时,如何从一个表列中获取值

    当我连接两个具有相同名称列的表时 我目前面临着尝试获取值的问题 例如 table1 date和table2 date 每个表中的日期不同 我将如何获取 日期 本例中的表1 我目前正在跑步 while row mysqliquery gt f
  • 通过 facebook graph API 检索 facebook 用户的邮政编码

    我正在尝试使用 facebook graph API 检索用户的邮政编码 我正在使用以下代码 代码在php ini中 facebook new Facebook array appId gt APP ID secret gt APP SEC
  • 如何在类似 MVC 的页面中加载基于漂亮 URL 的类?

    我想请教一些关于如何解决这个问题的提示 我正在尝试构建自己的 MVC 网站 我了解了 URL 的基础知识 http example com blog cosplay cosplayer expo today 博客 gt 控制器cosplay
  • 使用先前的反向引用作为命名捕获组的名称

    有没有办法使用对先前捕获组的反向引用作为捕获组的名称命名捕获组 这可能不可能 如果不可能 那么这就是一个有效的答案 下列 data description some description preg match data matches p
  • 如何阻止直接访问我的 JavaScript 文件?

    我使用 Minify 来缩小并缓存所有脚本请求 我只希望我的用户能够访问 JavaScript 文件的缩小版本 缩小位于www example com min我的脚本位于www example com scripts 如何阻止直接访问doc
  • 如何在 PHP 中使用 cURL 发出同时包含 GET 和 POST 参数的请求?

    其他人已经问过如何从 perl java bash 等执行此操作 但我需要在 PHP 中执行此操作 并且我没有看到任何已提出的专门与 PHP 相关的问题 或包含 PHP 的答案 My code ch curl init url curl s
  • PHP:读取字体文件的 TrueType/OpenType 元数据

    如何阅读字体详细信息 例如 字体在其元数据中包含版权 姓氏 设计者 版本等信息 我还希望脚本能够计算文件中的字形数量 并返回字体支持的语言 例如 典型的字体可能包含西方语言 瑞典语和罗马语言支持 并具有数百个字形 它应该支持 truetyp
  • 使用 XSLT 将 XML 转换为 SQL

    由于我无法控制的原因 我将获得一个 XML 文件和一个 XSLT 文件 该文件可以将 XML 文件转换为 SQL 代码或错误 现在让我们假设我们可以信任提供 XML 文件的人不会在 XML 中包含危险的构造 我什至不知道是否应该使用 Sim
  • apache_request_headers() 与 $_SERVER

    据我所知 apache request headers 提供与以下相同的信息 SERVER 但按键略有不同 为什么有人应该使用apache request headers 而不仅仅是从那里获取这些信息 SERVER 我在 Centos 上使
  • 从 PHP 数组生成 HTML 表

    我不明白这一点 我需要解决看似简单的问题 但这超出了我的逻辑 我需要编写一个函数 table columns input cols 它将输出一个表 示例 input array apple orange monkey potato chee
  • 如何在laravel中注册后自动登录

    我在 laravel 中注册用户时遇到问题 user假设是包含所有数组元素的数组 同时自动登录以下代码结果false 数据库中保存的密码是hash make password user id this gt user model gt ad

随机推荐