dever il y a 3 ans
Parent
commit
bf5a66fecc
5 fichiers modifiés avec 133 ajouts et 58 suppressions
  1. 2 2
      lib/Alipay.php
  2. 4 1
      lib/Core.php
  3. 2 2
      lib/Wechat.php
  4. 99 31
      lib/Yspay.php
  5. 26 22
      src/Api.php

+ 2 - 2
lib/Alipay.php

@@ -62,7 +62,7 @@ class Alipay extends Core
 	/**
 	 * 获取统一下单的基本信息
 	 */
-	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
+	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false)
 	{
 		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
 
@@ -165,7 +165,7 @@ class Alipay extends Core
 	}
 
 	# 退款
-	public function refund($order_id, $cash)
+	public function refund($order_id, $cash, $order, $refund_order_id = false)
 	{
 		$out_trade_no = $order_id;
 		$cash = $cash * 100;

+ 4 - 1
lib/Core.php

@@ -177,8 +177,11 @@ class Core
 	{
 		$info = Dever::db('pay/order')->one(array('order_id' => $order_id));
 		if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 6)) {
+			if ($info['param']) {
+				$info['param'] = Dever::array_decode($info['param']);
+			}
 			$cash = $refund_cash ? $refund_cash : $info['cash'];
-			$state = $this->refund($info['order_id'], $cash, $refund_order_id);
+			$state = $this->refund($info['order_id'], $cash, $info, $refund_order_id);
 			if ($state) {
 				if ($refund_order_id) {
 					$status = 6;

+ 2 - 2
lib/Wechat.php

@@ -55,7 +55,7 @@ class Wechat extends Core
 	}
 
 	# 退款
-	public function refund($order_id, $cash)
+	public function refund($order_id, $cash, $order, $refund_order_id = false)
 	{
 		$out_trade_no = $order_id;
 		$cash = $cash * 100;
@@ -75,7 +75,7 @@ class Wechat extends Core
 	/**
 	 * 获取统一下单的基本信息
 	 */
-	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
+	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false)
 	{
 		$trade_type = $this->getType($type);
 		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);

+ 99 - 31
lib/Yspay.php

@@ -8,6 +8,16 @@ class Yspay extends Core
 		$project = Dever::project('pay');
 		# 通知接口
 		$config['notify'] = $this->url($config['type'], $config['id']);
+		$set = explode('|', $config['mchid']);
+		$config['mid'] = $set[0];
+		$config['tid'] = $set[1];
+		if (isset($set[2])) {
+			$config['prefix'] = $set[2];
+		}
+
+		$set = explode('|', $config['key']);
+		$config['sub_appid'] = $set[0];
+		$config['sub_appsecret'] = $set[1];
 		$this->config = $config;
 	}
 
@@ -23,6 +33,7 @@ class Yspay extends Core
 			$input = Dever::input();
 		}
 		$this->log('支付回调-初始化', $input);
+		echo 'FAILED';die;
 		return;
 
 		ksort($input);
@@ -43,31 +54,46 @@ class Yspay extends Core
 	/**
 	 * 获取统一下单的基本信息
 	 */
-	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
+	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false)
 	{
 		$trade_type = $this->getType($type);
 		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
 
-		$request['merOrderId'] = $order_id;
-		$request['mid'] = $this->config['mchid'];
-		$request['tid'] = $this->config['key'];
+		if (isset($this->config['prefix']) && $this->config['prefix']) {
+			$request['merOrderId'] = $this->config['prefix'] . '_' . $order_id;
+		} else {
+			$request['merOrderId'] = $order_id;
+		}
+		
+
+		$request['mid'] = $this->config['mid'];
+		$request['tid'] = $this->config['tid'];
 		$request['instMid'] = 'MINIDEFAULT';
 		$request['totalAmount'] = $cash * 100;
-		$request['subAppId'] = $this->config['appid'];
+		//$request['totalAmount'] = 100;
+		$request['subAppId'] = $this->config['sub_appid'];
 		$request['requestTimestamp'] = date("Y-m-d H:i:s");
-		$request['expireTime'] = $this->config['timeout'];
+		$request['expireTime'] = date("Y-m-d H:i:s", time() + $this->config['timeout']); 
 		$request['notifyUrl'] = $this->config['notify'];
 		$request['tradeType'] = 'MINI';
-		/*
-		# 是否启用分账
-		$request['divisionFlag'] = true;
-		# 平台分账金额
-		$request['platformAmount'] = $request['totalAmount'] * 0.1;
-		if ($request['divisionFlag']) {
+
+		if ($other) {
+			$request['originalAmount'] = $other['oprice'];
+			$request['divisionFlag'] = true;
+			# 平台分账金额
+			$other['per'] = $other['per']/100;
+			$request['platformAmount'] = $request['totalAmount'] * $other['per'];
 			$request['subOrders'] = array();
-			$request['subOrders']['mid'] = 1;
+			$request['subOrders']['mid'] = $other['mid'];
+			if (isset($this->config['prefix']) && $this->config['prefix']) {
+				$request['subOrders']['merOrderId'] = $this->config['prefix'] . '_' . $other['order_id'];
+			} else {
+				$request['subOrders']['merOrderId'] = $other['order_id'];
+			}
+			
 			$request['subOrders']['totalAmount'] = $request['totalAmount'] - $request['platformAmount'];
-		}*/
+			$request['subOrders'] = array($request['subOrders']);
+		}
 		if (!$openid) {
 			# 测试的openid
 			$request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
@@ -76,25 +102,66 @@ class Yspay extends Core
 		}
 		
 		$result = Base::pay($request, $this->config);
-		$this->updateOrderParam($order_id, $result);
-		return $result;
+		if (isset($result['miniPayRequest'])) {
+			$result['request'] = $request;
+			$result['payMsg'] = $result['miniPayRequest'];
+			unset($result['miniPayRequest']);
+			if ($other) {
+				$result['other'] = $other;
+			}
+			$this->updateOrderParam($order_id, $result);
+			return $result['payMsg'];
+		} else {
+			return false;
+		}
 	}
 
 	# 退款
-	public function refund($order_id, $cash, $refund_order_id = false)
+	public function refund($order_id, $cash, $order, $refund_order_id = false)
 	{
-		$request['merOrderId'] = $order_id;
-		$request['mid'] = $this->config['mchid'];
-		$request['tid'] = $this->config['key'];
+		if (isset($this->config['prefix']) && $this->config['prefix']) {
+			$request['merOrderId'] = $this->config['prefix'] . '_' . $order_id;
+			if ($refund_order_id) {
+				$request['refundOrderId'] = $this->config['prefix'] . '_' . $refund_order_id;
+			}
+		} else {
+			$request['merOrderId'] = $order_id;
+			if ($refund_order_id) {
+				$request['refundOrderId'] = $refund_order_id;
+			}
+		}
+		$request['mid'] = $this->config['mid'];
+		$request['tid'] = $this->config['tid'];
 		$request['instMid'] = 'MINIDEFAULT';
+		if (isset($order['param']['targetOrderId'])) {
+			$request['targetOrderId'] = $order['param']['targetOrderId'];
+		}
+		
 		$request['subAppId'] = $this->config['appid'];
 		$request['requestTimestamp'] = date("Y-m-d H:i:s");
-		$request['targetOrderId'] = $order_id;
 		$request['refundAmount'] = $cash * 100;
-		if ($refund_order_id) {
-			$request['refundOrderId'] = $refund_order_id;
-		}
 		
+		if (isset($order['param']['other']) && $order['param']['other']) {
+			$other = $order['param']['other'];
+			$other['per'] = $other['per']/100;
+			$request['platformAmount'] = $request['refundAmount'] * $other['per'];
+			$request['subOrders'] = array();
+			$request['subOrders']['mid'] = $other['mid'];
+			if (isset($this->config['prefix']) && $this->config['prefix']) {
+				$request['subOrders']['merOrderId'] = $this->config['prefix'] . '_' . $other['order_id'];
+				if ($refund_order_id) {
+					$request['subOrders']['refundOrderId'] = $this->config['prefix'] . '_' . $refund_order_id;
+				}
+			} else {
+				$request['subOrders']['merOrderId'] = $other['order_id'];
+				if ($refund_order_id) {
+					$request['subOrders']['refundOrderId'] = $refund_order_id;
+				}
+			}
+			
+			$request['subOrders']['totalAmount'] = $request['refundAmount'] - $request['platformAmount'];
+			$request['subOrders'] = array($request['subOrders']);
+		}
 		$result = Base::refund($request, $this->config);
 
 		if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
@@ -148,10 +215,11 @@ class Yspay extends Core
 	public function applet($order)
 	{
 		$result = array();
-		if (isset($order['prepay_id'])) {
+		if (isset($order['package'])) {
+			$prepay_id = str_replace('prepay_id=', '', $order['package']);
 			$result['time'] = $order['timeStamp'];
 			$result['nonce_str'] = $order['nonceStr'];
-			$result['prepay_id'] = $order['prepay_id'];
+			$result['prepay_id'] = $prepay_id;
 			$result['sign_type'] = $order['signType'];
 			$result['sign'] = $order['paySign'];
 		}
@@ -266,7 +334,7 @@ class Base
 	 */
 	static public function pay($param, $config)
 	{
-		$url = self::$refund_url;
+		$url = self::$pay_wechat_url;
 		$result = self::get($url, $param, $config);
 
 		return $result;
@@ -290,7 +358,7 @@ class Base
 	static public function refund($param, $config)
 	{
 
-		$url = self::$pay_wechat_url;
+		$url = self::$refund_url;
 		$result = self::get($url, $param, $config);
 
 		return $result;
@@ -396,10 +464,8 @@ class Base
 			Dever::alert('系统错误');
 		}
 		$result = Dever::json_decode($result);
-		print_r($param);
-		print_r($result);die;
 		if (isset($result['errCode'])) {
-			if ($result['errCode'] == '0000') {
+			if ($result['errCode'] == '0000' || $result['errCode'] == 'SUCCESS') {
 				return $result;
 			} elseif (isset($result['errInfo'])) {
 				Dever::alert($result['errInfo']);
@@ -409,5 +475,7 @@ class Base
 		} else {
 			Dever::alert('系统错误');
 		}
+
+		return $result;
     }
 }

+ 26 - 22
src/Api.php

@@ -34,14 +34,17 @@ class Api
 			$type = 1;
 		}
 		$this->init($param);
-		if ($this->order_id > 0) {
+		if ($this->order_id) {
 			$pay = Dever::db('pay/order')->one(array('order_id' => $this->order_id));
 			if ($pay && $pay['status'] == 1 && $pay['param']) {
 				$order = Dever::array_decode($pay['param']);
+				if (isset($order['payMsg']) && $order['payMsg']) {
+					return $order['payMsg'];
+				}
 				return $order;
 			}
 		}
-		return $this->method->order($this->account_id, $this->project_id, $this->uid, $this->username, $this->product_id, $this->name, $this->cash, $this->openid, $type, $this->order_id);
+		return $this->method->order($this->account_id, $this->project_id, $this->uid, $this->username, $this->product_id, $this->name, $this->cash, $this->openid, $type, $this->order_id, $this->other);
 	}
 
 	/**
@@ -51,10 +54,7 @@ class Api
 	 */
 	public function notify($param = array())
 	{
-		$this->account_id = Dever::input('account_id', false);
-		if (!$this->account_id) {
-			Dever::alert('没有账户信息');
-		}
+		$this->checkAccount($param);
 		$this->handle();
 		return $this->method->notify();
 	}
@@ -165,10 +165,7 @@ class Api
 	 */
 	public function search($param = array())
 	{
-		$this->account_id = Dever::input('account_id', false);
-		if (!$this->account_id) {
-			Dever::alert('没有账户信息');
-		}
+		$this->checkAccount($param);
 		$this->handle();
 		$this->order_id = $this->getParam($param, 'order_id');
 		return $this->method->search($this->order_id);
@@ -181,10 +178,7 @@ class Api
 	 */
 	public function refund($param = array())
 	{
-		$this->account_id = Dever::input('account_id', false);
-		if (!$this->account_id) {
-			Dever::alert('没有账户信息');
-		}
+		$this->checkAccount($param);
 		$this->handle();
 		$this->order_id = $this->getParam($param, 'order_id');
 		$this->refund_order_id = $this->getParam($param, 'refund_order_id');
@@ -202,9 +196,7 @@ 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->checkAccount($param);
 		$this->project_id = $this->getParam($param, 'project_id');
 		$this->uid = $this->getParam($param, 'uid');
 		$this->openid = $this->getParam($param, 'openid');
@@ -226,10 +218,6 @@ class Api
 			$this->order_id = false;
 		}
 
-		if (!$this->account_id && !$this->channel_id && !$this->system_source) {
-			Dever::alert('没有账户信息');
-		}
-
 		if (!$this->uid) {
 			Dever::alert('没有用户信息');
 		}
@@ -253,6 +241,22 @@ class Api
 		return $this->handle();
 	}
 
+	/**
+	 * 初始化 验证账户信息
+	 *
+	 * @return mixed
+	 */
+	private function checkAccount($param = array())
+	{
+		$this->channel_id = $this->getParam($param, 'channel_id');
+		$this->system_source = $this->getParam($param, 'system_source');
+		$this->account_id = $this->getParam($param, 'account_id');
+
+		if (!$this->account_id && !$this->channel_id && !$this->system_source) {
+			Dever::alert('没有账户信息');
+		}
+	}
+
 	/**
 	 * 初始化
 	 *
@@ -277,7 +281,7 @@ class Api
 		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));
+			$pay = Dever::db('pay/account')->one(array('channel_id' => $this->channel_id, 'system_source' => $this->system_source));
 		}
 
 		if (!$pay || ($pay && $pay['state'] != 1)) {