无效响应paypal IPN与cakephp

时间:2013-11-30 15:58:04

标签: php cakephp paypal paypal-ipn

我有2个Paypal IPN问题:

1:paypal不会将响应发送到通知网址(在变量notify_url中),它只发送到返回网址,即使我更改了paypal沙箱配置中的通知网址。

2:我有来自paypal的INVALID响应,这是调试的返回($ res):

2013-11-30 16:33:34 Paypal: HTTP/1.1 200 OK

2013-11-30 16:33:34 Paypal: Date: Sat, 30 Nov 2013 15:33:33 GMT

2013-11-30 16:33:34 Paypal: Server: Apache

2013-11-30 16:33:34 Paypal: X-Frame-Options: SAMEORIGIN

2013-11-30 16:33:34 Paypal: Set-Cookie: 
c9MWDuvPtT9GIMyPc3jwol1VSlO=shzKxbnSzP95bjUA6JI1vfJ6avweTY7Y1emqMfI4pIhF0-q9oxObL5MiA1cCXnpE-jBFuLnk89t49vcYT-swZgYtmEanF7FGU7CKLYojFcHtDZfYmTbsMLMRxQhjfezCOrHEKd2-vS-3811JeYqaC2xqTEPuYV2inOzfHQJl7D7HwvG1fvePpgiCQe_XFxmx7ps5da9QREf4K8uzQ8bndygEiLWM6XQCfF8gA7jn_6qy3mqes36Qs5OunCp8Q9Z1qF_GgcXYd692opT7oflldeHgnRucy2GW2RKMk3431n_oqJe5JpKIdua7xHwO1R1RPyKahX2TPwpkK9TbI7FQns3ZqDoqxhmsAESCYjPuPQrPD2Ugj87V2Nx3D8JVFsVGzEwOdQ3RfUKp_oRfeM-F5CACP19VqQoaeQIo4SGsqsW9V7IX37fR5AQFaeG; domain=.paypal.com; path=/; Secure; HttpOnly

2013-11-30 16:33:34 Paypal: Set-Cookie: cookie_check=yes; expires=Tue, 28-Nov-2023 15:33:33 GMT; domain=.paypal.com; path=/; Secure; HttpOnly

2013-11-30 16:33:34 Paypal: Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly

2013-11-30 16:33:34 Paypal: Set-Cookie: navlns=0.0; expires=Mon, 30-Nov-2015 15:33:33 GMT; domain=.paypal.com; path=/; Secure; HttpOnly

2013-11-30 16:33:34 Paypal: Set-Cookie: Apache=10.72.109.11.1385825613169659; path=/; expires=Mon, 23-Nov-43 15:33:33 GMT

2013-11-30 16:33:34 Paypal: Connection: close

2013-11-30 16:33:34 Paypal: Set-Cookie: X-PP-
SILOVER=name%3DSANDBOX3.WEB.1%26silo_version%3D880%26app%3Dslingshot%26TIME%3D1292212818; domain=.paypal.com; path=/; Secure; HttpOnly

2013-11-30 16:33:34 Paypal: Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT

2013-11-30 16:33:34 Paypal: Set-Cookie: Apache=10.72.128.11.1385825613160690; path=/; expires=Mon, 23-Nov-43 15:33:33 GMT

2013-11-30 16:33:34 Paypal: Vary: Accept-Encoding

2013-11-30 16:33:34 Paypal: Strict-Transport-Security: max-age=14400

2013-11-30 16:33:34 Paypal: Transfer-Encoding: chunked

2013-11-30 16:33:34 Paypal: Content-Type: text/html; charset=UTF-8

2013-11-30 16:33:34 Paypal: 

2013-11-30 16:33:34 Paypal: 7

2013-11-30 16:33:34 Paypal: INVALID

2013-11-30 16:33:34 Paypal: 0

2013-11-30 16:33:34 Paypal: 

这是我的代码:

Transaction.php:

function requestPaypal($price, $name, $custom){
    $request = array(
        'METHOD'        => 'BMCreateButton',
        'VERSION'       => '87',
        'USER'          => Configure::read('Paypal.USER'),
        'PWD'           => Configure::read('Paypal.PWD'),
        'SIGNATURE'     => Configure::read('Paypal.SIGNATURE'),
        'BUTTONCODE'    => 'HOSTED',
        'BUTTONTYPE'    => 'BUYNOW',
        'BUTTONSUBTYPE' => 'SERVICES',
        'L_BUTTONVAR0'  => 'business='.Configure::read('Paypal.mail'),
        'L_BUTTONVAR1'  => "item_name=$name",
        'L_BUTTONVAR2'  => "amount=$price",
        'L_BUTTONVAR3'  => "currency_code=EUR",
        'L_BUTTONVAR4'  => "no_note=1",
        'L_BUTTONVAR5'  => "notify_url=".Router::url('/paypal/notify',true),
        'L_BUTTONVAR6'  => "return=".Router::url('/paypal/success',true),
        'L_BUTTONVAR7'  => "cancel=".Router::url('/paypal/cancel',true),
        'L_BUTTONVAR8'  => "custom=$custom",
    );

    $request = http_build_query($request); 

    $curlOptions = array(
        CURLOPT_URL => "https://api-3t.sandbox.paypal.com/nvp",
        CURLOPT_VERBOSE => 1,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_SSL_VERIFYHOST => 1,
        CURLOPT_CAINFO         => APP.'Vendor'.DS.'cacert.pem',
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POSTFIELDS     => $request
    );

    $ch = curl_init();
    if($ch == false){
        return false;
    }
    curl_setopt_array($ch,$curlOptions);
    $response = curl_exec($ch);

    if(curl_errno($ch)){
        debug(curl_error($ch));
        return false;
    }else{
        curl_close($ch);
        parse_str($response,$responseArray);
        return $responseArray['EMAILLINK'];
    }
}

PaypalController.php:

public function notify(){
    $email_account = Configure::read('Paypal.mail');
    $req = 'cmd=_notify-validate';

    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }

    $header="POST /cgi-bin/webscr HTTP/1.1\r\n";
    $header .="Host: www.sandbox.paypal.com\r\n";
    $header .="Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n";
    $header .="Connection: close\r\n\r\n";
    $fp =  fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

    $item_name        = $_POST['item_name'];
    $item_number      = $_POST['item_number'];
    $payment_status   = $_POST['payment_status'];
    $payment_amount   = $_POST['mc_gross'];
    $payment_tax      = $_POST['tax'];
    $payment_ht       = $payment_amount - $payment_tax; 
    $payment_currency = $_POST['mc_currency'];
    $address          = $_POST['address_street'];
    $country          = $_POST['address_country'];
    $city             = $_POST['address_city'];
    $name             = $_POST['address_name'];
    $txn_id           = $_POST['txn_id'];
    $receiver_email   = $_POST['receiver_email'];
    $payer_email      = $_POST['payer_email'];
    parse_str($_POST['custom'],$custom);

    if (!$fp) {

    } else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
        $res = fgets ($fp, 1024);
        debug($res);
        // it blocks here
        if (strpos ($res, "VERIFIED") !== false) {
                            echo '2';
            // vérifier que payment_status a la valeur Completed
            if ( $payment_status == "Completed") {
                            echo '3';
                   if ( $email_account == $receiver_email) {
                            echo '4';

                        if($custom['action'] == 'subscribe'){
                            echo '5';
                            $duration = $custom['duration'];
                            $uid = $custom['uid']; 
                            echo '6';
                                $this->Transaction->save(array(
                                    'price' => $payment_ht,
                                    'tax'   => $payment_tax,
                                    'txnid' => $txn_id,
                                    'user_id'=> $uid,
                                    'action' => 'subscribe',
                                    'amount' => $duration,
                                    'name'=> $name,
                                    'country'=> $country,
                                    'city'  => $city,
                                    'address'=> $address
                                ));

                                $utilisateurs = $this->User->find('all',array('conditions'=>array('societe'=>$this->Session->read('Auth.User.societe'))));
                                foreach($utilisateurs as $k=>$v){

                                }
                                $this->User->id = $uid; 
                                if($this->User->field('premium')){
                                    $end = $this->User->field('end_subscribtion');
                                    $date = new DateTime($end);
                                }else{
                                    $date = new DateTime();
                                }
                                $date->add(new DateInterval('P'.$duration.'M'));
                                $this->User->saveField('end_subscribtion',$date->format('Y-m-d H:i:s'));


                        }

                   }
            }
            else {
                    // Statut de paiement: Echec
            }
            exit();
       }
        else if (strcmp ($res, "INVALID") == 0) {
            // Transaction invalide
        }
    }
    fclose ($fp);
    }   
}

我已经检查了所有其他线程,但我无法找到问题的答案。

请帮助我,也祝你有个美好的一天。

1 个答案:

答案 0 :(得分:2)

问题解决了,我只是将paypal设置中的语言编码更改为UTF8,而对于notify_url问题,我只需要验证我的paypal地址。希望它会帮助某人