dever 3 years ago
parent
commit
e4d851a225
8 changed files with 187 additions and 23 deletions
  1. 20 1
      config/base.php
  2. 52 0
      database/account.php
  3. 1 1
      database/order.php
  4. 11 12
      lib/Core.php
  5. 30 0
      lib/Manage.php
  6. 1 1
      lib/Set.php
  7. 13 0
      lib/Wechat.php
  8. 59 8
      src/Api.php

+ 20 - 1
config/base.php

@@ -17,9 +17,28 @@ $config['base'] = array
 			1 => '待支付',
 			2 => '已支付',
 			3 => '支付失败',
-			//4 => '申请退款',
+			4 => '申请退款',
     		5 => '已退款',
 		),
+
+		# 开放的支付方法 具体数值参考system_source_key Dever::config('base')->system_source_key
+		/*
+		'h5' => 1,
+	    'android' => 2,
+	    'ios' => 3,
+	    'pc' => 4,
+	    'wx_applet' => 5,
+	    'wx_service' => 6,
+		*/
+		'method' => array
+		(
+		    1 => 'page',
+			2 => 'app',
+			3 => array('app', 'apple'),
+			4 => 'qrcode',
+			5 => 'applet',
+			6 => 'page',
+		),
 	),
 
 	'pay_notify' => '',

+ 52 - 0
database/account.php

@@ -2,6 +2,13 @@
 
 $type = Dever::config('base', 'pay')->pay['type'];
 
+$system_source = Dever::config('base')->system_source;
+
+$channel = function()
+{
+	return Dever::db('pay/channel')->state();
+};
+
 $box = array
 (
 	1 => '正式环境',
@@ -68,6 +75,32 @@ return array
 			'show'		=> 'alipay',
 		),
 
+		'system_source'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '支付平台',
+			'default' 	=> '1',
+			'desc' 		=> '支付平台',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'radio',
+			'option'	=> $system_source,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
+		'channel_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '所属渠道',
+			'default' 	=> '1',
+			'desc' 		=> '所属渠道',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'radio',
+			'option'	=> $channel,
+			'search'	=> 'select',
+			'list'		=> true,
+		),
+
 		'mchid'		=> array
 		(
 			'type' 		=> 'varchar-150',
@@ -245,4 +278,23 @@ return array
 			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
 		),
 	),
+
+	'manage' => array
+	(
+		# 自定义快捷新增和编辑
+        'button' => array
+        (
+            '渠道管理' => array('list', 'channel&project=pay&oper_parent=account&oper_project=pay'),
+        ),
+	),
+
+	# 默认值
+	'default' => array
+	(
+		'col' => 'name,state,cdate',
+		'value' => array
+		(
+			'"公众号支付",1,' . time(),
+		),
+	),
 );

+ 1 - 1
database/order.php

@@ -81,7 +81,7 @@ return array
 
 		'order_id'		=> array
 		(
-			'type' 		=> 'varchar-100',
+			'type' 		=> 'varchar-200',
 			'name' 		=> '订单id',
 			'default' 	=> '',
 			'desc' 		=> '订单id',

+ 11 - 12
lib/Core.php

@@ -41,35 +41,34 @@ class Core
 				$project = Dever::db('pay/project')->one($info['project_id']);
 				if ($project && $project['notify']) {
 					$notify = $project['notify'];
-					$key = md5($project['key']);
+					//$key = md5($project['key']);
+					$key = $project['key'];
 				}
 			}
 
 			if (!$notify) {
 				$notify = Dever::config('base', 'project')->pay_notify;
-				$key = md5($notify);
+				//$key = md5($notify);
+				$key = $notify;
 			}
 
 			if ($notify && $key) {
 				
 				$send = array();
-				if (isset($info['product_id'])) {
-					$send['pay_product_id'] = $info['product_id'];
-				}
-				if (isset($info['uid'])) {
-					$send['pay_uid'] = $info['uid'];
-				}
-				if (isset($info['cash'])) {
-					$send['pay_cash'] = $info['cash'];
-				}
+				$send['pay_product_id'] = $info['product_id'];
+				$send['pay_uid'] = $info['uid'];
+				$send['pay_cash'] = $info['cash'];
 				$send['pay_order_id'] = $order_id;
 				$send['pay_status'] = $param['status'];
 				$send['pay_msg'] = $msg;
+				# 2020-08-20 取消之前的加密方式,使用自动加密
+				/*
 				$send['pay_time'] = time();
 				$send['pay_nonce'] = Dever::nonce();
 				ksort($send);
 				$send['signature'] = md5($key . '&' . http_build_query($send));
-
+				*/
+				$send['dever_token'] = $key;
 				$this->log($notify, $send);
 
 				if (strstr($notify, 'http://') || strstr($notify, 'https://')) {

+ 30 - 0
lib/Manage.php

@@ -28,4 +28,34 @@ class Manage
             }
         }
     }
+
+    # 更新渠道的同时,更新passport下的项目
+    public function updateChannel($id, $name, $param)
+    {
+        if (!Dever::project('passport')) {
+            return;
+        }
+        $data['name'] = Dever::param('name', $param);
+        # 获取小程序信息,同步过去
+        $where['channel_id'] = $id;
+        $where['system_source'] = 5;
+        $account = Dever::db('pay/account')->one($where);
+        if ($account) {
+            $data['appid'] = $account['appid'];
+            $data['secret'] = $account['appsecret'];
+        }
+        # 与passport下的id 对应
+        $info = Dever::db('passport/system')->one($id);
+        if (!$info) {
+            Dever::db('passport/system')->insert($data);
+        } else {
+            $data['where_id'] = $info['id'];
+            $data['state'] = Dever::param('state', $param);
+            if (!$data['name']) {
+                unset($data['name']);
+            };
+            Dever::db('passport/system')->update($data);
+        }
+        return $info;
+    }
 }

+ 1 - 1
lib/Set.php

@@ -18,7 +18,7 @@ class Set
 	}
 
 	# 带有安全验证的接口 需要传入dever_token,可以参考下边的test_order例子
-    public function updateStatus_api($param = array())
+    public function updateStatus_secure_api($param = array())
     {
     	$send = $param ? $param : Dever::preInput('pay_');
         $tk_pic = $send['pay_tk_pic'];

+ 13 - 0
lib/Wechat.php

@@ -7,6 +7,8 @@ class Wechat extends Core
 	public function __construct($config)
 	{
 		$project = Dever::project('pay');
+		$this->channel_id = $config['channel_id'];
+		$this->system_source = $config['system_source'];
 		$this->config = new \WxPayConfig();
 		# 通知接口
 		$config['notify'] = $this->url($config['type'], $config['id']);
@@ -107,6 +109,17 @@ class Wechat extends Core
 		$input->SetTrade_type($trade_type);
 		$input->SetProduct_id($product_id);
 		if ($type != 4 && $openid != -1) {
+
+			if (!$openid && Dever::project('passport')) {
+				$where = array();
+				$where['uid'] = $uid;
+				$where['system_source'] = $this->system_source;
+				$where['system_id'] = $this->channel_id;
+				$wechat = Dever::db('passport/wechat')->one($where);
+				if ($wechat) {
+					$openid = $wechat['openid'];
+				}
+			}
 			$openid = $openid ? $openid : $tools->GetOpenid();
 			$input->SetOpenid($openid);
 		}

+ 59 - 8
src/Api.php

@@ -16,6 +16,13 @@ class Api
 		Dever::location($url);
 	}
 
+	# 获取支付渠道
+	public function channel()
+	{
+		$data = Dever::db('pay/channel')->getData();
+		return $data;
+	}
+
 	/**
 	 * 发起支付 下单 获取预支付信息
 	 *
@@ -48,10 +55,34 @@ class Api
 		if (!$this->account_id) {
 			Dever::alert('没有账户信息');
 		}
-		$this->pay();
+		$this->handle();
 		return $this->method->notify();
 	}
 
+	/**
+	 * 发起支付 通用的发起支付方法
+	 *
+	 * @return mixed
+	 */
+	public function pay($param = array())
+	{
+		$source = $this->getParam($param, 'source');
+		$receipt = $this->getParam($param, 'receipt');
+
+		$config = Dever::config('base', 'pay')->method;
+
+		if (isset($config[$source])) {
+			$param['account_id'] = $source;
+			$method = $config[$source];
+			if ($source == 3 && $receipt) {
+				$method = $method[1];
+			}
+			return $this->$method($param);
+		} else {
+			Dever::alert('错误的source');
+		}
+	}
+
 	/**
 	 * 发起支付 用于小程序支付
 	 *
@@ -59,6 +90,7 @@ class Api
 	 */
 	public function applet($param = array())
 	{
+		$param['source'] = 4;
 		$this->init($param);
 		return $this->method->applet($this->get(1));
 	}
@@ -70,6 +102,7 @@ class Api
 	 */
 	public function app($param = array())
 	{
+		$param['source'] = 1;
 		$this->init($param);
 		$this->openid = -1;
 		return $this->method->app($this->get(3));
@@ -129,7 +162,7 @@ class Api
 		if (!$this->account_id) {
 			Dever::alert('没有账户信息');
 		}
-		$this->pay();
+		$this->handle();
 		$this->order_id = $this->getParam($param, 'order_id');
 		return $this->method->search($this->order_id);
 	}
@@ -145,7 +178,7 @@ class Api
 		if (!$this->account_id) {
 			Dever::alert('没有账户信息');
 		}
-		$this->pay();
+		$this->handle();
 		$this->order_id = $this->getParam($param, 'order_id');
 		return $this->method->refundByOrder($this->order_id);
 	}
@@ -160,16 +193,18 @@ class Api
 		if (isset($this->account_id)) {
 			return;
 		}
+		$this->channel_id = $this->getParam($param, 'channel_id');
+		$this->system_source = $this->getParam($param, 'system_source');
 		$this->account_id = $this->getParam($param, 'account_id');
 		$this->project_id = $this->getParam($param, 'project_id');
 		$this->uid = $this->getParam($param, 'uid');
+		$this->openid = $this->getParam($param, 'openid');
 		$this->username = $this->getParam($param, 'username');
 		$this->product_id = $this->getParam($param, 'product_id');
 		$this->name = $this->getParam($param, 'name');
 		$this->cash = $this->getParam($param, 'cash');
 		$this->refer = $this->getParam($param, 'refer');
 		$this->order_id = $this->getParam($param, 'order_id');
-		$this->openid = $this->getParam($param, 'openid');
 		$this->other = $this->getParam($param, 'other');
 		$this->h5 = $this->getParam($param, 'h5');
 		$this->ip = $this->getParam($param, 'ip');
@@ -182,7 +217,7 @@ class Api
 			$this->order_id = false;
 		}
 
-		if (!$this->account_id) {
+		if (!$this->account_id && !$this->channel_id && !$this->system_source) {
 			Dever::alert('没有账户信息');
 		}
 
@@ -206,7 +241,7 @@ class Api
 			$this->ip = Dever::ip();
 		}
 
-		return $this->pay();
+		return $this->handle();
 	}
 
 	/**
@@ -227,9 +262,25 @@ class Api
 	 *
 	 * @return mixed
 	 */
-	private function pay()
+	private function handle()
 	{
-		$pay = Dever::db('pay/account')->one($this->account_id);
+		$pay = false;
+		if ($this->account_id > 0) {
+			$pay = Dever::db('pay/account')->one($this->account_id);
+		} elseif ($this->channel_id && $this->system_source) {
+			$pay = Dever::db('pay/account')->one(array('channel_id' => $channel_id, 'system_source' => $this->system_source));
+		}
+
+		if (!$pay || ($pay && $pay['state'] != 1)) {
+			Dever::alert('没有账户信息');
+		}
+
+		$this->account_id = $pay['id'];
+		/*
+		$this->channel_id = $pay['channel_id'];
+		$this->system_source = $pay['system_source'];
+		*/
+		
 		$method = '\\Pay\\Lib\\' . ucwords($pay['type']);
 		if (isset($this->refer) && $this->refer) {
 			$pay['refer'] = $this->refer;