dever 6 vuotta sitten
vanhempi
commit
dca32c1de4

+ 54 - 1
database/account.php

@@ -2,6 +2,12 @@
 
 $type = Dever::config('base', 'pay')->pay['type'];
 
+$box = array
+(
+	1 => '正式环境',
+	2 => '沙箱模式',
+);
+
 return array
 (
 	# 表名
@@ -50,6 +56,18 @@ return array
 			'control'	=> 'type',
 		),
 
+		'box'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '支付环境',
+			'default' 	=> '1',
+			'desc' 		=> '支付环境',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'radio',
+			'option'	=> $box,
+			'show'		=> 'alipay',
+		),
+
 		'mchid'		=> array
 		(
 			'type' 		=> 'varchar-150',
@@ -78,7 +96,7 @@ return array
 			'desc' 		=> 'appid',
 			'match' 	=> 'is_string',
 			'update'	=> 'text',
-			'show'	=> 'type=wechat,cmbc',
+			'show'	=> 'type=wechat,cmbc,alipay',
 		),
 
 		'appsecret'		=> array
@@ -103,6 +121,39 @@ return array
 			'show'	=> 'type=alipay',
 		),
 
+		'private_key'		=> array
+		(
+			'type' 		=> 'text-255',
+			'name' 		=> '商户私钥-https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.nBDxfy&treeId=58&articleId=103242&docType=1',
+			'default' 	=> '',
+			'desc' 		=> '商户私钥',
+			'match' 	=> 'is_string',
+			'update'	=> 'textarea',
+			'show'	=> 'type=alipay',
+		),
+
+		'm_public_key'		=> array
+		(
+			'type' 		=> 'varchar-1000',
+			'name' 		=> '商户公钥-https://b.alipay.com/order/pidAndKey.htm',
+			'default' 	=> '',
+			'desc' 		=> '商户公钥',
+			'match' 	=> 'is_string',
+			'update'	=> 'textarea',
+			'show'	=> 'type=alipay',
+		),
+
+		'public_key'		=> array
+		(
+			'type' 		=> 'varchar-1000',
+			'name' 		=> '支付宝公钥-https://b.alipay.com/order/pidAndKey.htm',
+			'default' 	=> '',
+			'desc' 		=> '支付宝公钥',
+			'match' 	=> 'is_string',
+			'update'	=> 'textarea',
+			'show'	=> 'type=alipay',
+		),
+
 		'file_cert'		=> array
 		(
 			'type' 		=> 'varchar-150',
@@ -112,6 +163,7 @@ return array
 			'match' 	=> 'option',
 			'update'	=> 'upload',
 			'key'		=> '4',
+			'show'	=> 'type=wechat',
 		),
 
 		'file_key'		=> array
@@ -123,6 +175,7 @@ return array
 			'match' 	=> 'option',
 			'update'	=> 'upload',
 			'key'		=> '4',
+			'show'	=> 'type=wechat',
 		),
 
 		'timeout'		=> array

+ 79 - 88
lib/Alipay.php

@@ -4,20 +4,13 @@ Dever::apply('sdk/alipay', 'pay');
 
 class Alipay extends Core
 {
+	private $signType = 'RSA2';
 	public function __construct($config)
 	{
 		$project = Dever::project('pay');
-		$this->config = new \Cmbc\Setting();
 		# 通知接口
 		$config['notify'] = $this->url($config['type'], $config['id']);
-		# 证书
-		$config['ssl'] = array
-		(
-			'cert' => $config['file_cert'],
-			'key' => $config['file_key'],
-		);
-
-		$this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout']);
+		$this->config = $config;
 	}
 
 	/**
@@ -25,23 +18,44 @@ class Alipay extends Core
 	 */
 	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;
+		$type = Dever::input('type');
+		if ($type == 1) {
+			$input = $_GET;
+			unset($input['account_id']);
+			$result = $this->check($input);
+			if ($result) {
+				$status = 1;
+			} else {
+				$status = 2;
+			}
+			$refer = Dever::input('refer');
+			$refer = urldecode($refer) . '&status=' . $status;
+			Dever::location($refer);
 		} else {
-			echo 'success=N';die;
+			$input = $_POST;
+			if (!$input) {
+				echo 'fail';die;
+			}
+			unset($input['account_id']);
+			$this->log('支付回调-初始化', $input);
+
+			$result = $this->check($input);
+
+			if ($result) {
+				$this->log('支付回调-获取数据', $result);
+
+				if($input['trade_status'] == 'TRADE_FINISHED') {
+					#退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
+					$this->updateOrder($result['out_trade_no'], $result['total_amount'], '已超过退款期限');
+				} elseif ($input['trade_status'] == 'TRADE_SUCCESS') {
+					#付款完成后,支付宝系统发送该交易状态通知
+					$this->updateOrder($result['out_trade_no'], $result['total_amount']);
+				}
+				
+				echo 'success';die;
+			} else {
+				echo 'fail';die;
+			}
 		}
 	}
 
@@ -51,27 +65,41 @@ class Alipay extends Core
 	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);
+		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
 
-		$tools = new \Cmbc\Handle();
+		$aop = new \AopClient();
 
-		$order = $tools->get('order', $this->config);
-
-		$request['mhtOrderNo'] = $order_id;
-		$request['mhtOrderName'] = $name;
-		$request['mhtOrderAmt'] = $cash * 100;
-		$request['mhtOrderDetail'] = $name;
-		$request['mhtOrderStartTime'] = date("YmdHis");
-		$request['notifyUrl'] = $this->config->getNotifyUrl();
-		$request['outputType'] = 1;
-		if (!$openid) {
-			$request['consumerId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
-		} else {
-			$request['consumerId'] = $openid;
+		$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
+		if ($this->config['box'] == 2) {
+			$aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
 		}
 		
-		$result = $order->request($request);
-		$this->updateOrderParam($order_id, $result);
+		$aop->appId = $this->config['appid'];
+		$aop->rsaPrivateKey = $this->config['private_key'];
+		$aop->alipayrsaPublicKey = $this->config['public_key'];
+		$aop->signType = $this->signType;
+		// 开启页面信息输出
+		$aop->debugInfo = true;
+
+		$request['out_trade_no'] = $order_id;
+		$request['body'] = $name;
+		$request['total_amount'] = $cash;
+		$request['subject'] = $name;
+		$request['timeout_express'] = $this->config['timeout'];
+		$content = json_encode($request, JSON_UNESCAPED_UNICODE);
+
+		$request = new \AlipayTradeWapPayRequest();
+	
+		$request->setNotifyUrl($this->config['notify']);
+		$request->setReturnUrl($this->config['refer']);
+		$request->setBizContent($content);
+
+		if ($type == 1) {
+			$result = $aop->pageExecute($request, 'post');
+		} else {
+			$result = $aop->Execute($request);
+		}
+
 		return $result;
 	}
 
@@ -92,13 +120,6 @@ class Alipay extends Core
 	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;
 	}
 
@@ -108,45 +129,7 @@ class Alipay extends Core
 	 */
 	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;
+		return $order;
 	}
 
 	private function getType($type)
@@ -163,4 +146,12 @@ class Alipay extends Core
 
 		return $type;
 	}
+
+	private function check($data)
+	{
+		$aop = new \AopClient();
+		$aop->alipayrsaPublicKey = $this->config['public_key'];
+		$result = $aop->rsaCheckV1($data, $this->config['public_key'], $this->signType);
+		return $result;
+	}
 }

+ 1 - 4
sdk/alipay.php

@@ -1,6 +1,3 @@
 <?php
 
-Dever::apply('sdk/wechat/WxPay.Api', 'pay');
-Dever::apply('sdk/wechat/WxPay.JsApiPay', 'pay');
-Dever::apply('sdk/wechat/WxPay.Config', 'pay');
-Dever::apply('sdk/wechat/WxPay.Notify', 'pay');
+Dever::apply('sdk/alipay/AopSdk', 'pay');

+ 0 - 0
sdk/alipay/config.php → sdk/alipay/demo/config.php


+ 0 - 0
sdk/alipay/index.php → sdk/alipay/demo/index.php


+ 0 - 0
sdk/alipay/lib/alipay_notify.class.php → sdk/alipay/demo/lib/alipay_notify.class.php


+ 0 - 0
sdk/alipay/log.txt → sdk/alipay/demo/log.txt


+ 0 - 0
sdk/alipay/notify_url.php → sdk/alipay/demo/notify_url.php


+ 0 - 0
sdk/alipay/readme.txt → sdk/alipay/demo/readme.txt


+ 0 - 0
sdk/alipay/return_url.php → sdk/alipay/demo/return_url.php


+ 0 - 0
sdk/alipay/wappay/buildermodel/AlipayDataDataserviceBillDownloadurlQueryContentBuilder.php → sdk/alipay/demo/wappay/buildermodel/AlipayDataDataserviceBillDownloadurlQueryContentBuilder.php


+ 0 - 0
sdk/alipay/wappay/buildermodel/AlipayTradeCloseContentBuilder.php → sdk/alipay/demo/wappay/buildermodel/AlipayTradeCloseContentBuilder.php


+ 0 - 0
sdk/alipay/wappay/buildermodel/AlipayTradeFastpayRefundQueryContentBuilder.php → sdk/alipay/demo/wappay/buildermodel/AlipayTradeFastpayRefundQueryContentBuilder.php


+ 0 - 0
sdk/alipay/wappay/buildermodel/AlipayTradeQueryContentBuilder.php → sdk/alipay/demo/wappay/buildermodel/AlipayTradeQueryContentBuilder.php


+ 0 - 0
sdk/alipay/wappay/buildermodel/AlipayTradeRefundContentBuilder.php → sdk/alipay/demo/wappay/buildermodel/AlipayTradeRefundContentBuilder.php


+ 0 - 0
sdk/alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php → sdk/alipay/demo/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php


+ 0 - 0
sdk/alipay/wappay/buildermodel/ContentBuilder.php → sdk/alipay/demo/wappay/buildermodel/ContentBuilder.php


+ 0 - 0
sdk/alipay/wappay/close.php → sdk/alipay/demo/wappay/close.php


+ 0 - 0
sdk/alipay/wappay/datadownioad.php → sdk/alipay/demo/wappay/datadownioad.php


+ 0 - 0
sdk/alipay/wappay/pay.php → sdk/alipay/demo/wappay/pay.php


+ 0 - 0
sdk/alipay/wappay/query.php → sdk/alipay/demo/wappay/query.php


+ 0 - 0
sdk/alipay/wappay/refund.php → sdk/alipay/demo/wappay/refund.php


+ 0 - 0
sdk/alipay/wappay/refundquery.php → sdk/alipay/demo/wappay/refundquery.php


+ 0 - 0
sdk/alipay/wappay/service/AlipayTradeService.php → sdk/alipay/demo/wappay/service/AlipayTradeService.php


+ 4 - 0
src/Api.php

@@ -177,6 +177,10 @@ class Api
 	{
 		$pay = Dever::db('pay/account')->one($this->account_id);
 		$method = '\\Pay\\Lib\\' . ucwords($pay['type']);
+		if (isset($this->refer) && $this->refer) {
+			$pay['refer'] = $this->refer;
+		}
+		
 		$this->method = new $method($pay);
 		return $this;
 	}