dever 3 years ago
parent
commit
6223086575
6 changed files with 271 additions and 45 deletions
  1. 1 0
      config/base.php
  2. 11 0
      database/order.php
  3. 24 0
      lib/Core.php
  4. 0 16
      lib/Wechat.php
  5. 232 28
      lib/Yspay.php
  6. 3 1
      src/Api.php

+ 1 - 0
config/base.php

@@ -21,6 +21,7 @@ $config['base'] = array
 			3 => '支付失败',
 			3 => '支付失败',
 			4 => '申请退款中',
 			4 => '申请退款中',
     		5 => '已退款',
     		5 => '已退款',
+    		6 => '部分退款',
 		),
 		),
 
 
 		# 开放的支付方法 具体数值参考system_source_key Dever::config('base')->system_source_key
 		# 开放的支付方法 具体数值参考system_source_key Dever::config('base')->system_source_key

+ 11 - 0
database/order.php

@@ -152,6 +152,17 @@ return array
 			'list'		=> true,
 			'list'		=> true,
 		),
 		),
 
 
+		'refund_cash'		=> array
+		(
+			'type' 		=> 'varchar-11',
+			'name' 		=> '退款金额',
+			'default' 	=> '',
+			'desc' 		=> '退款金额',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
 		'status'		=> array
 		'status'		=> array
 		(
 		(
 			'type' 		=> 'tinyint-1',
 			'type' 		=> 'tinyint-1',

+ 24 - 0
lib/Core.php

@@ -172,6 +172,30 @@ class Core
 		return str_replace('?', '', Dever::url('api.notify?account_id=' . $account_id, 'pay'));
 		return str_replace('?', '', Dever::url('api.notify?account_id=' . $account_id, 'pay'));
 	}
 	}
 
 
+	# 退款
+	public function refundByOrder($order_id, $refund_order_id = false, $refund_cash = false)
+	{
+		$info = Dever::db('pay/order')->one(array('order_id' => $order_id));
+		if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 6)) {
+			$cash = $refund_cash ? $refund_cash : $info['cash'];
+			$state = $this->refund($info['order_id'], $cash, $refund_order_id);
+			if ($state) {
+				if ($refund_order_id) {
+					$status = 6;
+				} else {
+					$status = 5;
+				}
+				Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => $status, 'refund_cash' => $cash));
+
+				return 'yes';
+			} else {
+				return false;
+			}
+		}
+
+		return false;
+	}
+
 	/**
 	/**
 	 * 写日志
 	 * 写日志
 	 */
 	 */

+ 0 - 16
lib/Wechat.php

@@ -54,22 +54,6 @@ class Wechat extends Core
 		}
 		}
 	}
 	}
 
 
-	# 退款
-	public function refundByOrder($order_id)
-	{
-		$info = Dever::db('pay/order')->one(array('order_id' => $order_id));
-		if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 5)) {
-			$this->refund($info['order_id'], $info['cash']);
-
-			Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => 5));
-
-			return $result;
-		}
-
-		return false;
-	}
-
-
 	# 退款
 	# 退款
 	public function refund($order_id, $cash)
 	public function refund($order_id, $cash)
 	{
 	{

+ 232 - 28
lib/Yspay.php

@@ -23,6 +23,10 @@ class Yspay extends Core
 			$input = Dever::input();
 			$input = Dever::input();
 		}
 		}
 		$this->log('支付回调-初始化', $input);
 		$this->log('支付回调-初始化', $input);
+		return;
+
+		ksort($input);
+		$input = http_build_query($input);
 		
 		
 		$tools = new \Cmbc\Handle();
 		$tools = new \Cmbc\Handle();
 		$callback = $tools->get('notify', $this->config);
 		$callback = $tools->get('notify', $this->config);
@@ -30,9 +34,9 @@ class Yspay extends Core
 		if ($result) {
 		if ($result) {
 			$this->log('支付回调-获取数据', $result);
 			$this->log('支付回调-获取数据', $result);
 			$this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt']);
 			$this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt']);
-			echo 'success=Y';die;
+			echo 'SUCCESS';die;
 		} else {
 		} else {
-			echo 'success=N';die;
+			echo 'FAILED';die;
 		}
 		}
 	}
 	}
 
 
@@ -42,11 +46,11 @@ 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)
 	{
 	{
 		$trade_type = $this->getType($type);
 		$trade_type = $this->getType($type);
-		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
+		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
 
 
 		$request['merOrderId'] = $order_id;
 		$request['merOrderId'] = $order_id;
 		$request['mid'] = $this->config['mchid'];
 		$request['mid'] = $this->config['mchid'];
-		$request['tid'] = $this->config['mchid'];
+		$request['tid'] = $this->config['key'];
 		$request['instMid'] = 'MINIDEFAULT';
 		$request['instMid'] = 'MINIDEFAULT';
 		$request['totalAmount'] = $cash * 100;
 		$request['totalAmount'] = $cash * 100;
 		$request['subAppId'] = $this->config['appid'];
 		$request['subAppId'] = $this->config['appid'];
@@ -71,42 +75,59 @@ class Yspay extends Core
 			$request['subOpenId'] = $openid;
 			$request['subOpenId'] = $openid;
 		}
 		}
 		
 		
-		$result = $this->request('unified-order', $request);
+		$result = Base::pay($request, $this->config);
 		$this->updateOrderParam($order_id, $result);
 		$this->updateOrderParam($order_id, $result);
 		return $result;
 		return $result;
 	}
 	}
 
 
 	# 退款
 	# 退款
-	public function refundByOrder($order_id)
+	public function refund($order_id, $cash, $refund_order_id = false)
 	{
 	{
-		$info = Dever::db('pay/order')->one(array('order_id' => $order_id));
-		if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 5)) {
-			$this->refund($info['order_id'], $info['cash']);
-
-			Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => 5));
-
-			return $result;
+		$request['merOrderId'] = $order_id;
+		$request['mid'] = $this->config['mchid'];
+		$request['tid'] = $this->config['key'];
+		$request['instMid'] = 'MINIDEFAULT';
+		$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;
 		}
 		}
+		
+		$result = Base::refund($request, $this->config);
 
 
+		if (isset($result['refundStatus']) && $result['refundStatus'] == 'SUCCESS') {
+			return true;
+		}
 		return false;
 		return false;
 	}
 	}
 
 
+	# 查询
+	public function query($order_id)
+	{
+		$request['merOrderId'] = $order_id;
+		$request['mid'] = $this->config['mchid'];
+		$request['tid'] = $this->config['key'];
+		$request['instMid'] = 'MINIDEFAULT';
+		$request['requestTimestamp'] = date("Y-m-d H:i:s");
+		
+		$result = Base::refund($request, $this->config);
+
+		return $result;
+	}
+
+	# 关闭订单
+	public function close($order_id)
+	{
+		$request['merOrderId'] = $order_id;
+		$request['mid'] = $this->config['mchid'];
+		$request['tid'] = $this->config['key'];
+		$request['instMid'] = 'MINIDEFAULT';
+		$request['requestTimestamp'] = date("Y-m-d H:i:s");
+		
+		$result = Base::close($request, $this->config);
 
 
-	# 退款
-	public function refund($order_id, $cash)
-	{
-		$out_trade_no = $order_id;
-		$cash = $cash * 100;
-		$total_fee = $cash;
-		$refund_fee = $cash;
-		$input = new \WxPayRefund();
-		$input->SetOut_trade_no($out_trade_no);
-		$input->SetTotal_fee($total_fee);
-		$input->SetRefund_fee($refund_fee);
-
-	    $input->SetOut_refund_no($out_trade_no . '_' . time());
-	    $input->SetOp_user_id($this->config->GetMerchantId());
-		$result = \WxPayApi::refund($this->config, $input);
 		return $result;
 		return $result;
 	}
 	}
 
 
@@ -206,4 +227,187 @@ class Yspay extends Core
 
 
 		return $type;
 		return $type;
 	}
 	}
+}
+
+class Base
+{
+	static $test_host = 'https://test-api-open.chinaums.com/';
+	static $host = 'https://api-mop.chinaums.com/';
+	static $signMethod = 'SHA256';  //签名方式
+
+	# 获取token
+	static $token_url = 'v1/token/access';
+
+	# 微信下单支付
+	static $pay_wechat_url = 'v1/netpay/wx/unified-order';
+
+	# 支付宝下单支付
+	static $pay_alipay_url = 'v1/netpay/trade/create';
+
+	# 云闪付下单支付
+	static $pay_uac_url = 'v1/netpay/uac/mini-order';
+
+	# 交易查询
+	static $query_url = 'v1/netpay/query';
+
+	# 退款
+	static $refund_url = 'v1/netpay/refund';
+
+	# 退款查询
+	static $refund_query_url = 'v1/netpay/refund-query';
+
+	# 订单关闭
+	static $close_url = 'v1/netpay/close';
+
+
+	//===================== 支付相关 ==============================
+	/**
+	 * 支付交易
+	 */
+	static public function pay($param, $config)
+	{
+		$url = self::$refund_url;
+		$result = self::get($url, $param, $config);
+
+		return $result;
+	}
+
+	/**
+	 * 支付撤销
+	 */
+	static public function close()
+	{
+		$url = self::$close_url;
+		$result = self::get($url, $param, $config);
+
+		return $result;
+
+	}
+
+	/**
+	 * 交易退款
+	 */
+	static public function refund($param, $config)
+	{
+
+		$url = self::$pay_wechat_url;
+		$result = self::get($url, $param, $config);
+
+		return $result;
+	}
+
+	/**
+	 * 交易查询
+	 */
+	static public function query()
+	{
+		$url = self::$query_url;
+		$result = self::get($url, $param, $config);
+
+		return $result;
+	}
+
+	/**
+	 * 交易退款查询
+	 */
+	static public function refundQuery()
+	{
+		$url = self::$refund_query_url;
+		$result = self::get($url, $param, $config);
+
+		return $result;
+	}
+
+	//====================== 获取调用凭证===========================
+	/**
+	 * 通用调用凭证获取
+	 * 默认使用token方式,使用签名方式需要传入参数
+	 */
+	static public function getAuth($type = 'token', $param, $config)
+	{
+		if ($type === 'token') {
+			return self::getAccessTokenByToken($config);
+		}
+		return self::getAccessTokenBySig($param, $config);
+	}
+
+	/**
+	 * 方式一,OPEN-ACCESS-TOKEN方式
+	 */
+	static function getAccessTokenByToken($config)
+	{
+		$param = [
+			'appId' => $config['appid'],
+			'timestamp' => date('YmdHis'),
+			'nonce' => md5(uniqid(microtime(true),true)),
+			'signMethod' => self::$signMethod,
+		];
+		$param['signature'] = self::signature($param, $config['appsecret']);
+
+		$result = self::get(self::$token_url, $param, $config);
+
+		if (isset($result['errCode']) && isset($result['accessToken'])) {
+			return 'OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken'];
+		}
+		return '';
+	}
+
+	/**
+	 * 方式二,OPEN-BODY-SIG方式
+	 */
+	static function getAccessTokenBySig($data, $config)
+	{
+		$param = [
+			'AppId' => $config['appid'],
+			'Timestamp' => date('YmdHis'),
+			'Nonce' => md5(uniqid(microtime(true),true))
+		];
+		return 'OPEN-BODY-SIG AppId="'.$param['AppId'].'", Timestamp="'.$param['Timestamp'].'", Nonce="'.$param['Nonce'].'", Signature="'.self::signature2($data, $param, $config['appsecret']).'"';
+	}
+
+	/**
+	 * 计算签名,方式一
+	 */
+	static function signature($param, $appsecret)
+	{
+		return bin2hex(hash(self::$signMethod, $param['appId'].$param['timestamp'].$param['nonce'].$appsecret, true));
+	}
+
+	/**
+	 * 计算签名,方式二
+	 */
+	static function signature2($data, $param, $appsecret)
+	{	
+		$str = bin2hex(hash('sha256', Dever::json_encode($data), true));
+		return base64_encode(hash_hmac('sha256', $param['AppId'].$param['Timestamp'].$param['Nonce'].$str, $appsecret, true));
+	}
+
+	/**
+	 * 获取信息
+	 */
+    static function get($url, $param, $config)
+    {
+    	$url = $config['box'] == 1 ? self::$host . $url : self::$test_host . $url;
+    	
+    	$header = array();
+    	$header['Authorization'] = self::getAuth('sig', $param, $config);
+		$result = Dever::curl($url, $param, 'post', true, $header);
+		if (strstr($result, '<html><head>')) {
+			Dever::alert('系统错误');
+		}
+		$result = Dever::json_decode($result);
+		print_r($param);
+		print_r($result);die;
+		if (isset($result['errCode'])) {
+			if ($result['errCode'] == '0000') {
+				return $result;
+			} elseif (isset($result['errInfo'])) {
+				Dever::alert($result['errInfo']);
+			} elseif (isset($result['errMsg'])) {
+				Dever::alert($result['errMsg']);
+			}
+		} else {
+			Dever::alert('系统错误');
+		}
+    }
 }
 }

+ 3 - 1
src/Api.php

@@ -187,7 +187,9 @@ class Api
 		}
 		}
 		$this->handle();
 		$this->handle();
 		$this->order_id = $this->getParam($param, 'order_id');
 		$this->order_id = $this->getParam($param, 'order_id');
-		return $this->method->refundByOrder($this->order_id);
+		$this->refund_order_id = $this->getParam($param, 'refund_order_id');
+		$this->refund_cash = $this->getParam($param, 'refund_cash');
+		return $this->method->refundByOrder($this->order_id, $this->refund_order_id, $this->refund_cash);
 	}
 	}
 
 
 	/**
 	/**