drbl91357 2014-09-16 15:58
浏览 97

Paypal将item_number从paypal传递到我的脚本

I am using Paypal Standard and I am attempting to get the item_number (it is unique in this use case) back from paypal after a successful transaction is completed. I can pass it to Paypal with no problem, I see it displayed during the transaction. I am using the following code when the transaction completes. Can someone show me what I need to do to display the item _number on the Success page - See success case below.

<?php
require_once("library.php"); // include the library file
define('EMAIL_ADD', 'me@gmail.com'); // define any notification email
define('PAYPAL_EMAIL_ADD', 'me-facilitator@gmail.com'); // facilitator email which will receive payments change this email to a live paypal account id when the site goes live
require_once("paypal_class.php");
    $p              = new paypal_class(); // paypal class
    $p->admin_mail  = EMAIL_ADD; // set notification email
$action         = $_REQUEST["action"];

switch($action){
case "process": // case process insert the form data in DB and process to the paypal
    mysql_query("INSERT INTO `purchases` (`invoice`, `product_id`, `product_name`, `product_quantity`, `product_amount`, `payer_fname`, `payer_lname`, `payer_address`, `payer_city`, `payer_state`, `payer_zip`, `payer_country`, `payer_email`, `payment_status`, `posted_date`) VALUES ('".$_POST["invoice"]."', '".$_POST["product_id"]."', '".$_POST["product_name"]."', '".$_POST["product_quantity"]."', '".$_POST["product_amount"]."', '".$_POST["payer_fname"]."', '".$_POST["payer_lname"]."', '".$_POST["payer_address"]."', '".$_POST["payer_city"]."', '".$_POST["payer_state"]."', '".$_POST["payer_zip"]."', '".$_POST["payer_country"]."', '".$_POST["payer_email"]."', 'pending', NOW())");
    $this_script = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    $p->add_field('business', PAYPAL_EMAIL_ADD); // Call the facilitator eaccount
    $p->add_field('cmd', $_POST["cmd"]); // cmd should be _cart for cart checkout
    $p->add_field('upload', '1');
    $p->add_field('return', $this_script.'?action=success'); // return URL after the transaction got over
    $p->add_field('cancel_return', $this_script.'?action=cancel'); // cancel URL if the trasaction was cancelled during half of the transaction
    $p->add_field('notify_url', $this_script.'?action=ipn'); // Notify URL which received IPN (Instant Payment Notification)
    $p->add_field('currency_code', $_POST["currency_code"]);
    $p->add_field('invoice', $_POST["invoice"]);
    $p->add_field('item_name_1', $_POST["product_name"]);
    $p->add_field('item_number_1', $_POST["product_id"]);
    $p->add_field('quantity_1', $_POST["product_quantity"]);
    $p->add_field('amount_1', $_POST["product_amount"]);
    $p->add_field('first_name', $_POST["payer_fname"]);
    $p->add_field('last_name', $_POST["payer_lname"]);
    $p->add_field('address1', $_POST["payer_address"]);
    $p->add_field('city', $_POST["payer_city"]);
    $p->add_field('state', $_POST["payer_state"]);
    $p->add_field('country', $_POST["payer_country"]);
    $p->add_field('zip', $_POST["payer_zip"]);
    $p->add_field('night_phone_b', $_POST["phone"]);
    $p->add_field('email', $_POST["payer_email"]);
    $p->submit_paypal_post(); // POST it to paypal
    //$p->dump_fields(); // Show the posted values for a reference, comment this line before app goes live
break;

case "success": // success case to show the user payment got success
    $i=$_GET['item_number'];
    echo '<title>Payment Done Successfully</title>';
    echo '<center><h1> Thank You for your purchase!</h1>';
    echo '<div class="as_wrapper">';
    echo "<h1>Click the image below to immediately download your stencil</h1>";
    echo '<br><br>';
    echo '<p>This file is a PDF. You will find it in your downloads folder. The filename will start with the word <b>stencil</b>. You can search for the word <b>stencil</b></p>';
    echo $i ; //Display item_number here
    echo '</center>';
break;

case "cancel": // case cancel to show user the transaction was cancelled
    echo "<h1>Transaction Cancelled";
break;

case "ipn": // IPN case to receive payment information. this case will not displayed in browser. This is server to server communication. PayPal will send the transactions each and every details to this case in secured POST menthod by server to server. 
    $trasaction_id  = $_POST["txn_id"];
    $payment_status = strtolower($_POST["payment_status"]);
    $invoice        = $_POST["invoice"];
    $log_array      = print_r($_POST, TRUE);
    $log_query      = "SELECT * FROM `paypal_log` WHERE `txn_id` = '$trasaction_id'";
    $log_check      = mysql_query($log_query);
    if(mysql_num_rows($log_check) <= 0){
        mysql_query("INSERT INTO `paypal_log` (`txn_id`, `log`, `posted_date`) VALUES ('$trasaction_id', '$log_array', NOW())");
    }else{
        mysql_query("UPDATE `paypal_log` SET `log` = '$log_array' WHERE `txn_id` = '$trasaction_id'");
    } // Save and update the logs array
    $paypal_log_fetch   = mysql_fetch_array(mysql_query($log_query));
    $paypal_log_id      = $paypal_log_fetch["id"];
    if ($p->validate_ipn()){ // validate the IPN, do the others stuffs here as per your app logic
        mysql_query("UPDATE `purchases` SET `trasaction_id` = '$trasaction_id ', `log_id` = '$paypal_log_id', `payment_status` = '$payment_status' WHERE `invoice` = '$invoice'");
        $subject = 'Instant Payment Notification - Recieved Payment';
        $p->send_report($subject); // Send the notification about the transaction
    }else{
        $subject = 'Instant Payment Notification - Payment Fail';
        $p->send_report($subject); // failed notification
    }
break;
}
?>
  • 写回答

1条回答 默认 最新

  • douque2016 2014-09-17 02:23
    关注

    I got your phone call and had in my notes to call you tomorrow. Then I just found this question here.

    I'm assuming you already have PDT enabled on your account with Auto-Return setup to point to your return URL..??

    With that assumption out of the way, you just need to adjust your $_GET['item_number']. PDT/IPN won't (typically) send it that way. You know how you're building it in your request as item_number_1, item_number_2, etc? You need to do the same thing when you pull it back out, except that PayPal decided to make things interesting and remove the second _ there. So what you'll use is $_GET['item_number1'], $_GET['item_number2'], etc.

    Of course, you would typically run through a loop of some sort to pick out all the items that might be on the order. Here's the way I've always pulled out item details in my IPN script (which will work the same for PDT)

    // Cart Items
    $num_cart_items = isset($_POST['num_cart_items']) ? $_POST['num_cart_items'] : '';
    
    $i = 1;
    $cart_items = array();   
    while(isset($_POST['item_number' . $i]))   
    {   
        $item_number = isset($_POST['item_number' . $i]) ? $_POST['item_number' . $i] : '';   
        $item_name = isset($_POST['item_name' . $i]) ? $_POST['item_name' . $i] : '';   
        $quantity = isset($_POST['quantity' . $i]) ? $_POST['quantity' . $i] : '';  
        $mc_gross = isset($_POST['mc_gross_' . $i]) ? $_POST['mc_gross_' . $i] : 0;
        $mc_handling = isset($_POST['mc_handling' . $i]) ? $_POST['mc_handling' . $i] : 0;
        $mc_shipping = isset($_POST['mc_shipping' . $i]) ? $_POST['mc_shipping' . $i] : 0;
        $custom = isset($_POST['custom' . $i]) ? $_POST['custom' . $i] : '';   
        $option_name1 = isset($_POST['option_name1_' . $i]) ? $_POST['option_name1_' . $i] : '';   
        $option_selection1 = isset($_POST['option_selection1_' . $i]) ? $_POST['option_selection1_' . $i] : '';   
        $option_name2 = isset($_POST['option_name2_' . $i]) ? $_POST['option_name2_' . $i] : '';   
        $option_selection2 = isset($_POST['option_selection2_' . $i]) ? $_POST['option_selection2_' . $i] : '';
        $option_name3 = isset($_POST['option_name3_' . $i]) ? $_POST['option_name3_' . $i] : '';   
        $option_selection3 = isset($_POST['option_selection3_' . $i]) ? $_POST['option_selection3_' . $i] : '';
        $option_name4 = isset($_POST['option_name4_' . $i]) ? $_POST['option_name4_' . $i] : '';   
        $option_selection4 = isset($_POST['option_selection4_' . $i]) ? $_POST['option_selection4_' . $i] : '';
        $option_name5 = isset($_POST['option_name5_' . $i]) ? $_POST['option_name5_' . $i] : '';   
        $option_selection5 = isset($_POST['option_selection5_' . $i]) ? $_POST['option_selection5_' . $i] : '';
        $option_name6 = isset($_POST['option_name6_' . $i]) ? $_POST['option_name6_' . $i] : '';   
        $option_selection6 = isset($_POST['option_selection6_' . $i]) ? $_POST['option_selection6_' . $i] : '';
        $option_name7 = isset($_POST['option_name7_' . $i]) ? $_POST['option_name7_' . $i] : '';   
        $option_selection7 = isset($_POST['option_selection7_' . $i]) ? $_POST['option_selection7_' . $i] : '';
        $option_name8 = isset($_POST['option_name8_' . $i]) ? $_POST['option_name8_' . $i] : '';   
        $option_selection8 = isset($_POST['option_selection8_' . $i]) ? $_POST['option_selection8_' . $i] : '';
        $option_name9 = isset($_POST['option_name9_' . $i]) ? $_POST['option_name9_' . $i] : '';   
        $option_selection9 = isset($_POST['option_selection9_' . $i]) ? $_POST['option_selection9_' . $i] : '';
    
        $btn_id = isset($_POST['btn_id' . $i]) ? $_POST['btn_id' . $i] : '';
    
        $current_item = array(   
                               'item_number' => $item_number,   
                               'item_name' => $item_name,   
                               'quantity' => $quantity, 
                               'mc_gross' => $mc_gross, 
                               'mc_handling' => $mc_handling, 
                               'mc_shipping' => $mc_shipping, 
                               'custom' => $custom,   
                               'option_name1' => $option_name1,   
                               'option_selection1' => $option_selection1,   
                               'option_name2' => $option_name2,   
                               'option_selection2' => $option_selection2, 
                               'option_name3' => $option_name3, 
                               'option_selection3' => $option_selection3, 
                               'option_name4' => $option_name4, 
                               'option_selection4' => $option_selection4, 
                               'option_name5' => $option_name5, 
                               'option_selection5' => $option_selection5, 
                               'option_name6' => $option_name6, 
                               'option_selection6' => $option_selection6, 
                               'option_name7' => $option_name7, 
                               'option_selection7' => $option_selection7, 
                               'option_name8' => $option_name8, 
                               'option_selection8' => $option_selection8, 
                               'option_name9' => $option_name9, 
                               'option_selection9' => $option_selection9, 
                               'btn_id' => $btn_id
                              );   
    
        array_push($cart_items, $current_item);   
        $i++;   
    }  
    

    That loads $cart_items with all of the different items, which you could then loop through to output individual parameters accordingly.

    评论

报告相同问题?

悬赏问题

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