Huafei.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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['status']=(int)$item['status'];
  100. return $item;
  101. });
  102. return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count, 'limit'=>$limit, 'page'=>$page]);
  103. }
  104. //话费充值验证
  105. public function verify($order_number, $money){
  106. $request['mobile'] = $order_number;
  107. $request['amount'] = $money;
  108. $request['recharge_type'] = 1;
  109. return $this->execute('/rest/Recharge/verify', $request);
  110. }
  111. //话费订单充值推送
  112. public function pushOrder($order_sn, $phone, $money){
  113. $request['store_id'] = '9189';
  114. $request['order_no'] = $order_sn;
  115. $request['mobile'] = $phone;
  116. $request['money'] = $money;
  117. $request['recharge_type'] = 1;
  118. $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify';
  119. return $this->execute('/rest/Recharge/PushOrder', $request);
  120. }
  121. //话费订单查询
  122. public function query($order_number){
  123. $request['store_id'] = '9189';
  124. $request['order_number'] = $order_number;
  125. return $this->execute('/rest/Recharge/query', $request);
  126. }
  127. //话费订单取消
  128. public function cancel($order_number){
  129. $request['order_number'] = $order_number;
  130. return $this->execute('/rest/Recharge/cancel', $request);
  131. }
  132. protected function generateSign($params){
  133. ksort($params);
  134. $stringToBeSigned = $this->secretKey;
  135. foreach ($params as $k => $v)
  136. {
  137. if (is_array($v)) {
  138. $stringToBeSigned .= $k;
  139. }else{
  140. if ("@" != substr($v, 0, 1)) {
  141. $stringToBeSigned .= "$k$v";
  142. }
  143. }
  144. }
  145. unset($k, $v);
  146. $stringToBeSigned .= $this->secretKey;
  147. return strtoupper(md5($stringToBeSigned));
  148. }
  149. public function curl($url, $postFields = null,$timeOut = 20){
  150. $ch = curl_init();
  151. curl_setopt($ch, CURLOPT_URL, $url);
  152. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  153. curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com');
  154. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  155. curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut);
  156. //https 请求
  157. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  158. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  159. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  160. }
  161. if (is_array($postFields) && 0 < count($postFields)){
  162. $postBodyString = "";
  163. $postMultipart = false;
  164. foreach ($postFields as $k => $v){
  165. if("@" != substr($v, 0, 1))//判断是不是文件上传
  166. {
  167. $postBodyString .= "$k=" . urlencode($v) . "&";
  168. }
  169. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  170. {
  171. $postMultipart = true;
  172. }
  173. $postMultipart = true;
  174. }
  175. unset($k, $v);
  176. curl_setopt($ch, CURLOPT_POST, true);
  177. if ($postMultipart){
  178. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  179. }else{
  180. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  181. }
  182. }
  183. $reponse = curl_exec($ch);
  184. curl_close($ch);
  185. return json_decode($reponse, true);
  186. }
  187. /**
  188. * request api接口后缀,如 /recharge/pushOrder
  189. * apiParams 业务参数 数组
  190. */
  191. public function execute($request, $apiParams ,$timeOut =20){
  192. //组装系统参数
  193. $sysParams["app_key"] = $this->appkey;
  194. $sysParams["v"] = $this->apiVersion;
  195. $sysParams["format"] = $this->format;
  196. $sysParams["timestamp"] = time();
  197. $sysParams["client"] = '';
  198. //签名
  199. $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams));
  200. //系统参数放入GET请求串
  201. //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?";
  202. $requestUrl = $this->gatewayUrl.$request. "?";
  203. foreach ($sysParams as $sysParamKey => $sysParamValue){
  204. $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
  205. }
  206. $requestUrl = substr($requestUrl, 0, -1);
  207. return $this->curl($requestUrl, $apiParams ,$timeOut);
  208. }
  209. }