2014-02-22 46 views
0

我正在爲我的第一個PayPal快速付款作爲學習目的在個人網站上工作,我發現了一個很好的在線教程,介紹瞭如何構建快速結帳。我正在使用本教程中提供的示例頁面以獲得更好的理解,但我收到一個錯誤:PaymentAction:Required參數丟失,我似乎無法跟蹤此問題。收到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 [email protected] 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 
) 

回答

0

Obvoiusly的PAYMENTREQUEST_0_PAYMENTACTION沒有在SetExpress Checkout或DoExpress結帳API調用傳遞。 嘗試打印$ paymentType並查看變量的範圍是否存在於內部(如您所說的$ paymentType是在配置文件中聲明的)。 或 在此文件本身內部聲明$ paymentType ='Sale'(僅用於交叉檢查)。

+0

我輸入了單詞sale而不是變量,沒有工作。 我已經在理智的文件中聲明瞭它,沒有工作。 當我打印$ paymentType時,它顯示爲Sale。 我不再需要這個答案,因爲我發現了一個更好的教程,教會如何自己編寫代碼。 – user3340895

0

我花了差不多4個小時,發現在貝寶返回到返回網址時得到令牌後,會話被破壞。

所以你得到這個錯誤。

分辨率:我用http://www.your-website.com作爲返回URL,而不是http://your-website.com

問題被成功解決。