Huafei.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. use app\common\repositories\store\order\StoreRefundOrderRepository;
  8. class Huafei extends BaseController
  9. {
  10. public $appkey = '1077740514';
  11. public $secretKey = 'f154a106d56d804fcc640d9da4844b0c';
  12. // 生产
  13. public $gatewayUrl = "https://router.wikeyun.cn";
  14. public $format = "json";
  15. /** 是否打开入参check**/
  16. protected $signMethod = "md5";
  17. protected $apiVersion = "1.0";
  18. //充值话费回调
  19. public function notify(){
  20. try{
  21. $param = $this->request->param();
  22. Db::name('log')->insert(['data'=>'话费充值回调数据'.json_encode($param)]);
  23. $order_no = $param['order_no'];
  24. $order_number = $param['order_number'];
  25. $huafeiInfo = Db::name('order_huafei')->where("order_sn", $order_no)->find();
  26. if(!$huafeiInfo){
  27. return 'error';
  28. }
  29. $updateData = [
  30. 'amount_price' => $param['amount'],
  31. 'fanli_price' => $param['fanli'],
  32. 'cost_price' => $param['cost_price'],
  33. 'arrived_amount'=> $param['arrived_amount'],
  34. 'status' => $param['status'],
  35. 'modify_time' => date('Y-m-d H:i:s'),
  36. ];
  37. $ret = Db::name("order_huafei")->where("order_sn", $order_no)->update($updateData);
  38. if($ret){
  39. if($param['status'] == 2) {
  40. // //判断数据库有无此订单
  41. // $type_shop = 9;
  42. // $orderThird = app()->make(UserThirdRepository::class);
  43. // //预估养老金
  44. // $orderThird->yuguOrder(
  45. // $huafeiInfo['order_huafei_id'],
  46. // $huafeiInfo['commission'],
  47. // $huafeiInfo['uid'],
  48. // $huafeiInfo['order_sn'],
  49. // $type_shop,
  50. // 'order_huafei');
  51. // //预估积分
  52. // $orderThird->yuguJifen(
  53. // $huafeiInfo['commission'] ,
  54. // $huafeiInfo['uid'],
  55. // $huafeiInfo['order_sn'],
  56. // $type_shop
  57. // );
  58. // //复购金
  59. // $orderThird->yuguFugou(
  60. // $huafeiInfo['commission'] ,
  61. // $huafeiInfo['uid'],
  62. // $huafeiInfo['order_sn'],
  63. // $type_shop
  64. // );
  65. }
  66. if($param['status'] == 3) {
  67. $order_data = Db::name('order_huafei_hf')->where('order_no', $order_no)->find();
  68. $pay_wx = systemConfig('pay_wx');
  69. //推送失败 给新支付的这一笔退款
  70. Db::name('log')->insert(['data' => '给新话费充值支付的这一笔退款']);
  71. if ($pay_wx == 3){
  72. $key='api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  73. }else{
  74. $key='api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  75. }
  76. $refund_traits = [
  77. 'payment_id' => $order_data['payment_id'],
  78. 'app_id' => $order_data['app_id'],
  79. 'order_no' => app()->make(StoreRefundOrderRepository::class)->getNewOrderId(),
  80. 'reverse_amt' => $order_data['reverse_amt'],
  81. 'notify_url' => env('APP_URL') . "/api/hf_notify/reverse_order",
  82. ];
  83. $hf_refund_res = app()->make(StoreRefundOrderRepository::class)->payment_Reverse($refund_traits, $order_data['pay_type'] == '1' ? 2 : 1, $pay_wx, $key);
  84. Db::name('log')->insert(['data'=>'话费充值汇付支付撤销返回结果:'.json_encode($hf_refund_res)]);
  85. //把赠送出去的养老金 积分 贡献值 复购金 改为已失效
  86. Db::name('user_bill')->where("type_shop", 9)->where('link_id', $order_data['order_id'])->update(['status'=> -1]);
  87. Db::name('user_sign_score')->where("order_type", 9)->where('order_id', $order_data['order_id'])->update(['status'=> 0]);
  88. Db::name('user_sign_gongxian')->where("order_type", 9)->where('order_id', $order_data['order_id'])->update(['status'=> 0]);
  89. Db::name('user_sign_fugou')->where("order_type", 9)->where('order_id', $order_data['order_id'])->update(['status'=> 0]);
  90. Db::name('user_sign_hongbao')->where("order_type", 9)->where('order_id', $order_data['order_id'])->update(['status'=> 0]);
  91. }
  92. echo 'success';
  93. return;
  94. }else{
  95. Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']);
  96. echo 'error';
  97. return;
  98. }
  99. }catch (\Exception $e) {
  100. Db::name('log')->insert(['data'=>'充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  101. return app('json')->fail('充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  102. }
  103. }
  104. //充值话费面额
  105. public function denomination(){
  106. $data = [
  107. ['money'=>50, 'name'=>'50元'],
  108. ['money'=>100, 'name'=>'100元'],
  109. ['money'=>200, 'name'=>'200元'],
  110. ];
  111. foreach($data as $k=>$v){
  112. $commissionMoney = $v['money']*0.02;
  113. $systemvalueObj = $goods_obj = Db::name('system_config_value')->where('config_key','sanfangfenpei')->find();
  114. $sanfangfenpei=json_decode($systemvalueObj['value'],true);
  115. $pv=$commissionMoney*$sanfangfenpei['pv']; //PV
  116. $yanglaojin=$commissionMoney*$sanfangfenpei['yanglaojin']; //新的养老金
  117. $gongxian=$pv; //新的养老金
  118. $jifen=$pv; //新的养老金
  119. $data[$k]['pv']=round($pv,2);
  120. $data[$k]['yanglaojin']=round($yanglaojin,2);
  121. $data[$k]['gongxian']=round($gongxian,2);
  122. $data[$k]['jifen']=round($jifen,2);
  123. }
  124. return app('json')->success('获取话费面额成功', $data);
  125. }
  126. //充值话费推送补发
  127. public function bufa(){
  128. try{
  129. $order_no = $this->request->param('order_no');
  130. $order_data = Db::name('order_huafei_hf') -> where('order_no', $order_no)->find();
  131. $RES = $this->pushHf($order_data['uid'], $order_data['order_no'], $order_data['mobile'], $order_data['pay_price'], $order_data['order_id']);
  132. return app('json')->success('充值话费推送补发成功', $RES);
  133. }catch (\Exception $e) {
  134. Db::name('log')->insert(['data'=>'充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']);
  135. return app('json')->fail('充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  136. }
  137. }
  138. //充值话费推送
  139. public function recharge(){
  140. $uid = $this->request->uid();
  141. $params = $this->request->params(['phone', 'money']);
  142. if(!$params['phone']) return app('json')->fail('请提交电话号码');
  143. if(!$params['money']) return app('json')->fail('请提交充值金额');
  144. //验证
  145. $verify = $this->verify($params['phone'], $params['money']);
  146. if($verify['code'] == '0000'){
  147. $order_sn = time().$uid.mt_rand(1000,9999);
  148. //创建预支付订单
  149. $insertdata = [
  150. 'uid'=> $uid,
  151. 'mobile' => $params['phone'],
  152. 'order_no' => $order_sn,
  153. 'pay_price' => $params['money'],
  154. 'status' => 0,
  155. 'create_time'=> date('Y-m-d H:i:s'),
  156. ];
  157. $oid = Db::name('order_huafei_hf')->insertGetId($insertdata);
  158. if($oid){
  159. return app('json')->success('订单创建成功', ['oid'=>$oid, 'order_sn'=>$order_sn]);
  160. }else{
  161. return app('json')->fail('创建话费充值预支付订单失败');
  162. }
  163. } elseif ($verify['code'] == '1004') {
  164. return app('json')->fail('各位用户您好:因中国移动充值端口升级,目前暂时无法使用,恢复充值需要等待官方通知,感谢理解');
  165. } else {
  166. return app('json')->fail($verify['msg'].'MSG');
  167. }
  168. }
  169. //话费充值推送
  170. public function pushHf($uid, $order_sn, $phone, $money, $order_id = 0){
  171. //判断数据库有无此订单
  172. $type_shop = 9;
  173. $orderThird = app()->make(UserThirdRepository::class);
  174. //预估养老金
  175. // //复购金
  176. // $orderThird->yuguFugou(
  177. // $commissionMoney,
  178. // $uid,
  179. // $order_sn,
  180. // $type_shop
  181. // );
  182. //推送订单
  183. $push = $this->pushOrder($order_sn, $phone, $money);
  184. Db::name('log')->insert(['data'=>'话费充值推送订单返回数据'.json_encode($push)]);
  185. if($push['code'] == '0000'){
  186. $commissionMoney = $money*0.02;
  187. //成功入库记录
  188. $insertdata = [
  189. 'uid'=> $uid,
  190. 'mobile' => $phone,
  191. 'commission'=> $commissionMoney,
  192. 'commission_share'=> 0.02,
  193. 'order_number' => $push['data']['order_number'],
  194. 'order_sn' => $order_sn,
  195. 'amount_price' => $push['data']['amount'],
  196. 'fanli_price' => $push['data']['fanli'],
  197. 'cost_price' => $push['data']['cost_price'],
  198. 'pay_money' => $money,
  199. 'status' => 1,
  200. 'create_time' => date('Y-m-d H:i:s'),
  201. 'finish_time' => date('Y-m-d H:i:s'),
  202. 'modify_time' => date('Y-m-d H:i:s'),
  203. ];
  204. $order_id = Db::name('order_huafei')->insertGetId($insertdata);
  205. $orderThird->yuguOrder(
  206. $order_id,
  207. $commissionMoney,
  208. $uid,
  209. $order_sn,
  210. $type_shop,
  211. 'order_huafei');
  212. //预估积分&贡献值
  213. $orderThird->yuguJifen(
  214. $order_id,
  215. $commissionMoney,
  216. $uid,
  217. $order_sn,
  218. $type_shop
  219. );
  220. return $push;
  221. }else{
  222. $commissionMoney = $money*0.02;
  223. //失败入库记录
  224. $insertdata = [
  225. 'uid'=> $uid,
  226. 'mobile' => $phone,
  227. 'commission'=> $commissionMoney,
  228. 'commission_share'=> 0.02,
  229. 'order_number' => '',
  230. 'order_sn' => $order_sn,
  231. 'amount_price' => $money,
  232. 'fanli_price' => 0,
  233. 'cost_price' => 0,
  234. 'pay_money' => $money,
  235. 'status' => 3,
  236. 'create_time' => date('Y-m-d H:i:s'),
  237. 'finish_time' => date('Y-m-d H:i:s'),
  238. 'modify_time' => date('Y-m-d H:i:s'),
  239. ];
  240. Db::name('order_huafei')->insert($insertdata);
  241. return $push;
  242. }
  243. }
  244. //话费充值记录
  245. public function record(){
  246. $uid = $this->request->uid();
  247. [$page, $limit] = $this->getPage();
  248. $where = [];
  249. $where[] = ['uid', '=', $uid];
  250. $query = Db::name("order_huafei");
  251. $count = $query->where($where)->count();
  252. // ->page($page, $limit)
  253. $list = $query->where($where)->order('order_huafei_id DESC')->select()->each(function ($item){
  254. return $item;
  255. });
  256. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count, 'uid'=>$uid]);
  257. }
  258. //话费充值验证
  259. public function verify($order_number, $money){
  260. $request['mobile'] = $order_number;
  261. $request['amount'] = $money;
  262. $request['recharge_type'] = 0;
  263. return $this->execute('/rest/Recharge/verify', $request);
  264. }
  265. //话费订单充值推送
  266. public function pushOrder($order_sn, $phone, $money){
  267. $request['store_id'] = '9189';
  268. $request['order_no'] = $order_sn;
  269. $request['mobile'] = $phone;
  270. $request['money'] = $money;
  271. $request['recharge_type'] = 0;
  272. $request['notify_url'] = 'https://' . request()->host() . '/api/huafei/notify';
  273. return $this->execute('/rest/Recharge/PushOrder', $request);
  274. }
  275. //话费订单查询
  276. public function query($order_number){
  277. $request['store_id'] = '9189';
  278. $request['order_number'] = $order_number;
  279. return $this->execute('/rest/Recharge/query', $request);
  280. }
  281. //话费订单取消
  282. public function cancel($order_number){
  283. $request['order_number'] = $order_number;
  284. return $this->execute('/rest/Recharge/cancel', $request);
  285. }
  286. protected function generateSign($params){
  287. ksort($params);
  288. $stringToBeSigned = $this->secretKey;
  289. foreach ($params as $k => $v)
  290. {
  291. if (is_array($v)) {
  292. $stringToBeSigned .= $k;
  293. }else{
  294. if ("@" != substr($v, 0, 1)) {
  295. $stringToBeSigned .= "$k$v";
  296. }
  297. }
  298. }
  299. unset($k, $v);
  300. $stringToBeSigned .= $this->secretKey;
  301. return strtoupper(md5($stringToBeSigned));
  302. }
  303. public function curl($url, $postFields = null,$timeOut = 20){
  304. $ch = curl_init();
  305. curl_setopt($ch, CURLOPT_URL, $url);
  306. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  307. curl_setopt($ch, CURLOPT_REFERER, 'https://api.hxxfb.com');
  308. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  309. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  310. //https 请求
  311. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  312. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  313. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  314. }
  315. if (is_array($postFields) && 0 < count($postFields)){
  316. $postBodyString = "";
  317. $postMultipart = false;
  318. foreach ($postFields as $k => $v){
  319. if("@" != substr($v, 0, 1))//判断是不是文件上传
  320. {
  321. $postBodyString .= "$k=" . urlencode($v) . "&";
  322. }
  323. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  324. {
  325. $postMultipart = true;
  326. }
  327. $postMultipart = true;
  328. }
  329. unset($k, $v);
  330. curl_setopt($ch, CURLOPT_POST, true);
  331. if ($postMultipart){
  332. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  333. }else{
  334. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  335. }
  336. }
  337. $reponse = curl_exec($ch);
  338. curl_close($ch);
  339. return json_decode($reponse, true);
  340. }
  341. /**
  342. * request api接口后缀,如 /recharge/pushOrder
  343. * apiParams 业务参数 数组
  344. */
  345. public function execute($request, $apiParams ,$timeOut =20){
  346. //组装系统参数
  347. $sysParams["app_key"] = $this->appkey;
  348. $sysParams["v"] = $this->apiVersion;
  349. $sysParams["format"] = $this->format;
  350. $sysParams["timestamp"] = time();
  351. $sysParams["client"] = '';
  352. //签名
  353. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  354. //系统参数放入GET请求串
  355. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  356. $requestUrl = $this->gatewayUrl.$request. "?";
  357. foreach ($sysParams as $sysParamKey => $sysParamValue){
  358. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  359. }
  360. $requestUrl = substr($requestUrl, 0, -1);
  361. return $this->curl($requestUrl, $apiParams ,$timeOut);
  362. }
  363. }