无法使paypal ipn工作

时间:2016-12-20 16:42:19

标签: php paypal paypal-ipn

我正在尝试使用paypal在我的网站上捐款。结果,我希望捐赠者显示出来。为此,我使用ipn。我认为并使其工作,但它似乎只适用于paypal沙箱。

这是我的代码: page.php文件

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="frmPayPal1">
    <input type="hidden" name="business" value="<?php echo $merchant_id; ?>">
    <input type="hidden" name="cmd" value="_donations">
    <input type="hidden" name="item_name" value="Donation">
    <input type="hidden" name="item_number" value="1">
    <input type="hidden" name="credits" value="510">
    <input type="hidden" name="userid" value="1">
    <input type="hidden" name="no_shipping" value="1">
    <input type="hidden" name="currency_code" value="EUR">
    <input type="hidden" name="handling" value="0">
    <input type="hidden" name="cancel_return" value="http://domain.com/donation">
    <input type="hidden" name="return" value="http://domain.com/donation">
    <div class = "donator_name_choice">
        Enter the name you want to be displayed as: <input type="text" class = "donate_amount" name="custom" maxlength="14">
    </div>
    <div class = "donator_donation">
        Enter the value You want to donate(EUR): <input type="number" class = "donate_amount" name="amount" step = "0.01">
    </div>
    <div class = "donation_submit">
    <input type="submit" name="submit" value = "DOTANE" class = "donate_here">
</div>
</form>

ipnlistener.php:

<?php
// STEP 1: Read POST data

// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST
// reading raw POST data from input stream instead. 
$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)
     $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
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";
}


// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
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_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment

    // assign posted variables to local variables
    $user_name = $_POST['custom'];
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];


    $payment_amount = $_POST['mc_gross'];


    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
    $payment_date = $_POST['payment_date'];
    $item_name = $_POST['item_name'];

    $sql162 = "INSERT INTO donators (user_name, receiver_email, payer_email, txn_id, payment_gross, currency_code, payment_status, payment_date) VALUES ('" . $user_name . "', '". $receiver_email ."', '" . $payer_email . "', '" . $txn_id . "', '" . $payment_amount . "', '" . $payment_currency . "', '" . $payment_status . "', '" . $payment_date . "')";

    if ($MySQLi_CON->query($sql162) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql162 . "<br>" . $MySQLi_CON->error;
    }

} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}
?>

有人能解释一下我错过了什么吗?谢谢!

0 个答案:

没有答案