dever 6 years ago
parent
commit
3ea8e0f9c9
4 changed files with 39 additions and 23 deletions
  1. 1 1
      config/base.php
  2. 14 16
      lib/Cmbc.php
  3. 16 6
      sdk/cmbc/Api/Core.php
  4. 8 0
      sdk/cmbc/Api/UnifiedOrder.php

+ 1 - 1
config/base.php

@@ -9,7 +9,7 @@ $config['base'] = array
 		(
 			'wechat' => '微信支付',
 			//'alipay' => '支付宝',
-			//'cmbc' => '招商支付',
+			'cmbc' => '招商支付',
 		),
 
 		'status' => array

+ 14 - 16
lib/Cmbc.php

@@ -29,7 +29,6 @@ class Cmbc extends Core
 		$tools = new \Cmbc\Handle();
 		$callback = $tools->get('notify', $this->config);
 		$result = $callback->request(Dever::input(), $this);
-		print_r($result);die;
 		$this->log('支付回调-获取数据', $result);
 		$this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt'], $msg);
 	}
@@ -37,7 +36,7 @@ class Cmbc extends Core
 	/**
 	 * 获取统一下单的基本信息
 	 */
-	public function order($account_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);
 		$order_id = $this->createOrder($uid, $username, $account_id, $product_id, $name, $cash, $this->config->getType(), $order_id);
@@ -52,11 +51,13 @@ class Cmbc extends Core
 		$request['mhtOrderStartTime'] = date("YmdHis");
 		$request['notifyUrl'] = $this->config->getNotifyUrl();
 		$request['outputType'] = 1;
-		$request['mhtSubAppId'] = '?';
-		$request['consumerId'] = $openid;
-
+		if (!$openid) {
+			$request['consumerId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
+		} else {
+			$request['consumerId'] = $openid;
+		}
+		
 		$result = $order->request($request);
-		print_r($result);die;
 		$this->updateOrderParam($order_id, $result);
 		return $result;
 	}
@@ -77,18 +78,15 @@ class Cmbc extends Core
 	 */
 	public function applet($order)
 	{
+		$result = array();
 		if (isset($order['prepay_id'])) {
-
-			$string = 'appId='.$this->config->getAppId().'&nonceStr='.$order['nonce_str'].'&package=prepay_id='.$order['prepay_id'].'&signType='.$order['sign_type'].'&timeStamp='.$order['time'].'&key='.$this->config->getKey();
-			if($order['sign_type'] == "MD5"){
-				$string = md5($string);
-			} else {
-				$string = hash_hmac("sha256", $string, $this->config->getKey());
-			}
-
-			$order['sign'] = $string;
+			$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 $order;
+		return $result;
 	}
 
 

+ 16 - 6
sdk/cmbc/Api/Core.php

@@ -18,8 +18,10 @@ abstract class Core
 	{
 		$this->setting = $setting;
 		$this->init();
-		$this->param['appId'] = $this->setting->getAppId();
-		$this->param['mhtSubMchId'] = $this->setting->getSubMchId();
+		$appid = explode('|', $this->setting->getAppId());
+		$this->param['appId'] = $appid[0];
+		$this->param['mhtSubAppId'] = $appid[1];
+		//$this->param['mhtSubMchId'] = $this->setting->getSubMchId();
 	}
 
 	protected function setParam($key, $default = false, $isMust = true)
@@ -69,7 +71,15 @@ abstract class Core
 		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 		if ($this->method == 'post' && $this->param) {
 			curl_setopt($curl, CURLOPT_POST, 1);
-			curl_setopt($curl, CURLOPT_POSTFIELDS, $this->param);
+
+			$string = '';
+			foreach ($this->param as $k => $v) {
+				if ($v || $v == 0) {
+					$string .= $k . '=' . $v . '&';
+				}
+			}
+			$string = substr($string, 0, -1);
+			curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
 		}
 		
 		$data = curl_exec($curl);
@@ -77,10 +87,10 @@ abstract class Core
 		parse_str($data, $result);
 		if (isset($result['responseCode']) && $result['responseCode'] == 'A002') {
 			# 失败
-			
+			$this->setError($result['responseMsg']);
+			return false;
 		}
-		print_r($result);die;
-		return false;
+		return $result;
 	}
 
 	protected function setError($msg)

+ 8 - 0
sdk/cmbc/Api/UnifiedOrder.php

@@ -89,6 +89,14 @@ class UnifiedOrder extends Core
 
 	public function response($response)
 	{
+		if (isset($response['tn']) && $response['tn']) {
+
+			$tn = urldecode($response['tn']);
+			parse_str($tn, $result);
+			return $result;
+		} else {
+			return 'error';
+		}
 		return $response;
 	}
 }