Huafei.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. return app('json')->success('获取话费面额成功', $data);
  50. }
  51. //充值话费推送
  52. public function recharge(){
  53. $uid = $this->request->uid();
  54. $params = $this->request->params(['phone', 'money']);
  55. if(!$params['phone']) return app('json')->fail('请提交电话号码');
  56. if(!$params['money']) return app('json')->fail('请提交充值金额');
  57. //验证
  58. $verify = $this->verify($params['phone'], $params['money']);
  59. if($verify['code'] == '0000'){
  60. //推送
  61. $order_sn = time().$uid;
  62. $push = $this->pushOrder($order_sn, $params['phone'], $params['money']);
  63. if($push['code'] == '0000'){
  64. //入库记录
  65. $insertdata = [
  66. 'uid'=> $uid,
  67. 'mobile' => $params['phone'],
  68. 'commission'=> 0,
  69. 'commission_share'=> 0,
  70. 'order_number' => $push['data']['order_number'],
  71. 'order_sn' => $order_sn,
  72. 'amount_price' => $push['data']['amount'],
  73. 'fanli_price' => $push['data']['fanli'],
  74. 'cost_price' => $push['data']['cost_price'],
  75. 'pay_money' => $params['money'],
  76. 'status' => 0,
  77. 'create_time' => time(),
  78. 'finish_time' => time(),
  79. 'modify_time' => time(),
  80. ];
  81. Db::name('order_huafei')->insert($insertdata);
  82. return app('json')->success('话费推送成功', $push);
  83. }else{
  84. return app('json')->fail($push['msg']);
  85. }
  86. }else{
  87. return app('json')->fail($verify['msg']);
  88. }
  89. }
  90. //话费充值记录
  91. public function record(){
  92. $uid = $this->request->uid();
  93. [$page, $limit] = $this->getPage();
  94. $where = [];
  95. $where[] = ['uid', '=', $uid];
  96. $query = Db::name("order_huafei");
  97. $count = $query->where($where)->count();
  98. $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){
  99. $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
  100. $item['status'] = (int)$item['status'];
  101. return $item;
  102. });
  103. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count]);
  104. }
  105. //话费充值验证
  106. public function verify($order_number, $money){
  107. $request['mobile'] = $order_number;
  108. $request['amount'] = $money;
  109. $request['recharge_type'] = 1;
  110. return $this->execute('/rest/Recharge/verify', $request);
  111. }
  112. //话费订单充值推送
  113. public function pushOrder($order_sn, $phone, $money){
  114. $request['store_id'] = '9189';
  115. $request['order_no'] = $order_sn;
  116. $request['mobile'] = $phone;
  117. $request['money'] = $money;
  118. $request['recharge_type'] = 1;
  119. $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify';
  120. return $this->execute('/rest/Recharge/PushOrder', $request);
  121. }
  122. //话费订单查询
  123. public function query($order_number){
  124. $request['store_id'] = '9189';
  125. $request['order_number'] = $order_number;
  126. return $this->execute('/rest/Recharge/query', $request);
  127. }
  128. //话费订单取消
  129. public function cancel($order_number){
  130. $request['order_number'] = $order_number;
  131. return $this->execute('/rest/Recharge/cancel', $request);
  132. }
  133. protected function generateSign($params){
  134. ksort($params);
  135. $stringToBeSigned = $this->secretKey;
  136. foreach ($params as $k => $v)
  137. {
  138. if (is_array($v)) {
  139. $stringToBeSigned .= $k;
  140. }else{
  141. if ("@" != substr($v, 0, 1)) {
  142. $stringToBeSigned .= "$k$v";
  143. }
  144. }
  145. }
  146. unset($k, $v);
  147. $stringToBeSigned .= $this->secretKey;
  148. return strtoupper(md5($stringToBeSigned));
  149. }
  150. public function curl($url, $postFields = null,$timeOut = 20){
  151. $ch = curl_init();
  152. curl_setopt($ch, CURLOPT_URL, $url);
  153. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  154. curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
  155. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  156. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  157. //https 请求
  158. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  159. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  160. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  161. }
  162. if (is_array($postFields) && 0 < count($postFields)){
  163. $postBodyString = "";
  164. $postMultipart = false;
  165. foreach ($postFields as $k => $v){
  166. if("@" != substr($v, 0, 1))//判断是不是文件上传
  167. {
  168. $postBodyString .= "$k=" . urlencode($v) . "&";
  169. }
  170. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  171. {
  172. $postMultipart = true;
  173. }
  174. $postMultipart = true;
  175. }
  176. unset($k, $v);
  177. curl_setopt($ch, CURLOPT_POST, true);
  178. if ($postMultipart){
  179. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  180. }else{
  181. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  182. }
  183. }
  184. $reponse = curl_exec($ch);
  185. curl_close($ch);
  186. return json_decode($reponse, true);
  187. }
  188. /**
  189. * request api接口后缀,如 /recharge/pushOrder
  190. * apiParams 业务参数 数组
  191. */
  192. public function execute($request, $apiParams ,$timeOut =20){
  193. //组装系统参数
  194. $sysParams["app_key"] = $this->appkey;
  195. $sysParams["v"] = $this->apiVersion;
  196. $sysParams["format"] = $this->format;
  197. $sysParams["timestamp"] = time();
  198. $sysParams["client"] = '';
  199. //签名
  200. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  201. //系统参数放入GET请求串
  202. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  203. $requestUrl = $this->gatewayUrl.$request. "?";
  204. foreach ($sysParams as $sysParamKey => $sysParamValue){
  205. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  206. }
  207. $requestUrl = substr($requestUrl, 0, -1);
  208. return $this->curl($requestUrl, $apiParams ,$timeOut);
  209. }
  210. }