PHP - PayPal集成,失败后运行我的自定义交易功能支付接受


PHP - PayPal Integration, failed running my custom transaction function after payment accepted

我曾尝试使用教程脚本PayPal支付在过去,它的工作。现在没有了。脚本很简单,所有我需要的是这一页处理付款:

<?php
  session_start();
  include ('mydbconfig.php');
  // payPal settings
  $paypal_email = 'seller@yahoo.com';
  $return_url = 'https://www.mytestsite.com/paypal-thanks.php';
  $cancel_url = 'https://www.mytestsite.com/paypal-cancel.php';
  $notify_url = 'https://www.mytestsite.com/paypal-notify.php';
  $item_name = $_POST['item_name'];
  $item_amount = $_POST['item_amount']; //price
  // 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.paypal.com/cgi-bin/webscr'.$querystring);
    exit();
  } else { // response from Paypal
    // read the post from PayPal system and add 'cmd'
    $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 local variables
    $data['item_name'] = $_POST['item_name'];
    $data['item_number'] = $_POST['item_number'];
    $data['payment_status'] = $_POST['payment_status'];
    $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 to validate
    $header = "POST /cgi-bin/webscr HTTP/1.0'r'n";
    $header .= "Content-Type: application/x-www-form-urlencoded'r'n";
    $header .= "Content-Length: " . strlen($req) . "'r'n'r'n";
    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
    if (!$fp) {
      // HTTP ERROR
      header('location: https://www.mytestsite.com/paypal-error.php?tr='.$data['txn_id'].'&in='.$data['item_name'].'&pe='.$data['payer_email'].'&pa='.$data['payment_amount'].'&ps='.$data['payment_status']);
    } else {
      fputs ($fp, $header . $req);
      while (!feof($fp)) {
        $res = fgets ($fp, 1024);
        if (strcmp ($res, "VERIFIED") == 0) {
          //payment accepted, insert transaction to my database
          //function to insert transaction here
        } else if (strcmp ($res, "INVALID") == 0) {
          //something to do if failed
        }
      }
      fclose ($fp);
    }
  }
?>

过程完成后,我检查并成功支付,但我的函数或我在//函数中编写的插入事务的任何内容将不会执行。最后,我被迫在paypal-thanks.php页面上执行该功能。剧本有什么问题吗?

这个脚本可以用来发送多个项目的购买吗?我的购物车是我自己定制的,我只想发送项目名称,编号,价格详细信息和总价到PayPal订单摘要。

我在这里检查了其他PayPal集成问题,其中大多数都指向PayPal教程,文档或集成向导,这些都令人困惑。我以前用过这个简单的脚本,因为我看不懂PayPal文档(和示例代码,它甚至不让我知道从哪里开始):(

最后我的终极问题是,这个脚本是正确和安全的方式来进行支付交易吗?

使用

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

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

我认为最好使用CURL代替socket