Huafei.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\controller\api\store\service;
  3. use think\App;
  4. use think\facade\Db;
  5. use crmeb\basic\BaseController;
  6. use app\common\repositories\user\UserThirdRepository;
  7. class Huafei extends BaseController
  8. {
  9. public $appkey = '1077740514';
  10. public $secretKey = 'f154a106d56d804fcc640d9da4844b0c';
  11. // 生产
  12. public $gatewayUrl = "https://router.wikeyun.cn";
  13. public $format = "json";
  14. /** 是否打开入参check**/
  15. protected $signMethod = "md5";
  16. protected $apiVersion = "1.0";
  17. //充值话费回调
  18. public function notify(){
  19. try{
  20. $param = $this->request->param();
  21. Db::name('log')->insert(['data'=>'话费充值回调数据'.json_encode($param)]);
  22. $order_no = $param['order_no'];
  23. $order_number = $param['order_number'];
  24. $huafeiInfo = Db::name('order_huafei')->where("order_sn", $order_no)->find();
  25. if(!$huafeiInfo){
  26. return 'error';
  27. }
  28. $updateData = [
  29. 'amount_price' => $param['amount'],
  30. 'fanli_price' => $param['fanli'],
  31. 'cost_price' => $param['cost_price'],
  32. 'arrived_amount'=> $param['arrived_amount'],
  33. 'status' => $param['status'],
  34. 'modify_time' => date('Y-m-d H:i:s'),
  35. ];
  36. $ret = Db::name("order_huafei")->where("order_sn", $order_no)->update($updateData);
  37. if($ret){
  38. if($param['status'] == 2) {
  39. //判断数据库有无此订单
  40. $type_shop = 9;
  41. $orderThird = app()->make(UserThirdRepository::class);
  42. //预估养老金
  43. $orderThird->yuguOrder(
  44. $huafeiInfo['order_pdd_id'],
  45. $huafeiInfo['commission'],
  46. $huafeiInfo['uid'],
  47. $huafeiInfo['order_sn'],
  48. $type_shop,
  49. 'order_huafei');
  50. //预估积分
  51. $orderThird->yuguJifen(
  52. $huafeiInfo['commission'] ,
  53. $huafeiInfo['uid'],
  54. $huafeiInfo['order_sn'],
  55. $type_shop
  56. );
  57. //复购金
  58. $orderThird->yuguFugou(
  59. $huafeiInfo['commission'] ,
  60. $huafeiInfo['uid'],
  61. $huafeiInfo['order_sn'],
  62. $type_shop
  63. );
  64. }
  65. echo 'success';
  66. return;
  67. }else{
  68. Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']);
  69. echo 'error';
  70. return;
  71. }
  72. }catch (\Exception $e) {
  73. Db::name('log')->insert(['data'=>'充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  74. return app('json')->fail('充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  75. }
  76. }
  77. //充值话费面额
  78. public function denomination(){
  79. $data = [
  80. ['money'=>50, 'name'=>'50元'],
  81. ['money'=>100, 'name'=>'100元'],
  82. ['money'=>200, 'name'=>'200元'],
  83. ];
  84. foreach($data as $k=>$v){
  85. $commissionMoney = $v['money']*0.04;
  86. $systemvalueObj = $goods_obj = Db::name('system_config_value')->where('config_key','sanfangfenpei')->find();
  87. $sanfangfenpei=json_decode($systemvalueObj['value'],true);
  88. $commissionMoney80=$commissionMoney*0.8;
  89. $pv=$commissionMoney80*$sanfangfenpei['pv']; //PV
  90. $yanglaojin=$commissionMoney80*$sanfangfenpei['yanglaojin']; //新的养老金
  91. $gongxian=$pv; //新的养老金
  92. $jifen=$pv; //新的养老金
  93. $data[$k]['pv']=round($pv,2);
  94. $data[$k]['yanglaojin']=round($yanglaojin,2);
  95. $data[$k]['gongxian']=round($gongxian,2);
  96. $data[$k]['jifen']=round($jifen,2);
  97. }
  98. return app('json')->success('获取话费面额成功', $data);
  99. }
  100. //充值话费推送补发
  101. public function bufa(){
  102. try{
  103. $order_no = $this->request->param('order_no');
  104. $order_data = Db::name('order_huafei_hf') -> where('order_no', $order_no)->find();
  105. $RES = $this->pushHf($order_data['uid'], $order_data['order_no'], $order_data['mobile'], $order_data['pay_price']);
  106. return app('json')->success('充值话费推送补发成功', $RES);
  107. }catch (\Exception $e) {
  108. Db::name('log')->insert(['data'=>'充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  109. return app('json')->fail('充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  110. }
  111. }
  112. //充值话费推送
  113. public function recharge(){
  114. $uid = $this->request->uid();
  115. $params = $this->request->params(['phone', 'money']);
  116. if(!$params['phone']) return app('json')->fail('请提交电话号码');
  117. if(!$params['money']) return app('json')->fail('请提交充值金额');
  118. //验证
  119. $verify = $this->verify($params['phone'], $params['money']);
  120. if($verify['code'] == '0000'){
  121. $order_sn = time().$uid.mt_rand(1000,9999);
  122. //创建预支付订单
  123. $insertdata = [
  124. 'uid'=> $uid,
  125. 'mobile' => $params['phone'],
  126. 'order_no' => $order_sn,
  127. 'pay_price' => $params['money'],
  128. 'status' => 0,
  129. 'create_time'=> date('Y-m-d H:i:s'),
  130. ];
  131. $oid = Db::name('order_huafei_hf')->insertGetId($insertdata);
  132. if($oid){
  133. return app('json')->success('订单创建成功', ['oid'=>$oid, 'order_sn'=>$order_sn]);
  134. }else{
  135. return app('json')->fail('创建话费充值预支付订单失败');
  136. }
  137. }else{
  138. return app('json')->fail($verify['msg'].'MSG');
  139. }
  140. }
  141. //话费充值推送
  142. public function pushHf($uid, $order_sn, $phone, $money){
  143. //推送订单
  144. $push = $this->pushOrder($order_sn, $phone, $money);
  145. Db::name('log')->insert(['data'=>'话费充值推送订单返回数据'.json_encode($push)]);
  146. if($push['code'] == '0000'){
  147. $commissionMoney = $money*0.04;
  148. //成功入库记录
  149. $insertdata = [
  150. 'uid'=> $uid,
  151. 'mobile' => $phone,
  152. 'commission'=> $commissionMoney,
  153. 'commission_share'=> 0.04,
  154. 'order_number' => $push['data']['order_number'],
  155. 'order_sn' => $order_sn,
  156. 'amount_price' => $push['data']['amount'],
  157. 'fanli_price' => $push['data']['fanli'],
  158. 'cost_price' => $push['data']['cost_price'],
  159. 'pay_money' => $money,
  160. 'status' => 1,
  161. 'create_time' => date('Y-m-d H:i:s'),
  162. 'finish_time' => date('Y-m-d H:i:s'),
  163. 'modify_time' => date('Y-m-d H:i:s'),
  164. ];
  165. Db::name('order_huafei')->insert($insertdata);
  166. return $push;
  167. }else{
  168. $commissionMoney = $money*0.04;
  169. //失败入库记录
  170. $insertdata = [
  171. 'uid'=> $uid,
  172. 'mobile' => $phone,
  173. 'commission'=> $commissionMoney,
  174. 'commission_share'=> 0.04,
  175. 'order_number' => '',
  176. 'order_sn' => $order_sn,
  177. 'amount_price' => $money,
  178. 'fanli_price' => 0,
  179. 'cost_price' => 0,
  180. 'pay_money' => $money,
  181. 'status' => 3,
  182. 'create_time' => date('Y-m-d H:i:s'),
  183. 'finish_time' => date('Y-m-d H:i:s'),
  184. 'modify_time' => date('Y-m-d H:i:s'),
  185. ];
  186. Db::name('order_huafei')->insert($insertdata);
  187. return $push;
  188. }
  189. }
  190. //话费充值记录
  191. public function record(){
  192. $uid = $this->request->uid();
  193. [$page, $limit] = $this->getPage();
  194. $where = [];
  195. $where[] = ['uid', '=', $uid];
  196. $query = Db::name("order_huafei");
  197. $count = $query->where($where)->count();
  198. $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){
  199. return $item;
  200. });
  201. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count]);
  202. }
  203. //话费充值验证
  204. public function verify($order_number, $money){
  205. $request['mobile'] = $order_number;
  206. $request['amount'] = $money;
  207. $request['recharge_type'] = 1;
  208. return $this->execute('/rest/Recharge/verify', $request);
  209. }
  210. //话费订单充值推送
  211. public function pushOrder($order_sn, $phone, $money){
  212. $request['store_id'] = '9189';
  213. $request['order_no'] = $order_sn;
  214. $request['mobile'] = $phone;
  215. $request['money'] = $money;
  216. $request['recharge_type'] = 1;
  217. $request['notify_url'] = 'https://api.gdjbp.com/api/huafei/notify';
  218. return $this->execute('/rest/Recharge/PushOrder', $request);
  219. }
  220. //话费订单查询
  221. public function query($order_number){
  222. $request['store_id'] = '9189';
  223. $request['order_number'] = $order_number;
  224. return $this->execute('/rest/Recharge/query', $request);
  225. }
  226. //话费订单取消
  227. public function cancel($order_number){
  228. $request['order_number'] = $order_number;
  229. return $this->execute('/rest/Recharge/cancel', $request);
  230. }
  231. protected function generateSign($params){
  232. ksort($params);
  233. $stringToBeSigned = $this->secretKey;
  234. foreach ($params as $k => $v)
  235. {
  236. if (is_array($v)) {
  237. $stringToBeSigned .= $k;
  238. }else{
  239. if ("@" != substr($v, 0, 1)) {
  240. $stringToBeSigned .= "$k$v";
  241. }
  242. }
  243. }
  244. unset($k, $v);
  245. $stringToBeSigned .= $this->secretKey;
  246. return strtoupper(md5($stringToBeSigned));
  247. }
  248. public function curl($url, $postFields = null,$timeOut = 20){
  249. $ch = curl_init();
  250. curl_setopt($ch, CURLOPT_URL, $url);
  251. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  252. curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
  253. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  254. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  255. //https 请求
  256. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  257. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  258. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  259. }
  260. if (is_array($postFields) && 0 < count($postFields)){
  261. $postBodyString = "";
  262. $postMultipart = false;
  263. foreach ($postFields as $k => $v){
  264. if("@" != substr($v, 0, 1))//判断是不是文件上传
  265. {
  266. $postBodyString .= "$k=" . urlencode($v) . "&";
  267. }
  268. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  269. {
  270. $postMultipart = true;
  271. }
  272. $postMultipart = true;
  273. }
  274. unset($k, $v);
  275. curl_setopt($ch, CURLOPT_POST, true);
  276. if ($postMultipart){
  277. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  278. }else{
  279. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  280. }
  281. }
  282. $reponse = curl_exec($ch);
  283. curl_close($ch);
  284. return json_decode($reponse, true);
  285. }
  286. /**
  287. * request api接口后缀,如 /recharge/pushOrder
  288. * apiParams 业务参数 数组
  289. */
  290. public function execute($request, $apiParams ,$timeOut =20){
  291. //组装系统参数
  292. $sysParams["app_key"] = $this->appkey;
  293. $sysParams["v"] = $this->apiVersion;
  294. $sysParams["format"] = $this->format;
  295. $sysParams["timestamp"] = time();
  296. $sysParams["client"] = '';
  297. //签名
  298. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  299. //系统参数放入GET请求串
  300. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  301. $requestUrl = $this->gatewayUrl.$request. "?";
  302. foreach ($sysParams as $sysParamKey => $sysParamValue){
  303. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  304. }
  305. $requestUrl = substr($requestUrl, 0, -1);
  306. return $this->curl($requestUrl, $apiParams ,$timeOut);
  307. }
  308. }