Huafei.php 6.3 KB

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