PHP-PayPal集成,在接受付款后无法运行我的自定义交易功能

PHP-PayPal集成,在接受付款后无法运行我的自定义交易功能,php,paypal,Php,Paypal,我曾经尝试过使用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

我曾经尝试过使用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

之前我遇到过类似的问题。PayPal指南中建议的代码没有更新。尝试使用:它是一个PHP5类,用于处理PayPal IPN通知,它很健壮,很容易使用。阅读本文后,我搜索了另一个简单的教程并找到了一个。稍微调整一下,我可以调整它在多项目(购物车)付款中,我会将链接发布为solution@eldblz