Huafei.php 8.2 KB

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