Huafei.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. $param = $this->request->param();
  20. Db::name('log')->insert(['data'=>'话费充值回调数据'.json_encode($param)]);
  21. $order_no = $param['order_no'];
  22. $order_number = $param['order_number'];
  23. $huafeiInfo = Db::name('order_huafei')->where("order_number", $order_number)->where("order_sn", $order_no)->find();
  24. if(!$huafeiInfo) echo 'error';exit;
  25. $updateData = [
  26. 'amount_price' => $param['amount'],
  27. 'fanli_price' => $param['fanli'],
  28. 'cost_price' => $param['cost_price'],
  29. 'arrived_amount'=> $param['arrived_amount'],
  30. 'status' => $param['status'],
  31. 'modify_time' => time(),
  32. ];
  33. $ret = Db::name("order_huafei")->where("order_number", $order_number)->where("order_sn", $order_no)->update($updateData);
  34. if($ret){
  35. //判断数据库有无此订单
  36. $type_shop = 9;
  37. $orderThird = app()->make(UserThirdRepository::class);
  38. //预估养老金
  39. $orderThird->yuguOrder(
  40. $huafeiInfo['order_pdd_id'],
  41. $huafeiInfo['commission'],
  42. $huafeiInfo['uid'],
  43. $huafeiInfo['order_sn'],
  44. $type_shop,
  45. 'order_huafei');
  46. //预估积分
  47. $orderThird->yuguJifen(
  48. $huafeiInfo['commission'] ,
  49. $huafeiInfo['uid'],
  50. $huafeiInfo['order_sn'],
  51. $type_shop
  52. );
  53. echo 'success';exit;
  54. }else{
  55. Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']);
  56. echo 'error';exit;
  57. }
  58. }
  59. //充值话费面额
  60. public function denomination(){
  61. $data = [
  62. ['money'=>50, 'name'=>'50元'],
  63. ['money'=>100, 'name'=>'100元'],
  64. ['money'=>200, 'name'=>'200元'],
  65. ];
  66. foreach($data as $k=>$v){
  67. $commissionMoney = $v['money']*0.04;
  68. $systemvalueObj = $goods_obj = Db::name('system_config_value')->where('config_key','sanfangfenpei')->find();
  69. $sanfangfenpei=json_decode($systemvalueObj['value'],true);
  70. $commissionMoney80=$commissionMoney*0.8;
  71. $pv=$commissionMoney80*$sanfangfenpei['pv']; //PV
  72. $yanglaojin=$commissionMoney80*$sanfangfenpei['yanglaojin']; //新的养老金
  73. $gongxian=$pv; //新的养老金
  74. $jifen=$pv; //新的养老金
  75. $data[$k]['pv']=round($pv,2);
  76. $data[$k]['yanglaojin']=round($yanglaojin,2);
  77. $data[$k]['gongxian']=round($gongxian,2);
  78. $data[$k]['jifen']=round($jifen,2);
  79. }
  80. return app('json')->success('获取话费面额成功', $data);
  81. }
  82. //充值话费推送
  83. public function recharge(){
  84. $uid = $this->request->uid();
  85. $params = $this->request->params(['phone', 'money']);
  86. if(!$params['phone']) return app('json')->fail('请提交电话号码');
  87. if(!$params['money']) return app('json')->fail('请提交充值金额');
  88. //验证
  89. $verify = $this->verify($params['phone'], $params['money']);
  90. if($verify['code'] == '0000'){
  91. $order_sn = time().$uid.mt_rand(1000,9999);
  92. //创建预支付订单
  93. $insertdata = [
  94. 'uid'=> $uid,
  95. 'mobile' => $params['phone'],
  96. 'order_no' => $order_sn,
  97. 'pay_price' => $params['money'],
  98. 'status' => 0,
  99. 'create_time' => time(),
  100. ];
  101. $oid = Db::name('order_huafei_hf')->insertGetId($insertdata);
  102. if($oid){
  103. return app('json')->success('订单创建成功', ['oid'=>$oid, 'order_sn'=>$order_sn]);
  104. }else{
  105. return app('json')->fail('创建话费充值预支付订单失败');
  106. }
  107. }else{
  108. return app('json')->fail($verify['msg']);
  109. }
  110. }
  111. //话费充值推送
  112. public function pushHf($uid, $order_sn, $phone, $money){
  113. //推送订单
  114. $push = $this->pushOrder($order_sn, $phone, $money);
  115. if($push['code'] == '0000'){
  116. $commissionMoney = $money*0.04;
  117. //入库记录
  118. $insertdata = [
  119. 'uid'=> $uid,
  120. 'mobile' => $phone,
  121. 'commission'=> $commissionMoney,
  122. 'commission_share'=> 0.04,
  123. 'order_number' => $push['data']['order_number'],
  124. 'order_sn' => $order_sn,
  125. 'amount_price' => $push['data']['amount'],
  126. 'fanli_price' => $push['data']['fanli'],
  127. 'cost_price' => $push['data']['cost_price'],
  128. 'pay_money' => $money,
  129. 'status' => 0,
  130. 'create_time' => time(),
  131. 'finish_time' => time(),
  132. 'modify_time' => time(),
  133. ];
  134. Db::name('order_huafei')->insert($insertdata);
  135. return $push;
  136. }else{
  137. return false;
  138. }
  139. }
  140. //话费充值记录
  141. public function record(){
  142. $uid = $this->request->uid();
  143. [$page, $limit] = $this->getPage();
  144. $where = [];
  145. $where[] = ['uid', '=', $uid];
  146. $query = Db::name("order_huafei");
  147. $count = $query->where($where)->count();
  148. $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){
  149. $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
  150. $item['status'] = (int)$item['status'];
  151. return $item;
  152. });
  153. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count]);
  154. }
  155. //话费充值验证
  156. public function verify($order_number, $money){
  157. $request['mobile'] = $order_number;
  158. $request['amount'] = $money;
  159. $request['recharge_type'] = 1;
  160. return $this->execute('/rest/Recharge/verify', $request);
  161. }
  162. //话费订单充值推送
  163. public function pushOrder($order_sn, $phone, $money){
  164. $request['store_id'] = '9189';
  165. $request['order_no'] = $order_sn;
  166. $request['mobile'] = $phone;
  167. $request['money'] = $money;
  168. $request['recharge_type'] = 1;
  169. $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify';
  170. return $this->execute('/rest/Recharge/PushOrder', $request);
  171. }
  172. //话费订单查询
  173. public function query($order_number){
  174. $request['store_id'] = '9189';
  175. $request['order_number'] = $order_number;
  176. return $this->execute('/rest/Recharge/query', $request);
  177. }
  178. //话费订单取消
  179. public function cancel($order_number){
  180. $request['order_number'] = $order_number;
  181. return $this->execute('/rest/Recharge/cancel', $request);
  182. }
  183. protected function generateSign($params){
  184. ksort($params);
  185. $stringToBeSigned = $this->secretKey;
  186. foreach ($params as $k => $v)
  187. {
  188. if (is_array($v)) {
  189. $stringToBeSigned .= $k;
  190. }else{
  191. if ("@" != substr($v, 0, 1)) {
  192. $stringToBeSigned .= "$k$v";
  193. }
  194. }
  195. }
  196. unset($k, $v);
  197. $stringToBeSigned .= $this->secretKey;
  198. return strtoupper(md5($stringToBeSigned));
  199. }
  200. public function curl($url, $postFields = null,$timeOut = 20){
  201. $ch = curl_init();
  202. curl_setopt($ch, CURLOPT_URL, $url);
  203. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  204. curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
  205. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  206. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  207. //https 请求
  208. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  209. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  210. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  211. }
  212. if (is_array($postFields) && 0 < count($postFields)){
  213. $postBodyString = "";
  214. $postMultipart = false;
  215. foreach ($postFields as $k => $v){
  216. if("@" != substr($v, 0, 1))//判断是不是文件上传
  217. {
  218. $postBodyString .= "$k=" . urlencode($v) . "&";
  219. }
  220. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  221. {
  222. $postMultipart = true;
  223. }
  224. $postMultipart = true;
  225. }
  226. unset($k, $v);
  227. curl_setopt($ch, CURLOPT_POST, true);
  228. if ($postMultipart){
  229. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  230. }else{
  231. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  232. }
  233. }
  234. $reponse = curl_exec($ch);
  235. curl_close($ch);
  236. return json_decode($reponse, true);
  237. }
  238. /**
  239. * request api接口后缀,如 /recharge/pushOrder
  240. * apiParams 业务参数 数组
  241. */
  242. public function execute($request, $apiParams ,$timeOut =20){
  243. //组装系统参数
  244. $sysParams["app_key"] = $this->appkey;
  245. $sysParams["v"] = $this->apiVersion;
  246. $sysParams["format"] = $this->format;
  247. $sysParams["timestamp"] = time();
  248. $sysParams["client"] = '';
  249. //签名
  250. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  251. //系统参数放入GET请求串
  252. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  253. $requestUrl = $this->gatewayUrl.$request. "?";
  254. foreach ($sysParams as $sysParamKey => $sysParamValue){
  255. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  256. }
  257. $requestUrl = substr($requestUrl, 0, -1);
  258. return $this->curl($requestUrl, $apiParams ,$timeOut);
  259. }
  260. }