在TotalAmount上的PHP PayPal结帐错误

时间:2017-10-15 21:04:39

标签: php paypal

我已经设法将PayPal快速结账功能整合到我的电子商务商店中,然后搜索并跟进教程,一切正常,直到我真正尝试付费。它返回以下错误:

  

警告:/home2/joso157/public_html/jssite/shop/paypal-express-checkout/index.php中为foreach()提供的参数无效   第139行

     

错误:OrderTotal(Amt):缺少必需参数

任何人都知道如何解决这个问题?提前致谢

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

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

if(isset($_SESSION["cart_products"]))
 {

$paypal_data ='';
$ItemTotalPrice = 0;
$productWght = "0";
$i = 0;
    foreach ($_SESSION["cart_products"] as $cart_itm)
    {
    $product_code   = filter_var($cart_itm["product_code"], FILTER_SANITIZE_STRING); 

    $results = $mysqli->query("SELECT product_name, product_desc, price_1, price_2, price_3, price_4, weight_1, weight_2, weight_3, weight_4 FROM products INNER JOIN weight ON (products.weight = weight.id) WHERE product_code='$product_code' LIMIT 1");
    $obj = $results->fetch_object();

    $paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$i.'='.urlencode($obj->product_name);
    $paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$i.'='.urlencode($cart_itm["product_code"]);
    $paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$i.'='.urlencode($cart_itm["price"]);     
    $paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$i.'='. urlencode($cart_itm["product_qty"]);
    $paypal_data .= '&L_PAYMENTREQUEST_0_WEIGHT'.$i.'='. urlencode($cart_itm["weight"]);    

    $subtotal = ($cart_itm["price"]*$cart_itm["product_qty"]);

    $ItemTotalPrice = $ItemTotalPrice + $subtotal;

    $paypal_product['items'][] = array('itm_name'=>$obj->product_name,
                                        'itm_price'=>$cart_itm["price"],
                                        'itm_code'=>$cart_itm["product_code"], 
                                        'itm_qty'=>$cart_itm["product_qty"],
                                        );
    $productWght += $cart_itm["product_qty"]*$cart_itm["weight"];

    $i++;
}
$k = $i;
$total_tax = 0; 
foreach($taxes as $key => $value){ 
        $tax_amount     = round($ItemTotalPrice * ($value / 100));
        $tax_item[$key] = $tax_amount;
        $total_tax = $total_tax + $tax_amount; 
}

$product_weight = ($product_qty*$cart_itm["weight"]);
$total_weight += $product_weight;
$total_weight = $productWght;

$shipping_cost = '0';
if($total_weight <= '1.9'){
  $shipping_cost = "2.95";
}elseif(($total_weight >= '1.9') && ($total_weight <= '11')){
  $shipping_cost = "5.95";
}elseif(($total_weight >= '11') && ($total_weight <= '30')){
  $shipping_cost = "9.95";
}elseif(($total_weight >= '30') && ($total_weight <= '100')){
  $shipping_cost = "24.95";

}

$GrandTotal = ($ItemTotalPrice + $total_tax + $HandalingCost + $InsuranceCost + $shipping_cost);

$discount = '0';
if(isset($_SESSION['discount_amount']) && !empty($_SESSION['discount_amount']))
{
        $GrandTotal = $GrandTotal - $_SESSION['discount_amount'];
        $GrandTotal = sprintf("%01.2f", $GrandTotal);
        $discount = ($_SESSION['discount_amount'] != '0') ? "-".$_SESSION['discount_amount'] : "0";
}

$paypal_product['assets'] = array('tax_total'=>$total_tax, 
                            'handaling_cost'=>$HandalingCost, 
                            'insurance_cost'=>$InsuranceCost,
                            'shippin_cost'=>$shipping_cost,
                            'discount'=>$discount,
                            'grand_total'=>$GrandTotal);

$_SESSION["paypal_products"] = $paypal_product;


/*$paypal_data .= "&L_PAYMENTREQUEST_0_NAME".$k."=Discount&L_PAYMENTREQUEST_0_NUMBER".$k."=1&L_PAYMENTREQUEST_0_AMT".$k."=".$discount."&L_PAYMENTREQUEST_0_QTY".$k."=1&L_PAYMENTREQUEST_0_WEIGHT".$k."=0";*/

$padata =   '&METHOD=SetExpressCheckout'.
            '&RETURNURL='.urlencode($PayPalReturnURL ).
            '&CANCELURL='.urlencode($PayPalCancelURL).
            '&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode("SALE").
            $paypal_data.               
            '&NOSHIPPING=0'. 
            '&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
            '&PAYMENTREQUEST_0_TAXAMT='.urlencode($total_tax).
            '&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($shipping_cost).
            '&PAYMENTREQUEST_0_HANDLINGAMT='.urlencode($HandalingCost).
            '&PAYMENTREQUEST_0_SHIPDISCAMT='.urlencode($discount).
            '&PAYMENTREQUEST_0_INSURANCEAMT='.urlencode($InsuranceCost).
            '&PAYMENTREQUEST_0_AMT='.urlencode($GrandTotal).
            '&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode).
            '&LOCALECODE=GB'. 
            '&LOGOIMG=http://www.sanwebe.com/wp-content/themes/sanwebe/img/logo.png'.
            '&CARTBORDERCOLOR=FFFFFF'. 
            '&ALLOWNOTE=1';


    $paypal= new MyPayPal();
    $httpParsedResponseAr = $paypal->PPHttpPost('SetExpressCheckout', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);


    if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
    {
            unset($_SESSION["cart_products"]); 

            $paypalurl ='https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$httpParsedResponseAr["TOKEN"].'';
            header('Location: '.$paypalurl);
    }
    else
    {

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

}

if(isset($_GET["token"]) && isset($_GET["PayerID"]))
{

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

$paypal_product = $_SESSION["paypal_products"];
$paypal_data = '';
$ItemTotalPrice = 0;
    $z = '0';
foreach($paypal_product['items'] as $key=>$p_item)
{       
    $paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$key.'='. urlencode($p_item['itm_qty']);
    $paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$key.'='.urlencode($p_item['itm_price']);
    $paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$key.'='.urlencode($p_item['itm_name']);
    $paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$key.'='.urlencode($p_item['itm_code']);

    $subtotal = ($p_item['itm_price']*$p_item['itm_qty']);
    $ItemTotalPrice = ($ItemTotalPrice + $subtotal);

    $z = $key;
}
/*$z = $z+1;
$paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$z.'='. urlencode("1");
$paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$z.'='.urlencode($discount);
$paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$z.'='.urlencode("Discount Amount");
$paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$z.'='.urlencode('1');*/

$padata =   '&TOKEN='.urlencode($token).
            '&PAYERID='.urlencode($payer_id).
            '&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode("SALE").
            $paypal_data.
            '&PAYMENTREQUEST_0_ITEMAMT='.urlencode($ItemTotalPrice).
            '&PAYMENTREQUEST_0_TAXAMT='.urlencode($paypal_product['assets']['tax_total']).
            '&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($paypal_product['assets']['shippin_cost']).
            '&PAYMENTREQUEST_0_HANDLINGAMT='.urlencode($paypal_product['assets']['handaling_cost']).
            '&PAYMENTREQUEST_0_SHIPDISCAMT='.urlencode($paypal_product['assets']['discount']).
            '&PAYMENTREQUEST_0_INSURANCEAMT='.urlencode($paypal_product['assets']['insurance_cost']).
            '&PAYMENTREQUEST_0_AMT='.urlencode($paypal_product['assets']['grand_total']).
            '&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode);

$paypal= new MyPayPal();
$httpParsedResponseAr = $paypal->PPHttpPost('DoExpressCheckoutPayment', $padata, $PayPalApiUsername, $PayPalApiPassword, $PayPalApiSignature, $PayPalMode);

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

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

            if('Completed' == $httpParsedResponseAr["PAYMENTINFO_0_PAYMENTSTATUS"])
            {
                echo '<div style="color:green">Payment Received! Your product will be sent to you very soon!</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>';
            }

            $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 />';

                echo '<pre>';

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

1 个答案:

答案 0 :(得分:0)

根据您的代码,错误在于:

$paypal_product = $_SESSION["paypal_products"];
$paypal_data = '';
$ItemTotalPrice = 0;
    $z = '0';
foreach($paypal_product['items'] as $key=>$p_item)
{       
    $paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$key.'='. urlencode($p_item['itm_qty']);
    $paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$key.'='.urlencode($p_item['itm_price']);
    $paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$key.'='.urlencode($p_item['itm_name']);
    $paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$key.'='.urlencode($p_item['itm_code']);

    $subtotal = ($p_item['itm_price']*$p_item['itm_qty']);
    $ItemTotalPrice = ($ItemTotalPrice + $subtotal);

    $z = $key;
}

该错误表示传递给$paypal_product['items']的{​​{1}}不是数组。这可能意味着会话数据为空或不包含您期望的详细信息。

这似乎填充在这里:

foreach()

如果我们创建此数据的示例,它可能如下所示:

$paypal_product['items'][] = array('itm_name'=>$obj->product_name,
                                    'itm_price'=>$cart_itm["price"],
                                    'itm_code'=>$cart_itm["product_code"], 
                                    'itm_qty'=>$cart_itm["product_qty"],
                                    );

然后将其传递给会话:

$paypal_product['items'] = array(
  0 => array(
    'itm_name'=>"Product 1",
    'itm_price'=>123.45,
    'itm_code'=>6789, 
    'itm_qty'=>1
  ),
  1 => array(
    'itm_name'=>"Product 2",
    'itm_price'=>9.87,
    'itm_code'=>6790, 
    'itm_qty'=>100
  )
);

我建议正确填充$_SESSION["paypal_products"] = $paypal_product; 。您可以在使用$paypal_product['items']后检查它,以确保至少有1个数组元素。