dever 3 年 前
コミット
9cc610a0f3
3 ファイル変更213 行追加4 行削除
  1. 1 0
      config/base.php
  2. 4 4
      database/account.php
  3. 208 0
      lib/Yspay.php

+ 1 - 0
config/base.php

@@ -9,6 +9,7 @@ $config['base'] = array
 		(
 			'wechat' => '微信支付',
 			'alipay' => '支付宝',
+			'yspay' => '银商支付',
 			'cmbc' => '招商支付',
 			'test' => '测试支付',
 		),

+ 4 - 4
database/account.php

@@ -79,9 +79,9 @@ return array
 		'system_source'		=> array
 		(
 			'type' 		=> 'tinyint-1',
-			'name' 		=> '支付平台',
+			'name' 		=> '所属平台',
 			'default' 	=> '1',
-			'desc' 		=> '支付平台',
+			'desc' 		=> '所属平台',
 			'match' 	=> 'is_numeric',
 			'update'	=> 'radio',
 			'option'	=> $system_source,
@@ -130,7 +130,7 @@ return array
 			'desc' 		=> 'appid',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
-			'show'	=> 'type=wechat,cmbc,alipay',
+			'show'	=> 'type=wechat,cmbc,alipay,yspay',
 		),
 
 		'appsecret'		=> array
@@ -141,7 +141,7 @@ return array
 			'desc' 		=> 'appsecret',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
-			'show'	=> 'type=wechat,cmbc',
+			'show'	=> 'type=wechat,cmbc,yspay',
 		),
 
 		'account'		=> array

+ 208 - 0
lib/Yspay.php

@@ -0,0 +1,208 @@
+<?php namespace Pay\Lib;
+use Dever;
+
+class Yspay extends Core
+{
+	public function __construct($config)
+	{
+		$project = Dever::project('pay');
+		# 通知接口
+		$config['notify'] = $this->url($config['type'], $config['id']);
+		$this->config = $config;
+	}
+
+	/**
+	 * 通知
+	 */
+	public function notify()
+	{
+		$input = file_get_contents("php://input");
+		if ($input) {
+			parse_str($input, $input);
+		} else {
+			$input = Dever::input();
+		}
+		$this->log('支付回调-初始化', $input);
+		
+		$tools = new \Cmbc\Handle();
+		$callback = $tools->get('notify', $this->config);
+		$result = $callback->request($input, $this);
+		if ($result) {
+			$this->log('支付回调-获取数据', $result);
+			$this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt']);
+			echo 'success=Y';die;
+		} else {
+			echo 'success=N';die;
+		}
+	}
+
+	/**
+	 * 获取统一下单的基本信息
+	 */
+	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);
+		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
+
+		$request['merOrderId'] = $order_id;
+		$request['mid'] = $this->config['mchid'];
+		$request['tid'] = $this->config['mchid'];
+		$request['instMid'] = 'MINIDEFAULT';
+		$request['totalAmount'] = $cash * 100;
+		$request['subAppId'] = $this->config['appid'];
+		$request['requestTimestamp'] = date("Y-m-d H:i:s");
+		$request['expireTime'] = $this->config['timeout'];
+		$request['notifyUrl'] = $this->config['notify'];
+		$request['tradeType'] = 'MINI';
+		# 是否启用分账
+		$request['divisionFlag'] = true;
+		# 平台分账金额
+		$request['platformAmount'] = $request['totalAmount'] * 0.1;
+		if ($request['divisionFlag']) {
+			$request['subOrders'] = array();
+			$request['subOrders']['mid'] = 1;
+			$request['subOrders']['totalAmount'] = $request['totalAmount'] - $request['platformAmount'];
+		}
+		if (!$openid) {
+			# 测试的openid
+			$request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
+		} else {
+			$request['subOpenId'] = $openid;
+		}
+		
+		$result = $this->request('unified-order', $request);
+		$this->updateOrderParam($order_id, $result);
+		return $result;
+	}
+
+	# 退款
+	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)
+	{
+		$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;
+	}
+
+	/**
+	 * 获取二维码支付
+	 */
+	public function qrcode($order, $refer)
+	{
+		$notify = new \NativePay();
+		$result = $notify->GetPayUrl($order);
+		$url = $result['code_url'];
+		return $url;
+	}
+
+	/**
+	 * 获取小程序支付
+	 */
+	public function applet($order)
+	{
+		$result = array();
+		if (isset($order['prepay_id'])) {
+			$result['time'] = $order['timeStamp'];
+			$result['nonce_str'] = $order['nonceStr'];
+			$result['prepay_id'] = $order['prepay_id'];
+			$result['sign_type'] = $order['signType'];
+			$result['sign'] = $order['paySign'];
+		}
+		return $result;
+	}
+
+
+	/**
+	 * 获取页面支付
+	 */
+	public function page($order, $refer)
+	{
+		$refer = urldecode($refer);
+		$tools = new \JsApiPay($this->config);
+		$info = $tools->GetJsApiParameters($order);
+
+		$html = '<script type="text/javascript">
+		function jsApiCall()
+		{
+			WeixinJSBridge.invoke(
+				"getBrandWCPayRequest",
+				'.$info.',
+				function(res){
+					//WeixinJSBridge.log(res.err_msg);
+					if(res.err_msg == "get_brand_wcpay_request:ok")
+					{
+						location.href = "'.$refer.'";
+					} else {
+						alert(res.err_code+res.err_desc+res.err_msg);
+					}
+				}
+			);
+		}
+
+		function callpay()
+		{
+			if (typeof WeixinJSBridge == "undefined"){
+			    if( document.addEventListener ){
+			        document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
+			    }else if (document.attachEvent){
+			        document.attachEvent("WeixinJSBridgeReady", jsApiCall); 
+			        document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
+			    }
+			}else{
+			    jsApiCall();
+			}
+		}
+		callpay();
+		</script>';
+
+		return $html;
+	}
+
+	private function getType($type)
+	{
+		switch ($type) {
+			case 1:
+				$type = 'JSAPI';
+				break;
+			
+			case 2:
+				$type = 'NATIVE';
+				break;
+
+			case 3:
+				$type = 'APP';
+				break;
+
+			case 4:
+				$type = 'MWEB';
+				break;
+		}
+
+		return $type;
+	}
+}