0

我无法在我的 joomla 1.5 网站上验证 paypal 付款。尽管付款成功,但我总是得到“无效”。我无法从贝宝获取 POST 值,只能获取值。这些代码或设置有什么问题。

我在 sandbox.paypal.com 中的贝宝设置

IPN : Turn On 
Message delivery : enabled
notification url : http://mysite.com/index.php?option=com_order&type=orders
auto return : on
return url : http://mysite.com/index.php?option=com_order&type=orders
PDT : on
Encrypted Website Payments : off
PayPal Account Optional : off

在 mycomponent joomla
payment.php

<form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr" name="paypal">
  <input type="hidden" value="_xclick" name="cmd">
  <input type="hidden" value="myname_1335697493_biz@gmail.com" name="business">
  <input type="hidden" value="test payment" name="item_name" id="item_name">
  <input type="hidden" value="11" name="item_number" id="item_number">
  <input type="hidden" value="0.1" name="amount" id="amount">
  <input type="hidden" value="USD" name="currency_code" id="currency_code">
  <input type="hidden" value="<?php echo JURI::base();?>index.php?option=com_order&type=orders" name="return" id="return">
  <input type="hidden" value="<?php echo JURI::base();?>index.php?option=com_order&type=orders" name="cancel_return" id="cancel_return">
  <input type="hidden" value="<?php echo JURI::base();?>index.php?option=com_order&task=orders" name="notify_url" id="notify_url">
  <input type="hidden" name="rm" value="2">
  <table class="tblpay">
  .....
  </table>
</form>

在我的 controller.php 上

function display()
{   
    $user =& JFactory::getUser();
    $type = JRequest::getVar('type');
    switch($type) {
        ...
            case 'orders':
            $viewName    = 'orders'; 
            $viewLayout  = 'orderslayout';
            if (JRequest::getVar('tx') != null){
                $this->processpayment();
                $viewLayout  = 'paymentlayout';
            }
            break;
        ...
}

function processpayment(){
    // Response from Paypal

    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    $get = JRequest::get('get');
    foreach ($get as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }
    // assign posted variables to local variables
    $data['amount']         = JRequest::getVar('amt');
    $data['currency']       = JRequest::getVar('cc');
    $data['cm']             = JRequest::getVar('cm');
    $data['idorder']        = JRequest::getVar('item_number');
    $data['st']             = JRequest::getVar('st');
    $data['tx']             = JRequest::getVar('tx');
    $data['option']         = JRequest::getVar('option');
    $data['type']           = JRequest::getVar('type');
    $data['paymentresult']  = "";

    // post back to PayPal system to validate
    $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 
    if (!$fp) {
        // HTTP ERROR
    } else {    

        fputs ($fp, $header . $req);
        while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp($res, "VERIFIED") == 0) {
                ...
            }else if (strcmp ($res, "INVALID") == 0) {
                ...
            }       
        }       
    fclose ($fp);
    }

    //$redirectTo = str_replace("amp;","",JRoute::_('index.php?option='.JRequest::getVar('option').'&type=orders&layout=paymentlayout')); 
    //$this->setRedirect($redirectTo, '');
}

这是我从贝宝得到的结果(使用 jdump):

[string] option = "com_order"
[string] type = "orders"
[string] tx = "9D9224627W344360N"
[string] st = "Completed"
[string] amt = "0.10"
[string] cc = "USD"
[string] cm = ""
[string] item_number = "41"
[string] Itemid = "" --> why i get this because i never send itemid?
4

1 回答 1

0

好的,我今天遇到了类似的问题,我认为通常这些“无效”响应通常是在提交的数据与收到的数据不完全相同的情况下。

对我来说,这是地址的问题,但对你来说,它可能是那个 Itemid。对于我的问题(我将在此处提及,因为它可能对其他人有所帮助),已要求贝宝发回用户地址。因为 paypal 允许街道地址为多行,所以它在地址的行之间添加了 \r\n。

通常最好的做法是使用这行来发送值......

$value = urlencode( stripslashes( $value ) );

但是如果您请求了一个地址,这将不起作用,因为它会从 \r\n 中删除斜杠,所以您只想做一个条件,以便如果键是 address_street 您只需 urlencode (或者只是以另一种方式获取地址我最终做了)

在您的情况下,您可以将上面的代码更改为

foreach ( $post as $key => $value ) {
        if ($key != 'Itemid')
        {
        $value = urlencode( stripslashes( $value ) );
        $req .= "&$key=$value";
        }
    }

希望能为你解决它;)

于 2012-05-01T17:04:23.557 回答