dongyukang7006 2012-11-13 03:58
浏览 38
已采纳

使用PHP中的PayPal IPN抓取自定义字段

So on my website, people are able to order upgrades by clicking "Pay Now" for the upgrade they want and by entering their username in the box. Up until now, I would then go in to PayPal, see the payment, and look at the Minecraft Username field, and upgrade it by hand. Now I want to start using IPN so that I can automate all of the work that i've been doing.

Here is the HTML code for a upgrade option:

<div class=donationBox>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="W2RH7VA3YS3Y4">
<table>
<tr>
<td width=75%><h2>Custom Maps - $5.00</h2></td>
<td><input type="hidden" name="on0" value="Minecraft Username">Minecraft Username<input type="text" name="os0" maxlength="200"></td>
</tr>
<tr>

<td>The ability to upload any map you want to play on! Maybe a survival island map? You bet! Or the Hunger Games!</td>
<td><input type="submit" class="btn btn-primary" value="Buy Now" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"></td>
</tr>
</table>
</form>
</div>

And here is the PHP code to simply send me an Email with the info I need to try to get it working:

  <?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
  $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'];
  $custom = $_POST['on0'];
  $message = "An order has been IPNified! " . $item_name . " " . $item_number . " " . $payment_amount . " " . $txn_id . " " . $custom;
  mail('myemail@gmail.com', 'PayPal IPN', $message);
  } else if (strcmp ($res, "INVALID") == 0) {
      mail('myemail@gmail.com', 'PayPal IPN', 'Errors');
  }
  ?>

The problem that I'm having is here:

$custom = $_POST['on0'];

I tried it as the above and:

$custom = $_POST['custom'];

The first one because that's the name of the Minecraft Username field, and the second I tried because I read that's what it should be, but either way it just didn't return anything. Any help would be great! Thanks!

  • 写回答

2条回答 默认 最新

  • duanpendan8067 2012-11-13 04:04
    关注

    Dump the results of $_POST and see which key fits the values you're looking for. Use either var_dump or print_r

    // Displays more detail such as value types
    var_dump($_POST);
    
    // Only displays key => value relationships of an array
    print_r($_POST);
    

    EDIT

    I just realized it's not possible to see the output of this using ipn. In this case you can always serialize the entire $_POST array and send it in an email.

    $post_data_string = serialize($_POST);
    mail('myemail@gmail.com', 'PayPal IPN', $post_data_string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加