ѡ̳

标题: phpcms支付模块集成说明 [打印本页]

作者: admin    时间: 2018-6-15 16:20
标题: phpcms支付模块集成说明

$info = array(
      'userid'      => $this->_userid,
      'username'    => $this->_username,
      'money'       => trim(floatval($_POST['info']['price'])),
      'quantity'    => $_POST['quantity'] ? trim(intval($_POST['quantity'])) : 1,
      'telephone'   => preg_match('/[^0-9\-]+/', $_POST['info']['telephone']) ? '' : trim($_POST['info']['telephone']),
      'contactname' => $_POST['info']['name'] ? trim($_POST['info']['name']).L('recharge') : $this->_username.L('recharge'),
      'email'       => is_email($_POST['info']['email']) ? trim($_POST['info']['email']) : '',
      'addtime'    => SYS_TIME,
      'ip'        => ip(),
      'pay_type'   => 'recharge',
      'pay_id'      => $payment['pay_id'],      
      'payment'     => trim($payment['pay_name']),
      'ispay'         => '1',
      'usernote'    => $usernote,
      'trade_sn'   => $trade_sn,
);

$payment = $this->handle->get_payment($info['pay_id']);
$cfg = unserialize_config($payment['config']);
$pay_name = ucwords($payment['pay_code']);

$pay_fee = pay_fee($info['money'],$payment['pay_fee'],$payment['pay_method']);
$logistics_fee = $info['logistics_fee'];
$discount = $info['discount'];      
// calculate amount
$info['price'] = $info['money'] + $pay_fee + $logistics_fee + $discount;         
// add order info
$order_info['id']  = $info['trade_sn'];
$order_info['quantity']    = $info['quantity'];
$order_info['buyer_email'] = $info['email'];
$order_info['order_time']  = $info['addtime'];

//add product info
$product_info['name'] = $info['contactname'];
$product_info['body'] = $info['usernote'];
$product_info['price'] = $info['price'];


pc_base::load_app_class('pay_factory','',0);
$payment_handler = new pay_factory($pay_name, $cfg);
$payment_handler->set_productinfo($product_info)->set_orderinfo($order_info)->set_customerinfo($customer_info);
$code = $payment_handler->get_code('value="'.L('confirm_pay').'" class="button"');

作者: admin    时间: 2018-6-15 17:05
class pay_factory  {
       
        public function __construct($adapter_name = '', $adapter_config = array()) {
                $this->set_adapter($adapter_name, $adapter_config);
        }
        /**
         * 构造适配器
         * @param  $adapter_name 支付模块code
         * @param  $adapter_config 支付模块配置
         */
        public function set_adapter($adapter_name, $adapter_config = array()) {
                if (!is_string($adapter_name)) return false;
                else {
                        $class_name = ucwords($adapter_name);
                        pc_base::load_app_class($class_name,'','0');
                        $this->adapter_instance = new $class_name($adapter_config);
                }
                return $this->adapter_instance;
        }
       
        public function __call($method_name, $method_args) {
                if (method_exists($this, $method_name))
                        return call_user_func_array(array(& $this, $method_name), $method_args);
                elseif (
                        !empty($this->adapter_instance)
                        && ($this->adapter_instance instanceof paymentabstract)
                        && method_exists($this->adapter_instance, $method_name)
                )
                return call_user_func_array(array(& $this->adapter_instance, $method_name), $method_args);
        }       
}
作者: admin    时间: 2018-6-15 17:06
admin 发表于 2018-6-15 17:05
class pay_factory  {
       
        public function __construct($adapter_name = '', $adapter_config = array()) ...

<?php
abstract class paymentabstract
{
        protected $config = array();
        protected $product_info = array();
        protected $customter_info = array();
        protected $order_info = array();
        protected $shipping_info = array();

        public function set_config($config)
        {
                foreach ($config as $key => $value) $this->config[$key] = $value;
                return $this;
        }

        public function set_productinfo($product_info)
        {
                $this->product_info = $product_info;
                return $this;
        }

        public function set_customerinfo($customer_info)
        {
                $this->customer_info = $customer_info;
                return $this;
        }

        public function set_orderinfo($order_info)
        {
                $this->order_info = $order_info;
                return $this;
        }

        public function set_shippinginfo($shipping_info)
        {
                $this->shipping_info = $shipping_info;
                return $this;
        }

        public function get_code($button_attr = '')
        {
                if (strtoupper($this->config['gateway_method']) == 'POST') $str = '<form action="' . $this->config['gateway_url'] . '" method="POST" target="_blank">';
                else $str = '<form action="' . $this->config['gateway_url'] . '" method="GET" target="_blank">';
                $prepare_data = $this->getpreparedata();
                foreach ($prepare_data as $key => $value) $str .= '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
                $str .= '<input type="submit" ' . $button_attr . ' />';
                $str .= '</form>';
                return $str;
        }

        protected function get_verify($url,$time_out = "60") {
        $urlarr     = parse_url($url);
        $errno      = "";
        $errstr     = "";
        $transports = "";
        if($urlarr["scheme"] == "https") {
            $transports = "ssl://";
            $urlarr["port"] = "443";
        } else {
            $transports = "tcp://";
            $urlarr["port"] = "80";
        }
        $fp=@fsockopen($transports . $urlarr['host'],$urlarr['port'],$errno,$errstr,$time_out);
        if(!$fp) {
            die("ERROR: $errno - $errstr<br />\n");
        } else {
            fputs($fp, "POST ".$urlarr["path"]." HTTP/1.1\r\n");
            fputs($fp, "Host: ".$urlarr["host"]."\r\n");
            fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
            fputs($fp, "Content-length: ".strlen($urlarr["query"])."\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, $urlarr["query"] . "\r\n\r\n");
            while(!feof($fp)) {
                $info[]=@fgets($fp, 1024);
            }
            fclose($fp);
            $info = implode(",",$info);
            return $info;
        }
    }


        abstract public function receive();

        abstract public function notify();

        abstract public function response($result);

        abstract public function getPrepareData();
}
作者: admin    时间: 2018-6-15 17:07
admin 发表于 2018-6-15 17:06

<?php
defined('IN_PHPCMS') or exit('No permission resources.');
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    $modules[$i]['code']    = basename(__FILE__, '.class.php');
    $modules[$i]['name']    = L('chinabank', '', 'pay');   
    $modules[$i]['desc']    = L('chinabank_tip', '', 'pay');
    $modules[$i]['is_cod']  = '0';
    $modules[$i]['is_online']  = '1';
    $modules[$i]['author']  = 'PHPCMS开发团队';
    $modules[$i]['website'] = 'http://www.chinabank.com.cn';
    $modules[$i]['version'] = '1.0.0';
    $modules[$i]['config']  = array(
             array('name' => 'chinabank_account','type' => 'text','value' => ''),
        array('name' => 'chinabank_key','type' => 'text','value' => ''),
    );

    return;
}
pc_base::load_app_class('pay_abstract','','0');

class Chinabank extends paymentabstract{
       
        public function __construct($config = array()) {       
                if (!empty($config)) $this->set_config($config);
      
                $this->config['gateway_url'] = 'https://pay3.chinabank.com.cn/PayGate';
                $this->config['gateway_method'] = 'POST';
                $this->config['return_url'] = return_url('chinabank');
                pc_base::load_app_func('alipay');
        }

        public function getpreparedata() {               
                $prepare_data['v_mid'] = $this->config['chinabank_account'];
                $prepare_data['v_url'] = $this->config['return_url'];
               
                $prepare_data['v_moneytype'] = 'CNY';
                $prepare_data['return_url'] = $this->config['return_url'];               
               
                // 商品信息
                $prepare_data['v_rcvname'] = $this->product_info['name'];
                $prepare_data['v_amount'] = $this->product_info['price'];
               
                //订单信息
                $prepare_data['v_oid'] = $this->order_info['id'];

                //买家信息
                $prepare_data['v_rcvmobile'] = $this->customer_info['telephone'];
                $prepare_data['v_rcvemail'] = $this->order_info['buyer_email'];
               
                //备注               
                $prepare_data['remark1'] = $this->product_info['body'];
               
                $data =$prepare_data['v_amount'].$prepare_data['v_moneytype'].$prepare_data['v_oid'].$prepare_data['v_mid'].$prepare_data['v_url'].$this->config['chinabank_key'];
                // 数字签名
                $prepare_data['v_md5info'] = strtoupper(md5($data));
               
                return $prepare_data;
        }
       
        /**
         * 客户端接收数据
         * 状态码说明  (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付)
         */
    public function receive() {
            $receive_data = $this->filterParameter($_POST);
            $receive_data = arg_sort($receive_data);
            if ($receive_data) {
                        $v_oid     =trim($receive_data['v_oid']);
                        $v_pmode   =trim($receive_data['v_pmode']);  
                        $v_pstatus =trim($receive_data['v_pstatus']);
                        $v_pstring =trim($receive_data['v_pstring']);
                        $v_amount  =trim($receive_data['v_amount']);
                        $v_moneytype  =trim($receive_data['v_moneytype']);
                        $remark1   =trim($receive_data['remark1' ]);
                        $remark2   =trim($receive_data['remark2' ]);
                        $v_md5str  =trim($receive_data['v_md5str' ]);
                        $md5string=strtoupper(md5($v_oid.$v_pstatus.$v_amount.$v_moneytype.$this->config['chinabank_key']));
                        if ($v_md5str==$md5string) {
                                $return_data['order_id'] = $v_oid;
                                $return_data['order_total'] = $v_amount;
                                $return_data['price'] = $v_amount;
                                if($v_pstatus=="20") {
                                        $return_data['order_status'] = 0;
                                        return $return_data;
                                } else {
                                        error_log(date('m-d H:i:s',SYS_TIME).'| chinabank GET: order_status=30 |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                                        return false;
                                }
                        } else {
                                showmessage(L('illegal_sign'));
                                return false;
                        }
                } else {
                       
                        error_log(date('m-d H:i:s',SYS_TIME).'| GET: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                        showmessage(L('illegal_return'));
                        return false;
                }          
    }       

    /**
         * POST接收数据
         * 状态码说明  (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付)
         */
    public function notify() {
            $receive_data = $this->filterParameter($_POST);
            $receive_data = arg_sort($receive_data);
            if ($receive_data) {
                        $v_oid     =trim($receive_data['v_oid']);
                        $v_pmode   =trim($receive_data['v_pmode']);  
                        $v_pstatus =trim($receive_data['v_pstatus']);
                        $v_pstring =trim($receive_data['v_pstring']);
                        $v_amount  =trim($receive_data['v_amount']);
                        $v_moneytype  =trim($receive_data['v_moneytype']);
                        $remark1   =trim($receive_data['remark1' ]);
                        $remark2   =trim($receive_data['remark2' ]);
                        $v_md5str  =trim($receive_data['v_md5str' ]);

                        $md5string=strtoupper(md5($v_oid.$v_pstatus.$v_amount.$v_moneytype.$this->config['chinabank_key']));
                        if ($v_md5str==$md5string) {
                                $return_data['order_id'] = $v_oid;
                                $return_data['order_total'] = $v_amount;
                                $return_data['price'] = $v_amount;
                                if($v_pstatus=="20") {
                                        $return_data['order_status'] = 0;
                                } else {
                                        error_log(date('m-d H:i:s',SYS_TIME).'| chinabank notify: order_status=30 |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                                        return false;
                                }
                        } else {
                                return false;
                        }
                } else {
                       
                        error_log(date('m-d H:i:s',SYS_TIME).'| notify: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                        return false;
                }         
    }
           
    /**
     * 相应服务器应答状态
     * @param $result
     */
    public function response($result) {
            if (FALSE == $result) echo 'ok';
                else echo 'success';
    }
   
    /**
     * 返回字符过滤
     * @param $parameter
     */
        private function filterParameter($parameter)
        {
                $para = array();
                foreach ($parameter as $key => $value)
                {
                        if ('sign' == $key || 'sign_type' == $key || '' == $value || 'm' == $key  || 'a' == $key  || 'c' == $key   || 'code' == $key ) continue;
                        else $para[$key] = $value;
                }
                return $para;
        }
}
?>
作者: admin    时间: 2018-6-15 17:08
admin 发表于 2018-6-15 17:07

<?php
defined('IN_PHPCMS') or exit('No permission resources.');
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    $modules[$i]['code']    = basename(__FILE__, '.class.php');
    $modules[$i]['name']    = L('alipay', '', 'pay');   
    $modules[$i]['desc']    = L('alipay_tip', '', 'pay');
    $modules[$i]['is_cod']  = '0';
    $modules[$i]['is_online']  = '1';
    $modules[$i]['author']  = 'PHPCMS开发团队';
    $modules[$i]['website'] = 'http://www.alipay.com';
    $modules[$i]['version'] = '1.0.0';
    $modules[$i]['config']  = array(
             array('name' => 'alipay_account','type' => 'text','value' => ''),
        array('name' => 'alipay_key','type' => 'text','value' => ''),
        array('name' => 'alipay_partner','type' => 'text','value' => ''),
        array('name' => 'service_type','type' => 'select','value' => '0'),
    );

    return;
}
pc_base::load_app_class('pay_abstract','','0');

class Alipay extends paymentabstract{
       
        public function __construct($config = array()) {       
                if (!empty($config)) $this->set_config($config);
               
            if ($this->config['service_type']==1) $this->config['service'] = 'trade_create_by_buyer';
                elseif($this->config['service_type']==2) $this->config['service'] = 'create_direct_pay_by_user';
        else $this->config['service'] = 'create_partner_trade_by_buyer';       
        
                $this->config['gateway_url'] = 'https://www.alipay.com/cooperate/gateway.do?_input_charset='.CHARSET;
                $this->config['gateway_method'] = 'POST';
                $this->config['notify_url'] = return_url('alipay',1);
                $this->config['return_url'] = return_url('alipay');
                pc_base::load_app_func('alipay');
        }

        public function getpreparedata() {               
                $prepare_data['service'] = $this->config['service'];
                $prepare_data['payment_type'] = '1';
                $prepare_data['seller_email'] = $this->config['alipay_account'];
                $prepare_data['partner'] = $this->config['alipay_partner'];
                $prepare_data['_input_charset'] = CHARSET;               
                $prepare_data['notify_url'] = $this->config['notify_url'];
                $prepare_data['return_url'] = $this->config['return_url'];
               
                // 商品信息
                $prepare_data['subject'] = $this->product_info['name'];
                $prepare_data['price'] = $this->product_info['price'];
                if (array_key_exists('url', $this->product_info)) $prepare_data['show_url'] = $this->product_info['url'];
                $prepare_data['body'] = $this->product_info['body'];
               
                //订单信息
                $prepare_data['out_trade_no'] = $this->order_info['id'];
                $prepare_data['quantity'] = $this->order_info['quantity'];

                // 物流信息
                if($this->config['service'] == 'create_partner_trade_by_buyer' || $this->config['service'] == 'trade_create_by_buyer') {
                        $prepare_data['logistics_type'] = 'EXPRESS';
                        $prepare_data['logistics_fee'] = '0.00';
                        $prepare_data['logistics_payment'] = 'SELLER_PAY';
                }
                //买家信息
                $prepare_data['buyer_email'] = $this->order_info['buyer_email'];
               
                $prepare_data = arg_sort($prepare_data);
                // 数字签名
                $prepare_data['sign'] = build_mysign($prepare_data,$this->config['alipay_key'],'MD5');
                return $prepare_data;
        }
       
        /**
         * GET接收数据
         * 状态码说明  (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付5交易取消6交易发生错误)
         */
    public function receive() {
            $receive_sign = $_GET['sign'];
            $receive_data = $this->filterParameter($_GET);
            $receive_data = arg_sort($receive_data);
            if ($receive_data) {
                        $verify_result = $this->get_verify('http://notify.alipay.com/trade/notify_query.do?partner=' . $this->config['alipay_partner'] . '&notify_id=' . $receive_data['notify_id']);
                        if (preg_match('/true$/i', $verify_result))
                        {
                                $sign = '';
                                $sign = build_mysign($receive_data,$this->config['alipay_key'],'MD5');                               
                                if ($sign != $receive_sign)
                                {
                                        error_log(date('m-d H:i:s',SYS_TIME).'| GET: signature is bad |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');                                       
                                        showmessage(L('illegal_sign'));
                                        return false;
                                }
                                else
                                {
                                        $return_data['order_id'] = $receive_data['out_trade_no'];
                                        $return_data['order_total'] = $receive_data['total_fee'];
                                        $return_data['price'] = $receive_data['price'];
                                        switch ($receive_data['trade_status'])
                                        {
                                                case 'WAIT_BUYER_PAY': $return_data['order_status'] = 3; break;
                                                case 'WAIT_SELLER_SEND_GOODS': $return_data['order_status'] = 3; break;
                                                case 'WAIT_BUYER_CONFIRM_GOODS': $return_data['order_status'] = 3; break;
                                                case 'TRADE_CLOSED': $return_data['order_status'] = 5; break;                                               
                                                case 'TRADE_FINISHED': $return_data['order_status'] = 0; break;
                                                case 'TRADE_SUCCESS': $return_data['order_status'] = 0; break;
                                                default:
                                                         $return_data['order_status'] = 5;                                               
                                        }                       
                                        return $return_data;
                                }

                        }
                        else
                        {
                                error_log(date('m-d H:i:s',SYS_TIME).'| GET: illegality notice : flase |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                                showmessage(L('illegal_notice'));
                                return false;
                        }
                } else {
                       
                        error_log(date('m-d H:i:s',SYS_TIME).'| GET: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                        showmessage(L('illegal_return'));
                        return false;
                }          
    }       

    /**
         * POST接收数据
         * 状态码说明  (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付 5交易取消6交易发生错误)
         */
    public function notify() {
            $receive_sign = $_POST['sign'];
            $receive_data = $this->filterParameter($_POST);
            $receive_data = arg_sort($receive_data);
            if ($receive_data) {
                        $verify_result = $this->get_verify('http://notify.alipay.com/trade/notify_query.do?service=notify_verify&partner=' . $this->config['alipay_partner'] . '&notify_id=' . $receive_data['notify_id']);
                        if (preg_match('/true$/i', $verify_result))
                        {
                                $sign = '';
                                $sign = build_mysign($receive_data,$this->config['alipay_key'],'MD5');                               
                                if ($sign != $receive_sign)
                                {
                                        error_log(date('m-d H:i:s',SYS_TIME).'| POST: signature is bad |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');                                       
                                        return false;
                                }
                                else
                                {
                                        $return_data['order_id'] = $receive_data['out_trade_no'];
                                        $return_data['order_total'] = $receive_data['total_fee'];
                                        $return_data['price'] = $receive_data['price'];
                                        switch ($receive_data['trade_status']) {
                                                case 'WAIT_BUYER_PAY': $return_data['order_status'] = 3; break;
                                                case 'WAIT_SELLER_SEND_GOODS': $return_data['order_status'] = 3; break;
                                                case 'WAIT_BUYER_CONFIRM_GOODS': $return_data['order_status'] = 3; break;
                                                case 'TRADE_CLOSED': $return_data['order_status'] = 5; break;                                               
                                                case 'TRADE_FINISHED': $return_data['order_status'] = 0; break;
                                                case 'TRADE_SUCCESS': $return_data['order_status'] = 0; break;
                                                default:
                                                         $return_data['order_status'] = 5;
                                        }
                                        return $return_data;
                                }

                        }
                        else
                        {
                                error_log(date('m-d H:i:s',SYS_TIME).'|  POST: illegality notice : flase |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                                return false;
                        }
                } else {
                       
                        error_log(date('m-d H:i:s',SYS_TIME).'|  POST: no post return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
                        return false;
                }          
    }
           
    /**
     * 相应服务器应答状态
     * @param $result
     */
    public function response($result) {
            if (FALSE == $result) echo 'fail';
                else echo 'success';
    }
   
    /**
     * 返回字符过滤
     * @param $parameter
     */
        private function filterParameter($parameter)
        {
                $para = array();
                foreach ($parameter as $key => $value)
                {
                        if ('sign' == $key || 'sign_type' == $key || '' == $value || 'm' == $key  || 'a' == $key  || 'c' == $key   || 'code' == $key ) continue;
                        else $para[$key] = $value;
                }
                return $para;
        }
}
?>




欢迎光临 ѡ̳ (http://www.sunminxuan.cn/bbs/) Powered by Discuz! X3.4