From 4ead08e2b2e4d56ff0c5868317bbe430670348aa Mon Sep 17 00:00:00 2001 From: zcy Date: Sun, 25 Jul 2021 16:29:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=9C=9C=E8=9C=82=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/ServicePay/DeWuDianServices.php | 364 +++++++++++++++++++++++++ app/ServicePay/DongJinPayServices.php | 12 +- app/ServicePay/MiFengServices.php | 358 ++++++++++++++++++++++++ app/ServicePay/PayApiProvidesTrait.php | 4 + config/adconfig.php | 2 + config/whitelist.php | 11 +- 6 files changed, 744 insertions(+), 7 deletions(-) create mode 100644 app/ServicePay/DeWuDianServices.php create mode 100644 app/ServicePay/MiFengServices.php diff --git a/app/ServicePay/DeWuDianServices.php b/app/ServicePay/DeWuDianServices.php new file mode 100644 index 0000000..311949f --- /dev/null +++ b/app/ServicePay/DeWuDianServices.php @@ -0,0 +1,364 @@ +getGateUrl().'/pay/create_order'; + $return = request()->all(); + $re_type = 'html'; + if (isset($return['re_type'])) { + $re_type = $return['re_type']; + } + //商户号 + $accountId = $this->pay_config['mch_id']; + //商户秘钥 + $key = $this->pay_gateway['cert_pub'];//公钥 + $payData = [ + 'mchId' => $accountId,//商户 + 'appId' =>$this->pay_config['token'],// + 'body' => $pay_data['subject'], // + 'subject' => $pay_data['subject'], // + 'currency' => 'cny', // + 'mchOrderNo' => strval($pay_data['out_trade_no']),//订单号 + 'amount' => $pay_data['total_amount']*100, //金额,单位为fen + 'clientIp' => request()->getClientIp(), //金额,单位为fen + 'notifyUrl' => strval($this->pay_config['notify_url']),// //异步回调地址 + 'returnUrl' => strval($this->pay_config['return_url']),// //页面跳转通知 + 'productId' => $this->pay_gateway['mch_id'],//对应的三方通道 + 'extra' => 'app', // + ]; +// $payData['sign'] = $this->md5Verify('create',$payData,$key,['ip']); + $payData['sign'] = $this->sign($payData,$key); +// $res=$this->ddcurl($createUrl,$payData,true); + $res = Curl::to($createUrl)->withData($payData)->post(); + $this->debugLog('下单接口请求内容' . json_encode($payData, true), []); + $this->debugLog('返回内容' . json_encode($res, true), []); + + $res = is_array($res) ? $res : json_decode($res, 256); + if (is_array($res) && isset($res['retCode']) && $res['retCode'] == 'SUCCESS') { + $data = $res['payParams']; + $msg = $res['retMsg'] ?? '未知错误'; + $url=$data['payUrl']; + return $this->returnPayRes($re_type,$msg,1,$url); + } else { + $msg = $res['retMsg'] ?? '未知错误'; + $this->debugLog('错误' . $msg, []); + return $this->returnPayRes($re_type,$msg,0); + } + } + + + + private function sign($tempArr, $signKey,$filter=[]): string + { + $keyVal = ''; + + if (isset($tempArr['sign'])) { + unset($tempArr['sign']); + } + ksort($tempArr); + $data = []; + foreach ($tempArr as $key=>$v){ + if(!in_array($key,$filter)){ + array_push($data, "{$key}={$v}"); + } + } + $keyVal = implode('&', $data); + + if (!empty($signKey) && is_string($signKey)) { + $keyVal .= '&key='.$signKey; + } + dd($keyVal); + return strtoupper(md5($keyVal)); + } + + /** + * 检查订单 + * @param $orderid + * @return bool|string + */ + private function checkOrder($orderid) + { + try { + $url = $this->getGateUrl() . '/pay/query_order'; + + $payData = [ + 'mchId' => $this->pay_gateway['app_id'],//商户号 + 'appId' => $this->pay_gateway['app_key'],//appid + 'mchOrderNo' => $orderid, + ]; + + $key = $this->pay_gateway['cert_pub']; + + $payData['sign'] = $this->sign($payData,$key,['sign']); + Log::channel('pay_order')->info('查询接口请求', $payData); +// $res=$this->ddcurl($url,$payData,true); + $res = Curl::to($url)->withData($payData)->post(); + $response = is_array($res) ? $res : json_decode($res, 256); +// $this->debugLog('查询接口返回' . is_array($res) ? json_encode($res, true) : json_encode(json_decode($res, 256), true)); + Log::channel('pay_order')->info('查询接口返回', $response); + if (is_array($response) + && isset($response['retCode']) && $response['retCode'] == 'SUCCESS' + && isset($response['mchOrderNo']) && $response['mchOrderNo'] == $orderid + && isset($response['status']) && $response['status'] == 2 + ) { + return true; + } + return false; + }catch (\Exception $e){ + Log::channel('pay_order')->info('err', $e->getMessage()); + } + return false; + } + + + //统一验证签名 + public function verify() + { + $params = $this->result_data; + $key = $this->pay_gateway['cert_pub']; + $sindata=[]; + foreach ($params as $k=>$v){ + if($v!='' && $k!='sign'){ + $sindata[$k]=$v; + } + } + + $sign = $this->sign($params, $key, ['sign']); + if (isset($params['sign']) && $sign == $params['sign']) { + return true; + } else { + return false; + } + } + + + //返回给支付商的成功 + public function success() + { + echo 'success'; + exit; + } + + //统一回调处理 + + /** + * 回调第一部,如果是同步,直接回传 + * @param $return + * @param $pay_type + * @param $request + * @return bool|\Illuminate\Http\RedirectResponse + */ + public function notify($return, $pay_type, $request) + { +// $request = json_decode(file_get_contents('php://input'), true); + $allData = $request->all(); + dd($allData); + $this->setLogName($pay_type); + $this->debugLog('回调原始数据', $allData); + $this->pay_type = $pay_type; + $this->request = $request; + //取得订单,是否有此订单,如果没有直接返回空 + $order_sn = $allData['mchOrderNo']; + + $status = $allData['status']; + + if ($status != 2) { + return '未支付成功'; + } + dd($allData); + $checkresult = $this->checkIP();//检查回调IP + if (!$checkresult) { + return 'ip 不合法'; + } + //取得订单 + $this->getOrder($order_sn); + + $return = ($return) ? 1 : 0; + if ($return) { + + //判断是否是商户id + if ($this->order->merchant_id == 0) { + return redirect()->route('web.user.index'); + } + //00 + return $this->merchantReturnSend(); + } + //取得通道id + $this->gateway = $this->pay_gateway = config('gateway.config')[$this->gateway_id]; + //dd($this->gateway); + //取得支付配置 + $this->payConfig($this->pay_type); + + $this->result_data = $allData; + //检查三方的订单 +// $checkorder = $this->checkOrder($order_sn); +// if (!$checkorder) { +// $this->debugLog('查询三方订单未支付成功'); +// return '查询三方订单未支付成功'; +// } + //dump($this->result_data); + //进行验证签名 + if (!$this->verify()) { + $this->debugLog('签名以及支付状态验证失败'); + return '签名以及支付状态验证失败'; + } + + return $this->payNotify(); + } + + + /** + * 同步回调 + * @return \Illuminate\Http\RedirectResponse + */ + public function returnPay() + { + //判断是否是商户id + if ($this->order->merchant_id == 0) { + return redirect()->route('web.user.index'); + } + // + return $this->merchantReturnSend(); + } + + //异步回调 + + /** + * 获得通道信息,取得支付配置模型,验证签名, + * @return bool|void + */ + private function payNotify() + { + //取得通道信息内容 + $data = $this->result_data; + $this->pay_data = $this->result_data; + //如果应答状态不是01/A6表示支付失败 + $this->debugLog(json_encode($data, true)); + $this->debugLog('-------------' . $this->order->order_sn . '--选择----' . $this->pay_type . '----' . $this->pay_method . '----开始回调处理-------------------------'); + if (empty($data)) { + return $this->debugLog('验证签名失败', $data); + } + try { + $pay_cn_name = ''; + //统一格式化 + $this->unionHandle($data); + //支付返回信息 + $this->pay_data = $data; + $this->debugLog('验证签名成功,数据', $data); + //更新订单 + $this->updateOrder(); + $this->debugLog('updateOrder Ok'); + + } catch (\Exception $exception) { + $this->debugLog('失败异常内容:' . $exception->getMessage()); + $this->order->notify_status = 1;//支付回调成功 + $this->order->pay_status = 1;//支付状态 + $this->order->pay_ok_at = date('Y-m-d H:i:s'); + $this->order->save();//保存 + Log::channel('pay_success')->info($pay_cn_name . '支付成功,但处理订单失败', ['data' => json_encode($data)]); + + } + + } + + /** + * 统一格式化 + */ + public function unionHandle($data) + { + $result = [ + 'order_sn' => $data['mchOrderNo'],//我方订单号 + 'pay_order_sn' => $data['payOrderId'],//三方的订单号 + 'pay_money' => $data['amount']/100, //支付金额, + 'money' => $data['income']/100, + 'pay_type' => $this->pay_type, + 'account' => '' + ]; + + $this->setResult($result); + Log::channel('pay_success')->info('支付成功', [ + 'pay_type' => $result['pay_type'], 'pay_money' => $result['pay_money'], + 'account' => $result['money'], 'order_sn' => $result['order_sn'], + 'pay_order_sn' => $result['pay_order_sn']]); + + } + + + /** + * 这个调试需要配置在config/logging.php + * @param $pay_type + */ + public function setLogName($pay_type) + { + $this->log_name = $pay_type; + } + + /** + * 模拟post提交 json数据 请求接口 + * @param string $url + * @param bool|true $https + * @param string $method + * @param string $param + * @return mixed + */ + private function ddcurl($url = '', $param = '', $https = true, $method = 'post') + { + $param = json_encode($param, 256); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + #判断是否为https请求 + if ($https === true) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + } +// dd($param); + #判断是否为post请求 + if ($method == 'post') { + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $param); + } + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); + + $str = curl_exec($ch); + + curl_close($ch); + return $str; + } + +} + diff --git a/app/ServicePay/DongJinPayServices.php b/app/ServicePay/DongJinPayServices.php index 8fe3687..0d8a559 100644 --- a/app/ServicePay/DongJinPayServices.php +++ b/app/ServicePay/DongJinPayServices.php @@ -210,7 +210,7 @@ class DongJinPayServices extends BasePay implements PayApiInterface if ($this->order->merchant_id == 0) { return redirect()->route('web.user.index'); } - // + //00 return $this->merchantReturnSend(); } //取得通道id @@ -221,11 +221,11 @@ class DongJinPayServices extends BasePay implements PayApiInterface $this->result_data = $allData; //检查三方的订单 - $checkorder = $this->checkOrder($order_sn,$allData['total_fee']); - if (!$checkorder) { - $this->debugLog('查询三方订单未支付成功'); - return '查询三方订单未支付成功'; - } +// $checkorder = $this->checkOrder($order_sn,$allData['total_fee']); +// if (!$checkorder) { +// $this->debugLog('查询三方订单未支付成功'); +// return '查询三方订单未支付成功'; +// } //dump($this->result_data); //进行验证签名 if (!$this->verify()) { diff --git a/app/ServicePay/MiFengServices.php b/app/ServicePay/MiFengServices.php new file mode 100644 index 0000000..98ba3eb --- /dev/null +++ b/app/ServicePay/MiFengServices.php @@ -0,0 +1,358 @@ +getGateUrl().'/sfjoin/orderdata'; + $return = request()->all(); + $re_type = 'html'; + if (isset($return['re_type'])) { + $re_type = $return['re_type']; + } + //商户号 + $accountId = $this->pay_config['mch_id']; + //商户秘钥 + $key = $this->pay_config['token'];//公钥 + $payData = [ + 'appId' => $accountId,//商户 + 'orderNo' => strval($pay_data['out_trade_no']),//订单号 + 'channelNo' =>$this->pay_gateway['mch_id'],//对应的三方通道 + 'payType' => '1', // + 'amount' => number_format($pay_data['total_amount'], 2, '.', ''), //金额,单位为元,精确到小数点后两位 + 'notifyCallback' => strval($this->pay_config['notify_url']),// //异步回调地址 + ]; + $sinArr=[ + 'key'=>$key, + 'orderNo'=>$payData['orderNo'], + 'appId'=>$payData['appId'], + 'amount'=>$payData['amount'], + 'notifyCallback'=>$payData['notifyCallback'], + ]; + + $payData['sign'] = $this->sign($sinArr,$key); + $res=$this->ddcurl($createUrl,$payData,true); +// $res = Curl::to($createUrl)->withData($payData)->post(); + $this->debugLog('下单接口请求内容' . json_encode($payData, true), []); + $this->debugLog('返回内容' . json_encode($res, true), []); + + $res = is_array($res) ? $res : json_decode($res, 256); + if (is_array($res) && isset($res['code']) && $res['code'] == 1) { + $msg = $res['msg'] ?? '未知错误'; + $url = $res['payUrl']; + return $this->returnPayRes($re_type, $msg, 1, $url); + } else { + $msg = $res['msg'] ?? '未知错误'; + $this->debugLog('错误' . $msg, []); + return $this->returnPayRes($re_type,$msg,0); + } + } + + + + private function sign($tempArr, $signKey,$filter=[]): string + { + $keyVal = ''; + + if (isset($tempArr['sign'])) { + unset($tempArr['sign']); + } + $data = []; + foreach ($tempArr as $key=>$v){ + if(!in_array($key,$filter)){ + array_push($data, "{$v}"); + } + } + $keyVal = implode('', $data); +// dd($keyVal); + + return md5($keyVal); + } + + /** + * 检查订单 + * @param $orderid + * @return bool|string + */ + private function checkOrder($orderid) + { + try { + $url = $this->getGateUrl() . '/sfjoin/search'; + + $payData = [ + 'appId' => $this->pay_gateway['app_id'],//商户号 + 'orderNo' => $orderid, + ]; + + $key = $this->pay_gateway['app_key']; + + $payData['sign'] = $this->sign(['key'=>$key,'orderNo'=>$payData['orderNo'],'appId'=>$payData['appId']],$key,['sign']); + Log::channel('pay_order')->info('查询接口请求', $payData); + $res=$this->ddcurl($url,$payData,true); +// $res = Curl::to($url)->withData($payData)->post(); + $response = is_array($res) ? $res : json_decode($res, 256); +// $this->debugLog('查询接口返回' . is_array($res) ? json_encode($res, true) : json_encode(json_decode($res, 256), true)); + Log::channel('pay_order')->info('查询接口返回', $response); + if (is_array($response) + && isset($response['code']) && $response['code'] == 1 + && isset($response['orderNo']) && $response['orderNo'] == $orderid + && isset($response['state']) && $response['state'] == 1 + ) { + return true; + } + return false; + }catch (\Exception $e){ + Log::channel('pay_order')->info('err', $e->getMessage()); + } + return false; + } + + + //统一验证签名 + public function verify() + { + $params = $this->result_data; + $key =$this->pay_gateway['app_key'];//公钥 + $signParam=[ + 'key'=>$key, + 'orderNo'=>$params['orderNo'], + 'machId'=>$this->pay_gateway['app_id'], + 'amount'=>$params['amount'], + ]; + $sign = $this->sign($signParam, $key, ['sign']); + if (isset($params['sign']) && $sign == $params['sign']) { + return true; + } else { + return false; + } + } + + + //返回给支付商的成功 + public function success() + { + echo 'success'; + exit; + } + + //统一回调处理 + + /** + * 回调第一部,如果是同步,直接回传 + * @param $return + * @param $pay_type + * @param $request + * @return bool|\Illuminate\Http\RedirectResponse + */ + public function notify($return, $pay_type, $request) + { +// $request = json_decode(file_get_contents('php://input'), true); + $allData = $request->all(); + + $this->setLogName($pay_type); + $this->debugLog('回调原始数据', $allData); + $this->pay_type = $pay_type; + $this->request = $request; + //取得订单,是否有此订单,如果没有直接返回空 + $order_sn = $allData['orderNo']; + + $status = $allData['status']; + + if ($status != 1) { + return '未支付成功'; + } + + $checkresult = $this->checkIP();//检查回调IP + if (!$checkresult) { + return 'ip 不合法'; + } + //取得订单 + $this->getOrder($order_sn); + + $return = ($return) ? 1 : 0; + if ($return) { + + //判断是否是商户id + if ($this->order->merchant_id == 0) { + return redirect()->route('web.user.index'); + } + //00 + return $this->merchantReturnSend(); + } + //取得通道id + $this->gateway = $this->pay_gateway = config('gateway.config')[$this->gateway_id]; + //dd($this->gateway); + //取得支付配置 + $this->payConfig($this->pay_type); + + $this->result_data = $allData; + //检查三方的订单 + $checkorder = $this->checkOrder($order_sn); + if (!$checkorder) { + $this->debugLog('查询三方订单未支付成功'); + return '查询三方订单未支付成功'; + } + //dump($this->result_data); + //进行验证签名 + if (!$this->verify()) { + $this->debugLog('签名以及支付状态验证失败'); + return '签名以及支付状态验证失败'; + } + + return $this->payNotify(); + } + + + /** + * 同步回调 + * @return \Illuminate\Http\RedirectResponse + */ + public function returnPay() + { + //判断是否是商户id + if ($this->order->merchant_id == 0) { + return redirect()->route('web.user.index'); + } + // + return $this->merchantReturnSend(); + } + + //异步回调 + + /** + * 获得通道信息,取得支付配置模型,验证签名, + * @return bool|void + */ + private function payNotify() + { + //取得通道信息内容 + $data = $this->result_data; + $this->pay_data = $this->result_data; + //如果应答状态不是01/A6表示支付失败 + $this->debugLog(json_encode($data, true)); + $this->debugLog('-------------' . $this->order->order_sn . '--选择----' . $this->pay_type . '----' . $this->pay_method . '----开始回调处理-------------------------'); + if (empty($data)) { + return $this->debugLog('验证签名失败', $data); + } + try { + $pay_cn_name = ''; + //统一格式化 + $this->unionHandle($data); + //支付返回信息 + $this->pay_data = $data; + $this->debugLog('验证签名成功,数据', $data); + //更新订单 + $this->updateOrder(); + $this->debugLog('updateOrder Ok'); + + } catch (\Exception $exception) { + $this->debugLog('失败异常内容:' . $exception->getMessage()); + $this->order->notify_status = 1;//支付回调成功 + $this->order->pay_status = 1;//支付状态 + $this->order->pay_ok_at = date('Y-m-d H:i:s'); + $this->order->save();//保存 + Log::channel('pay_success')->info($pay_cn_name . '支付成功,但处理订单失败', ['data' => json_encode($data)]); + + } + + } + + /** + * 统一格式化 + */ + public function unionHandle($data) + { + $result = [ + 'order_sn' => $data['orderNo'],//我方订单号 + 'pay_order_sn' => $data['ownOrderNo'],//三方的订单号 + 'pay_money' => $data['amount'], //支付金额, + 'money' => $data['amount'], + 'pay_type' => $this->pay_type, + 'account' => '' + ]; + + $this->setResult($result); + Log::channel('pay_success')->info('支付成功', [ + 'pay_type' => $result['pay_type'], 'pay_money' => $result['pay_money'], + 'account' => $result['money'], 'order_sn' => $result['order_sn'], + 'pay_order_sn' => $result['pay_order_sn']]); + + } + + + /** + * 这个调试需要配置在config/logging.php + * @param $pay_type + */ + public function setLogName($pay_type) + { + $this->log_name = $pay_type; + } + + /** + * 模拟post提交 json数据 请求接口 + * @param string $url + * @param bool|true $https + * @param string $method + * @param string $param + * @return mixed + */ + private function ddcurl($url = '', $param = '', $https = true, $method = 'post') + { + $param = json_encode($param, 256); + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + #判断是否为https请求 + if ($https === true) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + } +// dd($param); + #判断是否为post请求 + if ($method == 'post') { + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $param); + } + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); + + $str = curl_exec($ch); + + curl_close($ch); + return $str; + } + +} + diff --git a/app/ServicePay/PayApiProvidesTrait.php b/app/ServicePay/PayApiProvidesTrait.php index 57881c2..142090f 100644 --- a/app/ServicePay/PayApiProvidesTrait.php +++ b/app/ServicePay/PayApiProvidesTrait.php @@ -75,6 +75,10 @@ trait PayApiProvidesTrait return $this->pay_service = new ShouJiPayServices(); case '1006': return $this->pay_service = new DongJinPayServices(); + case '1007': + return $this->pay_service = new DeWuDianServices(); + case '1008': + return $this->pay_service = new MiFengServices(); break; } } diff --git a/config/adconfig.php b/config/adconfig.php index 395bc6b..5d06b7c 100644 --- a/config/adconfig.php +++ b/config/adconfig.php @@ -65,6 +65,8 @@ return [ '1001' =>'山野付', '1005' =>'手机网站支付', '1006' =>'东金天然气', + '1007' =>'得物点卡H5', + '1008' =>'蜜蜂-得物点卡', ], 'payfor_status' => [ 0 => '等待处理', diff --git a/config/whitelist.php b/config/whitelist.php index 183facd..30c7f50 100644 --- a/config/whitelist.php +++ b/config/whitelist.php @@ -74,7 +74,16 @@ return [ '154.89.11.174' ], '1006'=>[ - '47.243.100.241' + '47.243.65.80' ], +// '1007'=>[ +// '154.89.8.26' +// ], + + '1008'=>[ + '8.210.207.120', + '47.243.103.193', + '47.243.113.117', + ] ] ]; \ No newline at end of file