Huafei.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_huafei_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. if($param['status'] == 3) {
  66. //把赠送出去的养老金 积分 贡献值 复购金 改为已失效
  67. Db::name('user_bill')->where('order_sn', $huafeiInfo['order_no'])->update(['status'=> -1]);
  68. Db::name('user_sign_score')->where('order_id', $huafeiInfo['order_no'])->update(['status'=> 0]);
  69. Db::name('user_sign_gongxian')->where('order_id', $huafeiInfo['order_no'])->update(['status'=> 0]);
  70. Db::name('user_sign_fugou')->where('order_id', $huafeiInfo['order_no'])->update(['status'=> 0]);
  71. }
  72. echo 'success';
  73. return;
  74. }else{
  75. Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']);
  76. echo 'error';
  77. return;
  78. }
  79. }catch (\Exception $e) {
  80. Db::name('log')->insert(['data'=>'充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  81. return app('json')->fail('充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  82. }
  83. }
  84. //充值话费面额
  85. public function denomination(){
  86. $data = [
  87. ['money'=>50, 'name'=>'50元'],
  88. ['money'=>100, 'name'=>'100元'],
  89. ['money'=>200, 'name'=>'200元'],
  90. ];
  91. foreach($data as $k=>$v){
  92. $commissionMoney = $v['money']*0.04;
  93. $systemvalueObj = $goods_obj = Db::name('system_config_value')->where('config_key','sanfangfenpei')->find();
  94. $sanfangfenpei=json_decode($systemvalueObj['value'],true);
  95. $pv=$commissionMoney*$sanfangfenpei['pv']; //PV
  96. $yanglaojin=$commissionMoney*$sanfangfenpei['yanglaojin']; //新的养老金
  97. $gongxian=$pv; //新的养老金
  98. $jifen=$pv; //新的养老金
  99. $data[$k]['pv']=round($pv,2);
  100. $data[$k]['yanglaojin']=round($yanglaojin,2);
  101. $data[$k]['gongxian']=round($gongxian,2);
  102. $data[$k]['jifen']=round($jifen,2);
  103. }
  104. return app('json')->success('获取话费面额成功', $data);
  105. }
  106. //充值话费推送补发
  107. public function bufa(){
  108. try{
  109. $order_no = $this->request->param('order_no');
  110. $order_data = Db::name('order_huafei_hf') -> where('order_no', $order_no)->find();
  111. $RES = $this->pushHf($order_data['uid'], $order_data['order_no'], $order_data['mobile'], $order_data['pay_price'], $order_data['order_id']);
  112. return app('json')->success('充值话费推送补发成功', $RES);
  113. }catch (\Exception $e) {
  114. Db::name('log')->insert(['data'=>'充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  115. return app('json')->fail('充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  116. }
  117. }
  118. //充值话费推送
  119. public function recharge(){
  120. $uid = $this->request->uid();
  121. $params = $this->request->params(['phone', 'money']);
  122. if(!$params['phone']) return app('json')->fail('请提交电话号码');
  123. if(!$params['money']) return app('json')->fail('请提交充值金额');
  124. //验证
  125. $verify = $this->verify($params['phone'], $params['money']);
  126. if($verify['code'] == '0000'){
  127. $order_sn = time().$uid.mt_rand(1000,9999);
  128. //创建预支付订单
  129. $insertdata = [
  130. 'uid'=> $uid,
  131. 'mobile' => $params['phone'],
  132. 'order_no' => $order_sn,
  133. 'pay_price' => $params['money'],
  134. 'status' => 0,
  135. 'create_time'=> date('Y-m-d H:i:s'),
  136. ];
  137. $oid = Db::name('order_huafei_hf')->insertGetId($insertdata);
  138. if($oid){
  139. return app('json')->success('订单创建成功', ['oid'=>$oid, 'order_sn'=>$order_sn]);
  140. }else{
  141. return app('json')->fail('创建话费充值预支付订单失败');
  142. }
  143. }else{
  144. return app('json')->fail($verify['msg'].'MSG');
  145. }
  146. }
  147. //话费充值推送
  148. public function pushHf($uid, $order_sn, $phone, $money, $order_id = 0){
  149. $commissionMoney = $money*0.04;
  150. //判断数据库有无此订单
  151. $type_shop = 9;
  152. $orderThird = app()->make(UserThirdRepository::class);
  153. //预估养老金
  154. $orderThird->yuguOrder(
  155. $order_id,
  156. $commissionMoney,
  157. $uid,
  158. $order_sn,
  159. $type_shop,
  160. 'order_huafei');
  161. //预估积分&贡献值
  162. $orderThird->yuguJifen(
  163. $commissionMoney,
  164. $uid,
  165. $order_sn,
  166. $type_shop
  167. );
  168. //复购金
  169. $orderThird->yuguFugou(
  170. $commissionMoney,
  171. $uid,
  172. $order_sn,
  173. $type_shop
  174. );
  175. //推送订单
  176. $push = $this->pushOrder($order_sn, $phone, $money);
  177. Db::name('log')->insert(['data'=>'话费充值推送订单返回数据'.json_encode($push)]);
  178. if($push['code'] == '0000'){
  179. $commissionMoney = $money*0.04;
  180. //成功入库记录
  181. $insertdata = [
  182. 'uid'=> $uid,
  183. 'mobile' => $phone,
  184. 'commission'=> $commissionMoney,
  185. 'commission_share'=> 0.04,
  186. 'order_number' => $push['data']['order_number'],
  187. 'order_sn' => $order_sn,
  188. 'amount_price' => $push['data']['amount'],
  189. 'fanli_price' => $push['data']['fanli'],
  190. 'cost_price' => $push['data']['cost_price'],
  191. 'pay_money' => $money,
  192. 'status' => 1,
  193. 'create_time' => date('Y-m-d H:i:s'),
  194. 'finish_time' => date('Y-m-d H:i:s'),
  195. 'modify_time' => date('Y-m-d H:i:s'),
  196. ];
  197. Db::name('order_huafei')->insert($insertdata);
  198. return $push;
  199. }else{
  200. $commissionMoney = $money*0.04;
  201. //失败入库记录
  202. $insertdata = [
  203. 'uid'=> $uid,
  204. 'mobile' => $phone,
  205. 'commission'=> $commissionMoney,
  206. 'commission_share'=> 0.04,
  207. 'order_number' => '',
  208. 'order_sn' => $order_sn,
  209. 'amount_price' => $money,
  210. 'fanli_price' => 0,
  211. 'cost_price' => 0,
  212. 'pay_money' => $money,
  213. 'status' => 3,
  214. 'create_time' => date('Y-m-d H:i:s'),
  215. 'finish_time' => date('Y-m-d H:i:s'),
  216. 'modify_time' => date('Y-m-d H:i:s'),
  217. ];
  218. Db::name('order_huafei')->insert($insertdata);
  219. return $push;
  220. }
  221. }
  222. //话费充值记录
  223. public function record(){
  224. $uid = $this->request->uid();
  225. [$page, $limit] = $this->getPage();
  226. $where = [];
  227. $where[] = ['uid', '=', $uid];
  228. $query = Db::name("order_huafei");
  229. $count = $query->where($where)->count();
  230. $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){
  231. return $item;
  232. });
  233. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count, 'uid'=>$uid]);
  234. }
  235. //话费充值验证
  236. public function verify($order_number, $money){
  237. $request['mobile'] = $order_number;
  238. $request['amount'] = $money;
  239. $request['recharge_type'] = 0;
  240. return $this->execute('/rest/Recharge/verify', $request);
  241. }
  242. //话费订单充值推送
  243. public function pushOrder($order_sn, $phone, $money){
  244. $request['store_id'] = '9189';
  245. $request['order_no'] = $order_sn;
  246. $request['mobile'] = $phone;
  247. $request['money'] = $money;
  248. $request['recharge_type'] = 0;
  249. $request['notify_url'] = 'https://api.hxxfb.com/api/huafei/notify';
  250. return $this->execute('/rest/Recharge/PushOrder', $request);
  251. }
  252. //话费订单查询
  253. public function query($order_number){
  254. $request['store_id'] = '9189';
  255. $request['order_number'] = $order_number;
  256. return $this->execute('/rest/Recharge/query', $request);
  257. }
  258. //话费订单取消
  259. public function cancel($order_number){
  260. $request['order_number'] = $order_number;
  261. return $this->execute('/rest/Recharge/cancel', $request);
  262. }
  263. protected function generateSign($params){
  264. ksort($params);
  265. $stringToBeSigned = $this->secretKey;
  266. foreach ($params as $k => $v)
  267. {
  268. if (is_array($v)) {
  269. $stringToBeSigned .= $k;
  270. }else{
  271. if ("@" != substr($v, 0, 1)) {
  272. $stringToBeSigned .= "$k$v";
  273. }
  274. }
  275. }
  276. unset($k, $v);
  277. $stringToBeSigned .= $this->secretKey;
  278. return strtoupper(md5($stringToBeSigned));
  279. }
  280. public function curl($url, $postFields = null,$timeOut = 20){
  281. $ch = curl_init();
  282. curl_setopt($ch, CURLOPT_URL, $url);
  283. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  284. curl_setopt($ch, CURLOPT_REFERER, 'https://api.hxxfb.com');
  285. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  286. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  287. //https 请求
  288. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  289. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  290. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  291. }
  292. if (is_array($postFields) && 0 < count($postFields)){
  293. $postBodyString = "";
  294. $postMultipart = false;
  295. foreach ($postFields as $k => $v){
  296. if("@" != substr($v, 0, 1))//判断是不是文件上传
  297. {
  298. $postBodyString .= "$k=" . urlencode($v) . "&";
  299. }
  300. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  301. {
  302. $postMultipart = true;
  303. }
  304. $postMultipart = true;
  305. }
  306. unset($k, $v);
  307. curl_setopt($ch, CURLOPT_POST, true);
  308. if ($postMultipart){
  309. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  310. }else{
  311. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  312. }
  313. }
  314. $reponse = curl_exec($ch);
  315. curl_close($ch);
  316. return json_decode($reponse, true);
  317. }
  318. /**
  319. * request api接口后缀,如 /recharge/pushOrder
  320. * apiParams 业务参数 数组
  321. */
  322. public function execute($request, $apiParams ,$timeOut =20){
  323. //组装系统参数
  324. $sysParams["app_key"] = $this->appkey;
  325. $sysParams["v"] = $this->apiVersion;
  326. $sysParams["format"] = $this->format;
  327. $sysParams["timestamp"] = time();
  328. $sysParams["client"] = '';
  329. //签名
  330. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  331. //系统参数放入GET请求串
  332. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  333. $requestUrl = $this->gatewayUrl.$request. "?";
  334. foreach ($sysParams as $sysParamKey => $sysParamValue){
  335. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  336. }
  337. $requestUrl = substr($requestUrl, 0, -1);
  338. return $this->curl($requestUrl, $apiParams ,$timeOut);
  339. }
  340. }