从paypal沙箱到真正的“直播支付”

时间:2014-12-24 09:10:44

标签: php paypal

HÍ有这个代码,目前正在将付款人发送到沙盒测试帐户。但我需要它将付款人发送到真实的现场paypal.com

有人知道如何将其付诸实际付款。我试图删除“沙盒”(空)并插入“实时”。

Paypal给我这个错误:此交易已过期。请返回收件人的网站,使用常规结帐流程完成交易。

以下是代码:

<?php
        include_once("paypal.class.php");
        //$dbPayer = new Payer();
        $paypalmode = ($PayPalMode=='sandbox') ? '.sandbox' : '';

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

            $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   = 2.58;  //Sum of tax for all items in this order.
            $HandalingCost      = 2.00;  //Handling cost for this order.
            $InsuranceCost      = 1.00;  //shipping insurance cost for this order.
            $ShippinDiscount    = -3.00; //Shipping discount for this order. Specify this as negative number.
            $ShippinCost        = 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);
            //if(isset($_POST['ischecked']) and $_POST['ischecked'] == 'on')
            $ShippinCost = $_POST['frightprice'];
            //else
              //  $ShippinCost = 0;
            $GrandTotal = ($ItemTotalPrice + $ShippinCost);
            $customs = $_POST['Name'].'|'.$_POST['Email'].'|'.$_POST['Phone'].'|'.$_POST['address'].'|'.$ItemName.'|'.$ItemPrice.'|'.$ItemNumber.'|'.$ItemQty.'|'.$ShippinCost.'|'.$ItemTotalPrice.'|'.$GrandTotal;
            //Parameters for SetExpressCheckout, which will be sent to PayPal
            $padata =   '&METHOD=SetExpressCheckout'.
                '&RETURNURL='.urlencode($PayPalReturnURL ).
                '&CANCELURL='.urlencode($PayPalCancelURL).
                '&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode("SALE").

                '&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).

                '&NOSHIPPING=0'. //set 1 to hide buyer's shipping address, in-case products that does not require shipping

                '&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
                '&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($ShippinCost).
                '&PAYMENTREQUEST_0_AMT='.urlencode($GrandTotal).
                '&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode).
                '&PAYMENTREQUEST_0_ADDRESSNORMALIZATIONSTATUS ='.urlencode($customs).
                '&LOCALECODE=da_DK'. //PayPal pages to match the language on your website.
                '&LOGOIMG=http://au-aktioner.com/images/7.png'. //site logo
                '&CARTBORDERCOLOR=FFFFFF'. //border color of cart
                '&ALLOWNOTE=1';

            ############# 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['TotalTaxAmount']   =  $TotalTaxAmount;  //Sum of tax for all items in this order.
            $_SESSION['HandalingCost']      =  $HandalingCost;  //Handling cost for this order.
            $_SESSION['InsuranceCost']      =  $InsuranceCost;  //shipping insurance cost for this order.
            $_SESSION['ShippinDiscount']    =  $ShippinDiscount; //Shipping discount for this order. Specify this as negative number.
            $_SESSION['ShippinCost']        =   $ShippinCost; //Although you may change the value later, try to pass in a shipping amount that is reasonably accurate.*/
            $_SESSION['ShippinCost']        =   $ShippinCost;
            $_SESSION['GrandTotal']         =   $GrandTotal;
            $_SESSION['customs'] = $customs;


            //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;
          /*  $TotalTaxAmount   = $_SESSION['TotalTaxAmount'] ;  //Sum of tax for all items in this order.
            $HandalingCost      = $_SESSION['HandalingCost'];  //Handling cost for this order.
            $InsuranceCost      = $_SESSION['InsuranceCost'];  //shipping insurance cost for this order.
            $ShippinDiscount    = $_SESSION['ShippinDiscount']; //Shipping discount for this order. Specify this as negative number.
            $ShippinCost        = $_SESSION['ShippinCost']; //Although you may change the value later, try to pass in a shipping amount that is reasonably accurate.*/
            $ShippinCost        = $_SESSION['ShippinCost'];
            $GrandTotal         = $_SESSION['GrandTotal'];
            $customs        = $_SESSION['customs'];

            $padata =   '&TOKEN='.urlencode($token).
                '&PAYERID='.urlencode($payer_id).
                '&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode("SALE").

                //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_TAXAMT='.urlencode($TotalTaxAmount).
                '&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($ShippinCost).
                '&PAYMENTREQUEST_0_HANDLINGAMT='.urlencode($HandalingCost).
                '&PAYMENTREQUEST_0_SHIPDISCAMT='.urlencode($ShippinDiscount).
                '&PAYMENTREQUEST_0_INSURANCEAMT='.urlencode($InsuranceCost).*/
                '&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($ShippinCost).
                '&PAYMENTREQUEST_0_ADDRESSNORMALIZATIONSTATUS ='.urlencode($customs).
                '&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>';
                $_SESSION['payer_id'] = urldecode($httpParsedResponseAr["PAYMENTINFO_0_TRANSACTIONID"]);
                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" >Betaling modtaget - Dine vare vil blive sendt til din adresse!</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>';
                }

                // 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 ###




                    */

                    $data = json_encode($httpParsedResponseAr);
                    $mysqli = new mysqli('au-aktioner.com.mysql','au_aktioner_com','','au_aktioner_com');
                   // $mysqli = new mysqli('localhost','root','','auctionsales');

                    //Output any connection error
                    if ($mysqli->connect_error) {
                        die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
                    }
                    $query = "INSERT INTO tblPayPal_Ipn_Details (ieverything_else) values ('$data')";
                    $insert_row = $mysqli->query($query);

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


                }
                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>';
            }
        }
        ?>

0 个答案:

没有答案