Huafei.php 13 KB

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