简体   繁体   English

paypal ipn notify_url正常工作,但是在设置的返回网址上什么都没有显示

[英]paypal ipn notify_url working correctly but it shows nothing on my return url that is set

I am up and running my E-Commerce website on a server. 我正在服务器上运行我的电子商务网站。 I use Paypal IPN notifications to send customers with notifications about payments made by them. 我使用Paypal IPN通知向客户发送有关其付款的通知。 I am currently using Paypal sandbox for testing. 我目前正在使用Paypal沙箱进行测试。

I proceed to checkout => paypal developer account => make payment => i receive message in my paypal account as ipn request is sent but i don't get the values of ipn request in my orders table in the database. 我继续进行结帐=>贝宝开发人员帐户=>付款=>我在我的贝宝帐户中收到消息,因为发送了ipn请求,但是我在数据库的订单表中没有ipn请求的值。 What could be the reason for this ? 这可能是什么原因?

I have set my IPN details as in the screenshot below. 我已经按照下面的屏幕快照设置了IPN详细信息。 and I also have the screenshot of IPN history of my Paypal developer account. 我还拥有我的Paypal开发者帐户的IPN历史记录的屏幕截图。 Can anyone please help me and tell me the reason why the values in my database are not updated ? 谁能帮助我,并告诉我为什么数据库中的值未更新的原因?

Following is my code: 以下是我的代码:

classes/Paypal.php classes / Paypal.php

<?php
class PayPal {


    private $_environment = 'sandbox';
    private $_url_production = 'https://www.paypal.com/cgi-bin/webscr';
    private $_url_sandbox = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    private $_url;
    private $_cmd;
    private $_products = array();
    private $_fields = array();
    private $_business = 'xxx-xxx@gmail.com';
    private $_page_style = 'null';
    private $_return;
    private $_cancel_payment;
    private $_notify_url;
    private $_currency_code = 'GBP';
    public $_tax_cart = 0;  
    public $_tax = 0;
    public $_populate = array();
    private $_ipn_data = array();
    private $_log_file = null;
    private $_ipn_result;



    public function __construct($cmd = '_cart') {

        $this->_url = $this->_environment == 'sandbox' ?
                        $this->_url_sandbox :
                        $this->_url_production;

        $this->_cmd = $cmd;

        $this->_return = SITE_URL."/?page=return";
        $this->_cancel_payment = SITE_URL."/?page=cancel";
        $this->_notify_url = SITE_URL."/?page=ipn";
        $this->_log_file = ROOT_PATH.DS."log".DS."ipn.log";

    }


    public function addProduct($number, $name, $price = 0, $qty = 1) {

        switch($this->_cmd) {

            case '_cart':
            $id = count($this->_products) + 1;
            $this->_products[$id]['item_number_'.$id] = $number;
            $this->_products[$id]['item_name_'.$id] = $name;
            $this->_products[$id]['amount_'.$id] = $price;
            $this->_products[$id]['quantity_'.$id] = $qty;
            break;
            case '_xclick':
            if (empty($this->_products)) {
                $this->_products[0]['item_number'] = $number;
                $this->_products[0]['item_name'] = $name;
                $this->_products[0]['amount'] = $price;
                $this->_products[0]['quantity'] = $qty;
            }
            break;  
        }
    }

    private function addField($name = null, $value = null) {
        if (!empty($name) && !empty($value)) {
            $field  = '<input type="hidden" name="'.$name.'" ';
            $field .= 'value="'.$value.'" />';
            $this->_fields[] = $field;
        }
    }

    private function standardFields() {
        $this->addField('cmd', $this->_cmd);
        $this->addField('business', $this->_business);
        if ($this->_page_style != null) {
            $this->addField('page_style', $this->_page_style);
        }
        $this->addField('return', $this->_return);
        $this->addField('notify_url', $this->_notify_url);
        $this->addField('cancel_payment', $this->_cancel_payment);
        $this->addField('currency_code', $this->_currency_code);
        $this->addField('rm', 2);

        switch($this->_cmd) {
            case '_cart':
            if ($this->_tax_cart != 0) {
                $this->addField('tax_cart', $this->_tax_cart);
            }
            $this->addField('upload', 1);
            break;
            case '_xclick':
            if ($this->_tax != 0) {
                $this->addField('tax', $this->_tax);
            }
            break;
        }   
    }

    private function prePopulate() {
        if (!empty($this->_populate)) {
            foreach($this->_populate as $key => $value) {
                $this->addField($key, $value);
            }
        }
    }

    private function processFields() {
        $this->standardFields();
        if (!empty($this->_products)) {
            foreach($this->_products as $product) {
                foreach($product as $key => $value) {
                    $this->addField($key, $value);
                }
            }
        }
        $this->prePopulate();
    }

    private function getFields() {
        $this->processFields();
        if (!empty($this->_fields)) {
            return implode("", $this->_fields);
        }
    }
    private function render() {
        $out  = '<form action="'.$this->_url.'" method="post" id="frm_paypal">';
        $out .= $this->getFields();
        $out .= '<input type="submit" value="Submit" />';
        $out .= '</form>';
        return $out;
    }

    public function run($transaction_id = null) {
        if (!empty($transaction_id)) {
            $this->addField('custom', $transaction_id);
        }
        return $this->render();
    }

    private function validateIpn() {

        $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
            if (!preg_match('/paypal\.com$/', $hostname)) {
            return false;
        }   
            $objForm = new Form();
        $this->_ipn_data = $objForm->getPostArray();

        if (
            !empty($this->_ipn_data) &&
            array_key_exists('receiver_email', $this->_ipn_data) &&
            strtolower($this->_ipn_data['receiver_email']) !=
            strtolower($this->_business)
        ) {
            return false;
        }

        return true;    
    }
    private function getReturnParams() {

        $out = array('cmd=_notify-validate');
        if (!empty($this->_ipn_data)) {
            foreach($this->_ipn_data as $key => $value) {
                $value = function_exists('get_magic_quotes_gpc') ?
                            urlencode(stripslashes($value)) :
                            urlencode($value);
                $out[] = "{$key}={$value}";
            }
        }
        return implode("&", $out);      
    }

    private function sendCurl() {

        $response = $this->getReturnParams();

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
        curl_setopt($ch,    T_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/x-www-form-urlencoded",
            "Content-Length: " . strlen($response)
        ));
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        $this->_ipn_result = curl_exec($ch);
        curl_close($ch);

    }

    public function ipn() {
        if ($this->validateIpn()) {
            $this->sendCurl();
            if (strcmp($this->_ipn_result, "VERIFIED") == 0) {
                $objOrder = new Order();

                if (!empty($this->_ipn_data)) {

                    $objOrder->approve(
                        $this->_ipn_data, 
                        $this->_ipn_result
                    );      
                }   
            }       
        }   
    }
}

mod/paypal.php mod / paypal.php

<?php
require_once('../inc/autoload.php');
$token2 = Session::getSession('token2');
$objForm = new Form();
$token1 = $objForm->getPost('token');

if ($token2 == Login::string2hash($token1)) {

    // create order
    $objOrder = new Order();
    if ($objOrder->createOrder()) {

        // populate order details
        $order = $objOrder->getOrder();
        $items = $objOrder->getOrderItems();


        if (!empty($order) && !empty($items)) {

            $objBasket = new Basket();
            $objCatalogue = new Catalogue();
            $objPayPal = new PayPal();


            foreach($items as $item) {
                $product = $objCatalogue->getProduct($item['product']);
                $objPayPal->addProduct(
                    $item['product'], 
                    $product['name'], 
                    $item['price'], 
                    $item['qty']
                );
            }


            $objPayPal->_tax_cart = $objBasket->_vat;

            // populate client's details
            $objUser = new User();
            $user = $objUser->getUser($order['client']);

            if (!empty($user)) {


                $objCountry = new Country();
                $country = $objCountry->getCountry($user['country']);


                $objPayPal->_populate = array(
                    'address1'      => $user['address_1'],
                    'address2'      => $user['address_2'],
                    'city'          => $user['town'],
                    'state'         => $user['county'],
                    'zip'           => $user['post_code'],
                    'country'       => $country['code'],
                    'email'         => $user['email'],
                    'first_name'    => $user['first_name'],
                    'last_name'     => $user['last_name']                   
                );


                // redirect client to PayPal
                echo $objPayPal->run($order['id']);



            }

        }

    }   

}

IPN settings IPN Details IPN设置 IPN详细信息

Please help me with this . 请帮我解决一下这个 。

IPN is not related to your return URL. IPN与您的返回网址无关。 If you're adding your code that you expect to run when IPNs are triggered to your return URL you will not get the expected result. 如果要将您希望在触发IPN时运行的代码添加到返回URL,则不会获得预期的结果。

To get data to your return URL you would need to use PDT which is very similar to IPN but is intended to be sent to the return URL. 要将数据获取到返回URL,您将需要使用与IPN非常相似的PDT,但它应发送到返回URL。 IPN goes to your notify URL, which should not match your return URL. IPN转到您的通知URL,该URL应与您的返回URL不匹配。

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

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