简体   繁体   English

无效的paypal IPN与cakephp

[英]INVALID response paypal IPN with cakephp

i have 2 problems with paypal IPN : 我有2个与Paypal IPN有关的问题:

1 : paypal don't send the response to the notify url ( in the variable notify_url ), it only send to the return url, even if i change the notify url in the paypal sandbox configuration. 1:paypal不会将响应发送到notify url(在变量notify_url中),即使我在paypal沙箱配置中更改了notify url,它也只会发送到返回URL。

2 : i have an INVALID response from paypal, here is the return of debug($res) : 2:我收到了来自贝宝的无效响应,这是debug($ 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: 

Here is my code : 这是我的代码:

Transaction.php : 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 : 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);
    }   
}

i already checked all the others threads and i can't find an answer to my problem. 我已经检查了所有其他线程,但找不到我的问题的答案。

Please help me, also have a good day. 请帮助我,也有美好的一天。

Problem solved, i just changed the language encodage in the paypal settings to UTF8, and for the notify_url problem i just had to verify my paypal adress. 问题解决了,我只是将贝宝设置中的语言编码更改为UTF8,对于notify_url问题,我只需要验证我的贝宝地址即可。 Hope it will help someone 希望对别人有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM