在Sandbox中工作的IPN脚本,但不在Live中

时间:2018-02-24 01:14:29

标签: mysql paypal paypal-sandbox paypal-ipn

我正在使用PayPal中的代码并在Stackoverflow上找到。这些脚本在沙盒模式下工作正常,但不在实时模式下工作。这是我的代码PaypalIPN:

<?php
class PaypalIPN
{
private $use_sandbox = false;
private $use_local_certs = false;
const VERIFY_URI = 'https://ipnpb.paypal.com/cgi-bin/webscr';
const SANDBOX_VERIFY_URI = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr';
const VALID = 'VERIFIED';
const INVALID = 'INVALID';
public function useSandbox()
{
    $this->use_sandbox = false;
}
public function usePHPCerts()
{
    $this->use_local_certs = false;
}
public function getPaypalUri()
{
    if ($this->use_sandbox) {
        return self::SANDBOX_VERIFY_URI;
    } else {
        return self::VERIFY_URI;
    }
}
public function verifyIPN()
{
    if ( ! count($_POST)) {
        throw new Exception("Missing POST Data");
    }

    $raw_post_data = file_get_contents('php://input');
    $raw_post_array = explode('&', $raw_post_data);
    $myPost = array();
    foreach ($raw_post_array as $keyval) {
        $keyval = explode('=', $keyval);
        if (count($keyval) == 2) {
         if ($keyval[0] === 'payment_date') {
                if (substr_count($keyval[1], '+') === 1) {
                    $keyval[1] = str_replace('+', '%2B', $keyval[1]);
                }
            }
            $myPost[$keyval[0]] = urldecode($keyval[1]);
        }
    }
    $req = 'cmd=_notify-validate';
    $get_magic_quotes_exists = false;
    if (function_exists('get_magic_quotes_gpc')) {
        $get_magic_quotes_exists = true;
    }
    foreach ($myPost as $key => $value) {
        if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
            $value = urlencode(stripslashes($value));
        } else {
            $value = urlencode($value);
        }
        $req .= "&$key=$value";
    }

    $ch = curl_init($this->getPaypalUri());
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSLVERSION, 6);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    if ($this->use_local_certs) {
        curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "cert/cacert.pem");
    }
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
    $res = curl_exec($ch);
    if ( ! ($res)) {
        $errno = curl_errno($ch);
        $errstr = curl_error($ch);
        curl_close($ch);
        throw new Exception("cURL error: [$errno] $errstr");
    }

    $info = curl_getinfo($ch);
    $http_code = $info['http_code'];
    if ($http_code != 200) {
        throw new Exception("PayPal responded with http code       $http_code");
    }

    curl_close($ch);
    if ($res == self::VALID) {
        return true;
    } else {
        return false;
    }
}
}

IPN侦听器

<?php namespace Listener;
require 'PaypalIPN.php';
require_once('ppdbconnect.php');
use PaypalIPN;
$ipn = new PaypalIPN();
// Use the sandbox endpoint during testing.
//$ipn->useSandbox();
 $verified = $ipn->verifyIPN();
 if ($verified) {
 $item_name = $_POST['item_name'];
 $amount = $_POST['mc_gross'];
 $currency = $_POST['mc_currency'];
 $payer_email = $_POST['payer_email'];
 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];
 $country = $_POST['residence_country'];
 $txn_id = $_POST['txn_id'];
 $txn_type = $_POST['txn_type'];
 $payment_status = $_POST['payment_status'];
 $payment_type = $_POST['payment_type'];
 $payer_id = $_POST['payer_id'];
 $create_date = date('Y-m-d H:i:s');
 $payment_date = date('Y-m-d H:i:s');
 mysqli_query($con, "INSERT INTO paypal_transaction_tbl       (item_name,payer_email,first_name,last_name,amount,currency,country,txn_id,txn_type,payer_id,payment_status,payment_type,create_date,payment_date)
  VALUES   ('$item_name','$payer_email','$first_name','$last_name','$amount','$currency','$country','$txn_id','$txn_type','$payer_id','$payment_status','$payment_type','$create_date','$payment_date')");
 mysqli_close($con);
 }

 // Reply with an empty 200 response to indicate to paypal the IPN was     received correctly.
 header("HTTP/1.1 200 OK");
 catch (Exception $e) {
 error_log("There was a problem: ".$e->getMessage(),3,LOG_FILE);
 }

错误文件中没有显示错误,但数据库中没有任何内容存储,PayPal回复&#34;失败 - 重新发送&#34;在即时付款通知(IPN)历史记录中。数据库中没有存储新记录。关于它为什么失败的任何想法?

0 个答案:

没有答案