Huafei.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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('link_id', $order_data['order_id'])->update(['status'=> -1]);
  87. Db::name('user_sign_score')->where('order_id', $order_data['order_id'])->update(['status'=> 0]);
  88. Db::name('user_sign_gongxian')->where('order_id', $order_data['order_id'])->update(['status'=> 0]);
  89. Db::name('user_sign_fugou')->where('order_id', $order_data['order_id'])->update(['status'=> 0]);
  90. Db::name('user_sign_hongbao')->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. }else{
  164. return app('json')->fail($verify['msg'].'MSG');
  165. }
  166. }
  167. //话费充值推送
  168. public function pushHf($uid, $order_sn, $phone, $money, $order_id = 0){
  169. $commissionMoney = $money*0.02;
  170. //判断数据库有无此订单
  171. $type_shop = 9;
  172. $orderThird = app()->make(UserThirdRepository::class);
  173. //预估养老金
  174. $orderThird->yuguOrder(
  175. $order_id,
  176. $commissionMoney,
  177. $uid,
  178. $order_sn,
  179. $type_shop,
  180. 'order_huafei');
  181. //预估积分&贡献值
  182. $orderThird->yuguJifen(
  183. $order_id,
  184. $commissionMoney,
  185. $uid,
  186. $order_sn,
  187. $type_shop
  188. );
  189. // //复购金
  190. // $orderThird->yuguFugou(
  191. // $commissionMoney,
  192. // $uid,
  193. // $order_sn,
  194. // $type_shop
  195. // );
  196. //推送订单
  197. $push = $this->pushOrder($order_sn, $phone, $money);
  198. Db::name('log')->insert(['data'=>'话费充值推送订单返回数据'.json_encode($push)]);
  199. if($push['code'] == '0000'){
  200. $commissionMoney = $money*0.02;
  201. //成功入库记录
  202. $insertdata = [
  203. 'uid'=> $uid,
  204. 'mobile' => $phone,
  205. 'commission'=> $commissionMoney,
  206. 'commission_share'=> 0.02,
  207. 'order_number' => $push['data']['order_number'],
  208. 'order_sn' => $order_sn,
  209. 'amount_price' => $push['data']['amount'],
  210. 'fanli_price' => $push['data']['fanli'],
  211. 'cost_price' => $push['data']['cost_price'],
  212. 'pay_money' => $money,
  213. 'status' => 1,
  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. }else{
  221. $commissionMoney = $money*0.02;
  222. //失败入库记录
  223. $insertdata = [
  224. 'uid'=> $uid,
  225. 'mobile' => $phone,
  226. 'commission'=> $commissionMoney,
  227. 'commission_share'=> 0.02,
  228. 'order_number' => '',
  229. 'order_sn' => $order_sn,
  230. 'amount_price' => $money,
  231. 'fanli_price' => 0,
  232. 'cost_price' => 0,
  233. 'pay_money' => $money,
  234. 'status' => 3,
  235. 'create_time' => date('Y-m-d H:i:s'),
  236. 'finish_time' => date('Y-m-d H:i:s'),
  237. 'modify_time' => date('Y-m-d H:i:s'),
  238. ];
  239. Db::name('order_huafei')->insert($insertdata);
  240. return $push;
  241. }
  242. }
  243. //话费充值记录
  244. public function record(){
  245. $uid = $this->request->uid();
  246. [$page, $limit] = $this->getPage();
  247. $where = [];
  248. $where[] = ['uid', '=', $uid];
  249. $query = Db::name("order_huafei");
  250. $count = $query->where($where)->count();
  251. $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){
  252. return $item;
  253. });
  254. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count, 'uid'=>$uid]);
  255. }
  256. //话费充值验证
  257. public function verify($order_number, $money){
  258. $request['mobile'] = $order_number;
  259. $request['amount'] = $money;
  260. $request['recharge_type'] = 0;
  261. return $this->execute('/rest/Recharge/verify', $request);
  262. }
  263. //话费订单充值推送
  264. public function pushOrder($order_sn, $phone, $money){
  265. $request['store_id'] = '9189';
  266. $request['order_no'] = $order_sn;
  267. $request['mobile'] = $phone;
  268. $request['money'] = $money;
  269. $request['recharge_type'] = 0;
  270. $request['notify_url'] = 'https://' . request()->host() . '/api/huafei/notify';
  271. return $this->execute('/rest/Recharge/PushOrder', $request);
  272. }
  273. //话费订单查询
  274. public function query($order_number){
  275. $request['store_id'] = '9189';
  276. $request['order_number'] = $order_number;
  277. return $this->execute('/rest/Recharge/query', $request);
  278. }
  279. //话费订单取消
  280. public function cancel($order_number){
  281. $request['order_number'] = $order_number;
  282. return $this->execute('/rest/Recharge/cancel', $request);
  283. }
  284. protected function generateSign($params){
  285. ksort($params);
  286. $stringToBeSigned = $this->secretKey;
  287. foreach ($params as $k => $v)
  288. {
  289. if (is_array($v)) {
  290. $stringToBeSigned .= $k;
  291. }else{
  292. if ("@" != substr($v, 0, 1)) {
  293. $stringToBeSigned .= "$k$v";
  294. }
  295. }
  296. }
  297. unset($k, $v);
  298. $stringToBeSigned .= $this->secretKey;
  299. return strtoupper(md5($stringToBeSigned));
  300. }
  301. public function curl($url, $postFields = null,$timeOut = 20){
  302. $ch = curl_init();
  303. curl_setopt($ch, CURLOPT_URL, $url);
  304. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  305. curl_setopt($ch, CURLOPT_REFERER, 'https://api.hxxfb.com');
  306. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  307. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  308. //https 请求
  309. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  310. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  311. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  312. }
  313. if (is_array($postFields) && 0 < count($postFields)){
  314. $postBodyString = "";
  315. $postMultipart = false;
  316. foreach ($postFields as $k => $v){
  317. if("@" != substr($v, 0, 1))//判断是不是文件上传
  318. {
  319. $postBodyString .= "$k=" . urlencode($v) . "&";
  320. }
  321. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  322. {
  323. $postMultipart = true;
  324. }
  325. $postMultipart = true;
  326. }
  327. unset($k, $v);
  328. curl_setopt($ch, CURLOPT_POST, true);
  329. if ($postMultipart){
  330. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  331. }else{
  332. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  333. }
  334. }
  335. $reponse = curl_exec($ch);
  336. curl_close($ch);
  337. return json_decode($reponse, true);
  338. }
  339. /**
  340. * request api接口后缀,如 /recharge/pushOrder
  341. * apiParams 业务参数 数组
  342. */
  343. public function execute($request, $apiParams ,$timeOut =20){
  344. //组装系统参数
  345. $sysParams["app_key"] = $this->appkey;
  346. $sysParams["v"] = $this->apiVersion;
  347. $sysParams["format"] = $this->format;
  348. $sysParams["timestamp"] = time();
  349. $sysParams["client"] = '';
  350. //签名
  351. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  352. //系统参数放入GET请求串
  353. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  354. $requestUrl = $this->gatewayUrl.$request. "?";
  355. foreach ($sysParams as $sysParamKey => $sysParamValue){
  356. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  357. }
  358. $requestUrl = substr($requestUrl, 0, -1);
  359. return $this->curl($requestUrl, $apiParams ,$timeOut);
  360. }
  361. }