Huafei.php 8.0 KB

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