Paypal IPN无法使用ngrok

时间:2017-04-23 19:35:40

标签: php paypal paypal-ipn

嘿伙计们我正在尝试整合支付网关Paypal IPN。我目前正在使用ngrok通过互联网运行我的本地主机,目前我正在http://229a1c8a.ngrok.io/sites/paypal6/进行测试。您可以打开链接并单击Buy Now按钮来测试付款。付款成功但没有记录插入数据库。成功页面中会给出一个记录列表,如果插入了任何记录,它将显示出来。目前它是空的。我尝试了很多,但它不起作用。我的代码如下。

的index.php

<!DOCTYPE html>
<html>
  <head>
    <title>Custom PayPal Test Integration</title>
    <meta charset="utf-8">
  </head>
  <body>
    <form action="https://sandbox.paypal.com/cgi-bin/webscr" method="post">
        <!-- Identify your business so that you can collect the payments. -->
        <input type="hidden" name="business" value="shubhamjha900@gmail.com">
        <!-- Specify a Buy Now button. -->
        <input type="hidden" name="cmd" value="_xclick">
        <!-- Specify details about the item that buyers will purchase. -->
        <input type="hidden" name="item_name" value="Test Item">
        <input type="hidden" name="item_number" value="12345">
        <input type="hidden" name="amount" value="5.50">
        <input type="hidden" name="currency_code" value="USD">
        <!-- Specify URLs -->
        <input type='hidden' name='cancel_return' value='http://229a1c8a.ngrok.io/sites/paypal6/failed.php'>
                <input type='hidden' name='return' value='http://229a1c8a.ngrok.io/sites/paypal6/success.php'>
                <input type='hidden' name='notify_url' value='http://229a1c8a.ngrok.io/sites/paypal6/ipn.php' />
        <!-- Display the payment button. -->
        <input type="image" name="submit" border="0"
        src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
        <img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
    </form>
  </body>
</html>

ipn.php

<?php
include'db.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://sandbox.paypal.com/cgi-bin/webscr'); // change to [...]sandbox.paypal[...] when using sandbox to test
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
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    if ($_POST['mc_gross'] != NULL)
        $payment_amount = $_POST['mc_gross'];
    else
        $payment_amount = $_POST['mc_gross1'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
    $custom = $_POST['custom'];

      $stmt = $pdo->prepare("INSERT INTO `payments`(`pay_record`)VALUES(1)");
    $stmt-> execute();
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}
?>

请帮帮我们。我无法解决这个问题。已经很久了。请帮我。上面给出的URL将不会存在太长时间。

0 个答案:

没有答案