Huafei.php 10 KB

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