diff --git a/Application/Callback/Controller/NotifyController.class.php b/Application/Callback/Controller/NotifyController.class.php index 082b1cb..ca25728 100644 --- a/Application/Callback/Controller/NotifyController.class.php +++ b/Application/Callback/Controller/NotifyController.class.php @@ -9,7 +9,9 @@ use Think\Log; use Qiniu\json_decode; use Sdk\Controller\Ipa365Controller; use Base\Tool\Redis; - +use Org\Kudian\Config as KDConfig; +use Org\Kudian\Sign as KDSign; +use Org\Kudian\Response as KDResponse; /** * 支付回调控制器 @@ -927,4 +929,43 @@ class NotifyController extends BaseController echo "SUCCESS"; } + /** + * 酷点回调 + */ + public function kd_callback() { + $params = I('post.'); + + Log::write(serialize($params), Log::DEBUG); + $response = new KDResponse($params); + if (!$response->verify()) { + echo 'SIGN_VERIFY_FAIL'; + return; + } + if ($response->getResult('status') != 100) { + echo 'SUCCESS'; + return; + } + + $orderInfo['trade_no'] = $response->getResult('transaction_id'); + $orderInfo['out_trade_no'] = $response->getResult('out_trade_no'); + $orderInfo['money'] = round($response->getResult('fee') / 100, 2); + + $payWhere = substr($orderInfo['out_trade_no'], 0, 2); + + switch ($payWhere) { + case 'SP': + $result = $this->set_spend($orderInfo); + break; + case 'PF': + $result = $this->set_deposit($orderInfo); + break; + case 'AG': + $result = $this->set_agent($orderInfo); + break; + default: + exit('accident order data'); + break; + } + echo 'SUCCESS'; + } } \ No newline at end of file diff --git a/Application/Sdk/Controller/PayH5Controller.class.php b/Application/Sdk/Controller/PayH5Controller.class.php index efef447..0527430 100644 --- a/Application/Sdk/Controller/PayH5Controller.class.php +++ b/Application/Sdk/Controller/PayH5Controller.class.php @@ -14,6 +14,7 @@ use Sdk\Model\ValueDetailLogModel; use Sdk\Model\PayLimitConfModel; use Sdk\Model\PayChannelInterntionModel; use Sdk\Model\PayChannelIntentionModel; +use Org\Kudian\Api as KDApi; class PayH5Controller extends BaseController{ @@ -519,6 +520,44 @@ class PayH5Controller extends BaseController{ // redirect(U('Spend/notice',array('user_id'=>$user_id,'game_id'=>$game_id,'msg'=>$ret[0]['respMess'], 'user_token' => $this->userToken)));exit; } + } elseif($pay_info['channel'] == 6) { // 酷点 + $request['pay_way'] = 11; + $request['pay_status'] = 0; + $request['spend_ip'] = get_client_ip(); + $request['merchant_id'] = $pay_info['id']; + $request['merchant_way'] = 1; + $request['main_id'] = $main_id; + + $ordercheck = M("spend","tab_")->where(array('pay_order_number'=>$request["pay_order_number"]))->find(); + if ($ordercheck) { + $returl = U('Spend/notice',array('user_id'=>$ordercheck['user_id'],'game_id'=>$ordercheck['game_id'],'msg'=>'订单已经存在,请刷新充值页面重新下单!')); + echo json_encode(['code'=>0,'msg'=>'订单已经存在,请刷新充值页面重新下单!','wap'=>1]); + exit; + } + $this->add_spend($request,1); + + $notifyUrl = C('PAY_DOMAIN')."/callback.php/Notify/kd_callback"; + $backUrl = C('PAY_DOMAIN')."sdk.php/Spend/paycallback/orderno/{$request['pay_order_number']}/user_token/".$this->userToken."/paytype/weixinpay/game_id/{$game_id}"; + $body = '消费-' . $request["pay_order_number"]; + $response = KDApi::h5Pay($request['pay_order_number'], $request['price']*100, $notifyUrl, $backUrl, 'ALI_PAY', $body, get_client_ip()); + + if($response->isSuccess()){ + // 存入pay_url 继续支付 + M("spend", "tab_")->where([ + 'extend' => $request['extend'], + 'game_id' => $request['game_id'], + 'pay_order_number' => $request["pay_order_number"] + ])->save([ + 'pay_url' => $response->getResult('url') + ]); + $orderno = $request['pay_order_number']; + + } else { + echo json_encode(['code'=>1001,'msg'=> "请求发起失败【{$request->getMsg()}】,请选择其他支付方式或联系客服"]);exit; + + // redirect(U('Spend/notice',array('user_id'=>$user_id,'game_id'=>$game_id,'msg'=>$ret[0]['respMess'], 'user_token' => $this->userToken)));exit; + } + } else{ // 支付宝第三方 if( empty(C('goldpig.partner'))||empty(C('goldpig.wooolid'))){ // $this->set_message(1009, "fail", "支付参数未配置"); @@ -816,7 +855,45 @@ class PayH5Controller extends BaseController{ jsonOutput(0, "支付失败"); exit; } - }elseif(get_wx_pay_type() == 2){ + } elseif($pay_info['channel'] == 6) { // 酷点 + $request['pay_way'] = 11; + $request['pay_status'] = 0; + $request['spend_ip'] = get_client_ip(); + $request['merchant_id'] = $pay_info['id']; + $request['merchant_way'] = 2; + $request['main_id'] = $main_id; + + $ordercheck = M("spend","tab_")->where(array('pay_order_number'=>$request["pay_order_number"]))->find(); + if ($ordercheck) { + $returl = U('Spend/notice',array('user_id'=>$ordercheck['user_id'],'game_id'=>$ordercheck['game_id'],'msg'=>'订单已经存在,请刷新充值页面重新下单!')); + echo json_encode(['code'=>0,'msg'=>'订单已经存在,请刷新充值页面重新下单!','wap'=>1]); + exit; + } + $this->add_spend($request,1); + + $notifyUrl = C('PAY_DOMAIN')."/callback.php/Notify/kd_callback"; + $backUrl = C('PAY_DOMAIN')."sdk.php/Spend/paycallback/orderno/{$request['pay_order_number']}/user_token/".$this->userToken."/paytype/weixinpay/game_id/{$game_id}"; + $body = '消费-' . $request["pay_order_number"]; + $response = KDApi::h5Pay($request['pay_order_number'], $request['price']*100, $notifyUrl, $backUrl, 'WX_PAY', $body, get_client_ip()); + + if($response->isSuccess()){ + // 存入pay_url 继续支付 + M("spend", "tab_")->where([ + 'extend' => $request['extend'], + 'game_id' => $request['game_id'], + 'pay_order_number' => $request["pay_order_number"] + ])->save([ + 'pay_url' => $response->getResult('url') + ]); + $orderno = $request['pay_order_number']; + + } else { + echo json_encode(['code'=>1001,'msg'=> "请求发起失败【{$response->getMsg()}】,请选择其他支付方式或联系客服"]);exit; + + // redirect(U('Spend/notice',array('user_id'=>$user_id,'game_id'=>$game_id,'msg'=>$ret[0]['respMess'], 'user_token' => $this->userToken)));exit; + } + + } elseif(get_wx_pay_type() == 2){ if($request['code']==1){ $this->add_spend($request,1); diff --git a/ThinkPHP/Library/Org/Kudian/Api.php b/ThinkPHP/Library/Org/Kudian/Api.php new file mode 100644 index 0000000..507ef9e --- /dev/null +++ b/ThinkPHP/Library/Org/Kudian/Api.php @@ -0,0 +1,35 @@ +setContent([ + 'fee' => $amount, + 'out_trade_no' => $orderNumber, + 'body' => $body, + 'client_ip' => $clientIp, + 'notify_url' => $notifyUrl, + 'payment' => $payType, + 'sence_info' => [ + 'wap_url' => 'http://www.jianghuifa.cn', + 'wap_name' => '聚游', + 'return_url' => $backUrl, + ], + ]); + return self::getClient()->request($request); + } +} \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Kudian/Client.php b/ThinkPHP/Library/Org/Kudian/Client.php new file mode 100644 index 0000000..3d973ab --- /dev/null +++ b/ThinkPHP/Library/Org/Kudian/Client.php @@ -0,0 +1,43 @@ +post($request->getUrl(), $request->getParams()); + } catch (Exception $e) { + $response = json_encode([ + 'code' => -999, + 'msg' => '网络错误', + 'sign_type' => 'MD5', + 'sign' => '', + 'result' => json_encode([]), + ]); + } + return new Response($response); + } + + public function post($url, $params): string + { + $curl = curl_init(); + if (stripos($url, 'https://') !== false){ + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + } + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_POST, TRUE); + $headers = ['Content-type: application/json;charset=utf-8']; + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_POSTFIELDS, $params); + $response = curl_exec($curl); + curl_close($curl); + return $response; + } +} \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Kudian/Config.php b/ThinkPHP/Library/Org/Kudian/Config.php new file mode 100644 index 0000000..6a54432 --- /dev/null +++ b/ThinkPHP/Library/Org/Kudian/Config.php @@ -0,0 +1,15 @@ + 'KP10009694', + 'secret_key' => 'q5zZyyA071zS0SZR6xcz0gP0fFhtojqq', + ]; + + public static function get($key, $default = null) + { + return self::$params[$key] ?? $default; + } +} \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Kudian/Request/H5PayRequest.php b/ThinkPHP/Library/Org/Kudian/Request/H5PayRequest.php new file mode 100644 index 0000000..27777f1 --- /dev/null +++ b/ThinkPHP/Library/Org/Kudian/Request/H5PayRequest.php @@ -0,0 +1,12 @@ +content = $content; + } +} \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Kudian/Request/Request.php b/ThinkPHP/Library/Org/Kudian/Request/Request.php new file mode 100644 index 0000000..530f4aa --- /dev/null +++ b/ThinkPHP/Library/Org/Kudian/Request/Request.php @@ -0,0 +1,64 @@ +mchId = $mchId; + $this->secretKey = $secretKey; + $this->timestamp = time(); + $this->nonceStr = self::generateRandomString(); + $this->signType = 'MD5'; + } + + public function setContent(array $content) + { + $this->content = json_encode($content); + } + + final public function reset() + { + $this->timestamp = time(); + $this->nonceStr = self::generateRandomString(); + } + + final protected function generateRandomString($length = 16) + { + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + $string = ''; + for ( $i = 0; $i < $length; $i++ ) + $string .= $chars[mt_rand(0, strlen($chars) - 1)]; + + return $string; + } + + public function getParams() { + $params = [ + 'mch_id' => $this->mchId, + 'timestamp' => $this->timestamp, + 'nonce_str' => $this->nonceStr, + 'sign_type' => $this->signType, + 'content' => $this->content, + ]; + $params[Sign::SIGN_KEY] = Sign::generate($params, $this->secretKey); + return $params; + } + + public function getUrl() { + return $this->url; + } +} \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Kudian/Response.php b/ThinkPHP/Library/Org/Kudian/Response.php new file mode 100644 index 0000000..818984e --- /dev/null +++ b/ThinkPHP/Library/Org/Kudian/Response.php @@ -0,0 +1,86 @@ +parse($response); + } + + private function parse($response) + { + $data = null; + if (is_array($response)) { + $data = $response; + } else { + $data = json_decode($response); + } + if (!$data) { + $data = [ + 'code' => -998, + 'msg' => '数据异常', + 'sign' => '', + 'sign_type' => 'MD5', + 'result' => json_encode([]), + ]; + } + $this->data = $data; + $result = $data['result'] ? json_decode($data['result']) : []; + $this->result = $result; + $this->code = $data['code']; + $this->msg = $data['msg']; + $this->sign = $data['sign']; + $this->signType = $data['sign_type']; + } + + public function isSuccess() + { + return !($this->code < 0); + } + + public function getCode() + { + return $this->code; + } + + public function getMsg() + { + return $this->msg; + } + + public function getResult($key = null) + { + if ($key) { + return $this->result[$key] ?: null; + } + return $this->result; + } + + public function getSign() + { + return $this->sign; + } + + public function getSignType() + { + return $this->signType; + } + + public function verify() + { + return Sign::verify($this->data, Config::get('secret_key')); + } +} \ No newline at end of file diff --git a/ThinkPHP/Library/Org/Kudian/Sign.php b/ThinkPHP/Library/Org/Kudian/Sign.php new file mode 100644 index 0000000..0c044c5 --- /dev/null +++ b/ThinkPHP/Library/Org/Kudian/Sign.php @@ -0,0 +1,27 @@ + $value) { + $signStr .= $key . "=" . $value . "&"; + } + $signStr = rtrim($signStr, '&') . "&secret_key=" . $secretKey; + $sign = md5($signStr); + $sign = strtoupper($sign); + return $sign; + } + + public static function verify($params, $secretKey) + { + $sign = $params[self::SIGN_KEY] ?: null; + return self::generate($params, $secretKey) == $sign; + } +} \ No newline at end of file