收到PaymentAction错误

时间:2014-02-22 14:52:12

标签: php paypal express-checkout

我正在将我的第一张PayPal快递结账作为个人网站上的学习目的,我在网上发现了一个很好的教程,解释了如何建立快速结账。我正在使用本教程中提供的示例页面以便更好地理解,但我收到一个错误:PaymentAction:缺少必需参数,我似乎无法追查问题。

所以我理解我必须通过PaymentAction参数发送,我将其作为销售发送。我不明白为什么它说我不发送它。我希望有人可以在编码中发现错误,或者对导致此问题的原因提出建议。

$ paymentType存储在我的config.php中,以及下面代码中使用的一些其他变量。

这是我的$ paymentType在我的config.php中的样子:

$ paymentType ='促销'; //付款方式

<?php
session_start();
include_once("config.php");
include_once("paypal.class.php");
include ("../inc/settings.php");





$paypalmode = ($PayPalMode=='sandbox') ? '.sandbox' : '';

if($_POST) //Post Data received from product list page.
{


    //Mainly we need 4 variables from product page Item Name, Item Price, Item Number and Item Quantity.

    //Please Note : People can manipulate hidden field amounts in form,
    //In practical world you must fetch actual price from database using item id. Eg: 
    //$ItemPrice = $mysqli->query("SELECT item_price FROM products WHERE id = Product_Number");

    // Connect to DB
    mysql_connect($server, $user, $pass) or die(mysql_error());
    mysql_select_db($name) or die(mysql_error());

    // Get the item price from the database
    $result = mysql_query("SELECT * FROM admin_settings");
    while($row = mysql_fetch_array( $result )) {
        // Check how many credits they are buying
        if ($_POST["itemnumber"] == 5) {
            $ItemPrice = $row['five_credits'];  
        } else if ($_POST["itemnumber"] == 10) {
            $ItemPrice = $row['ten_credits'];   
        } else if ($_POST["itemnumber"] == 20) {
            $ItemPrice = $row['twenty_credits'];    
        } else if ($_POST["itemnumber"] == 30) {
            $ItemPrice = $row['thirty_credits'];    
        }
    }// end while

    $ItemName       = $_POST["itemname"]; //Item Name
    //$ItemPrice    = $_POST["itemprice"]; //Item Price
    $ItemNumber     = $_POST["itemnumber"]; //Item Number
    $ItemDesc       = $_POST["itemdesc"]; //Item Number
    $ItemQty        = $_POST["itemQty"]; // Item Quantity
    $ItemTotalPrice = ($ItemPrice*$ItemQty); //(Item Price x Quantity = Total) Get total amount of product; 

    //Other important variables like tax, shipping cost
    $TotalTaxAmount     = 0; //2.58;  //Sum of tax for all items in this order. 
    $HandalingCost  = 0; //2.00;  //Handling cost for this order.
    $InsuranceCost  = 0; //1.00;  //shipping insurance cost for this order.
    $ShippinDiscount    = 0; //-3.00; //Shipping discount for this order. Specify this as negative number.
    $ShippinCost        = 0; //3.00; //Although you may change the value later, try to pass in a shipping amount that is reasonably accurate.

    //Grand total including all tax, insurance, shipping cost and discount
    // $GrandTotal = ($ItemTotalPrice + $TotalTaxAmount + $HandalingCost + $InsuranceCost + $ShippinCost + $ShippinDiscount);
    $GrandTotal = $ItemTotalPrice;

    //Parameters for SetExpressCheckout, which will be sent to PayPal
    $padata =   '&METHOD=SetExpressCheckout'.
                '&RETURNURL='.urlencode($PayPalReturnURL ).
                '&CANCELURL='.urlencode($PayPalCancelURL).
                '&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode($paymentType).
                '&L_PAYMENTREQUEST_0_NUMBER0='.urlencode($ItemNumber).
                '&L_PAYMENTREQUEST_0_DESC0='.urlencode($ItemDesc).
                '&L_PAYMENTREQUEST_0_AMT0='.urlencode($ItemPrice).
                '&L_PAYMENTREQUEST_0_QTY0='. urlencode($ItemQty).
                '&NOSHIPPING=1'. //set 1 to hide buyer's shipping address, in-case products that does not require shipping
                '&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
                '&PAYMENTREQUEST_0_AMT='.urlencode($GrandTotal).
                '&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode).
                '&LOCALECODE=US'. //PayPal pages to match the language on your website.
                '&LOGOIMG=http://www.sanwebe.com/wp-content/themes/sanwebe/img/logo.png'. //site logo
                '&CARTBORDERCOLOR=FFFFFF'. //border color of cart
                '&ALLOWNOTE=0';



                ############# set session variable we need later for "DoExpressCheckoutPayment" #######
                $_SESSION['ItemName']           =  $ItemName; //Item Name
                $_SESSION['ItemPrice']          =  $ItemPrice; //Item Price
                $_SESSION['ItemNumber']         =  $ItemNumber; //Item Number
                $_SESSION['ItemDesc']           =  $ItemDesc; //Item Number
                $_SESSION['ItemQty']            =  $ItemQty; // Item Quantity
                $_SESSION['ItemTotalPrice']     =  $ItemTotalPrice; //(Item Price x Quantity = Total) Get total amount of product; 
                $_SESSION['GrandTotal']         =  $GrandTotal;


        //We need to execute the "SetExpressCheckOut" method to obtain paypal token
        $paypal= new MyPayPal();
        $httpParsedResponseAr = $paypal->PPHttpPost('SetExpressCheckout', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);

        //Respond according to message we receive from Paypal
        if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
        {

                //Redirect user to PayPal store with Token received.
                $paypalurl ='https://www'.$paypalmode.'.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$httpParsedResponseAr["TOKEN"].'';
                header('Location: '.$paypalurl);

        }else{
            //Show error message
            echo '<div style="color:red"><b>Error : </b>'.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
            echo '<pre>';
            print_r($httpParsedResponseAr);
            echo '</pre>';
        }

}

//Paypal redirects back to this page using ReturnURL, We should receive TOKEN and Payer ID
if(isset($_GET["token"]) && isset($_GET["PayerID"]))
{
    //we will be using these two variables to execute the "DoExpressCheckoutPayment"
    //Note: we haven't received any payment yet.

    $token = $_GET["token"];
    $payer_id = $_GET["PayerID"];

    //get session variables
    $ItemName           = $_SESSION['ItemName']; //Item Name
    $ItemPrice      = $_SESSION['ItemPrice'] ; //Item Price
    $ItemNumber         = $_SESSION['ItemNumber']; //Item Number
    $ItemDesc           = $_SESSION['ItemDesc']; //Item Number
    $ItemQty            = $_SESSION['ItemQty']; // Item Quantity
    $ItemTotalPrice     = $_SESSION['ItemTotalPrice']; //(Item Price x Quantity = Total) Get total amount of product; 
    $GrandTotal         = $_SESSION['GrandTotal'];

    $padata =   '&TOKEN='.urlencode($token).
                '&PAYERID='.urlencode($payer_id).
                '&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode($paymentType).
                //set item info here, otherwise we won't see product details later  
                '&L_PAYMENTREQUEST_0_NAME0='.urlencode($ItemName).
                '&L_PAYMENTREQUEST_0_NUMBER0='.urlencode($ItemNumber).
                '&L_PAYMENTREQUEST_0_DESC0='.urlencode($ItemDesc).
                '&L_PAYMENTREQUEST_0_AMT0='.urlencode($ItemPrice).
                '&L_PAYMENTREQUEST_0_QTY0='. urlencode($ItemQty).
                '&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
                '&PAYMENTREQUEST_0_AMT='.urlencode($GrandTotal).
                '&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode);

    //We need to execute the "DoExpressCheckoutPayment" at this point to Receive payment from user.
    $paypal= new MyPayPal();
    $httpParsedResponseAr = $paypal->PPHttpPost('DoExpressCheckoutPayment', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);

    //Check if everything went ok..
    if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) 
    {

            echo '<h2>Success</h2>';
            echo 'Your Transaction ID : '.urldecode($httpParsedResponseAr["PAYMENTINFO_0_TRANSACTIONID"]);

                /*
                //Sometimes Payment are kept pending even when transaction is complete. 
                //hence we need to notify user about it and ask him manually approve the transiction
                */

                if('Completed' == $httpParsedResponseAr["PAYMENTINFO_0_PAYMENTSTATUS"])
                {
                    echo '<div style="color:green">Payment Received! Your product will be sent to you very soon!</div>';
                    echo '<div style="color:green">Add the code here to add credits to the user\'s account.</div>';
                }
                elseif('Pending' == $httpParsedResponseAr["PAYMENTINFO_0_PAYMENTSTATUS"])
                {
                    echo '<div style="color:red">Transaction Complete, but payment is still pending! '.
                    'You need to manually authorize this payment in your <a target="_new" href="http://www.paypal.com">Paypal Account</a></div>';
                    echo '<div style="color:red">Once you have authorized your payment manually, you will need to contact me via admin@etseo.net to apply the credits to your account. Due to your payment being blocked until authorized, credits can not be added automatically.</div>';
                }

                // we can retrive transection details using either GetTransactionDetails or GetExpressCheckoutDetails
                // GetTransactionDetails requires a Transaction ID, and GetExpressCheckoutDetails requires Token returned by SetExpressCheckOut
                $padata =   '&TOKEN='.urlencode($token);
                $paypal= new MyPayPal();
                $httpParsedResponseAr = $paypal->PPHttpPost('GetExpressCheckoutDetails', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);

                if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) 
                {

                    echo '<br /><b>Stuff to store in database :</b><br /><pre>';
                    /*
                    #### SAVE BUYER INFORMATION IN DATABASE ###
                    //see (http://www.sanwebe.com/2013/03/basic-php-mysqli-usage) for mysqli usage

                    $buyerName = $httpParsedResponseAr["FIRSTNAME"].' '.$httpParsedResponseAr["LASTNAME"];
                    $buyerEmail = $httpParsedResponseAr["EMAIL"];

                    //Open a new connection to the MySQL server
                    $mysqli = new mysqli('host','username','password','database_name');

                    //Output any connection error
                    if ($mysqli->connect_error) {
                        die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
                    }       

                    $insert_row = $mysqli->query("INSERT INTO BuyerTable 
                    (BuyerName,BuyerEmail,TransactionID,ItemName,ItemNumber, ItemAmount,ItemQTY)
                    VALUES ('$buyerName','$buyerEmail','$transactionID','$ItemName',$ItemNumber, $ItemTotalPrice,$ItemQTY)");

                    if($insert_row){
                        print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />'; 
                    }else{
                        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
                    }

                    */

                    echo '<pre>';
                    print_r($httpParsedResponseAr);
                    echo '</pre>';
                } else  {
                    echo '<div style="color:red"><b>GetTransactionDetails failed:</b>'.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
                    echo '<pre>';
                    print_r($httpParsedResponseAr);
                    echo '</pre>';

                }

    }else{
            echo '<div style="color:red"><b>Error : </b>'.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
            echo '<pre>';
            print_r($httpParsedResponseAr);
            echo '</pre>';
    }
}
?>

它从我的网站到PayPal。我输入了我的PayPal凭证,允许付款,然后它将我发回我的网站。当它发回给我的时候是它显示错误。

这是确切的错误

Error : PaymentAction : Required parameter missing
Array
(
    [TIMESTAMP] => 2014%2d02%2d22T14%3a41%3a42Z
    [CORRELATIONID] => 57246c353bd81
    [ACK] => Failure
    [VERSION] => 109%2e0
    [BUILD] => 9720069
    [L_ERRORCODE0] => 81115
    [L_SHORTMESSAGE0] => Missing%20Parameter
    [L_LONGMESSAGE0] => PaymentAction%20%3a%20Required%20parameter%20missing
    [L_SEVERITYCODE0] => Error
)

2 个答案:

答案 0 :(得分:0)

很可能PAYMENTREQUEST_0_PAYMENTACTION未在SetExpress Checkout或DoExpress Checkout API调用中传递。 尝试打印$ paymentType并查看变量的范围是否存在于内部(正如您所说的$ paymentType在配置文件中声明)。 要么 在此文件中声明$ paymentType ='Sale'(仅用于交叉检查)。

答案 1 :(得分:0)

我花了差不多4个小时,发现在获得令牌后,当paypal返回到返回网址时,会话被破坏。

所以你得到这个错误。

解决方案:我使用http://www.your-website.com作为返回网址,而不是http://your-website.com

问题已成功解决。