DyrPayService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. /**
  13. * 生活充值接口管理-服务类
  14. * @author laravel开发员
  15. * @since 2020/11/11
  16. * @package App\Services
  17. */
  18. class DyrPayService extends BaseService
  19. {
  20. // 静态对象
  21. protected static $instance = null;
  22. protected $apiUrl = 'http://1.huichikeji.cn/yrapi.php';
  23. protected $apiKey = '';
  24. protected $apiClientId = '';
  25. protected $apiUrls = [
  26. 'recharge' => '/index/recharge',
  27. 'query' => '/index/check',
  28. ];
  29. public function __construct()
  30. {
  31. $apiUrl = ConfigService::make()->getConfigByCode('pay_api_url');
  32. $this->apiUrl = $apiUrl ? $apiUrl : $this->apiUrl;
  33. $this->apiKey = ConfigService::make()->getConfigByCode('pay_api_key');
  34. $this->apiClientId = ConfigService::make()->getConfigByCode('pay_ userid');
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 充值
  48. * @param $no 单号
  49. * @param string $account // 充值账号
  50. * @param string $productId 产品 ID
  51. * @param string $amount 充值面值
  52. * @return bool
  53. */
  54. public function recharge($no, $account, $productId, $amount, $params=[])
  55. {
  56. if (empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiClientId)) {
  57. $this->error = '接口参数未配置';
  58. return false;
  59. }
  60. $cacheKey = "caches:dryPay:{$no}_{$productId}_{$account}";
  61. if (RedisService::get($cacheKey . '_lock')) {
  62. $this->error = '充值处理中~';
  63. return false;
  64. }
  65. $param = [
  66. 'userid' => $this->apiClientId, // 商户ID
  67. 'product_id' => trim($productId), // 产品ID
  68. 'out_trade_num' => $no, // 单号
  69. 'amount' => $amount, // 金额
  70. 'mobile' => $account, // 手机号或账号
  71. 'notify_url' => url('/api/dry/notify/' . $productId), // 回调地址
  72. ];
  73. if(isset($params['price']) && $params['price']){
  74. $param['price'] = floatval($params['price']);
  75. }
  76. if(isset($params['ytype']) && $params['ytype']){
  77. $param['ytype'] = intval($params['ytype']);
  78. }
  79. if(isset($params['id_card_no']) && $params['id_card_no']){
  80. $param['id_card_no'] = $params['id_card_no'];
  81. }
  82. if(isset($params['area']) && $params['area']){
  83. $param['area'] = $params['area'];
  84. }
  85. if(isset($params['city']) && $params['city']){
  86. $param['city'] = $params['city'];
  87. }
  88. //请求参数
  89. $param['sign'] = $this->makeSign($param);
  90. $url = $this->apiUrl.$this->apiUrls['recharge'];
  91. $result = httpRequest($url, $param, 'post', '', 5);
  92. $this->saveLog($cacheKey, ['url'=>$url,'param'=>$param,'result'=>$result]);
  93. return $result;
  94. }
  95. /**
  96. * 查询订单
  97. * @param $nos 查询单号
  98. * @return bool
  99. */
  100. public function query($nos)
  101. {
  102. if (empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiClientId)) {
  103. $this->error = '接口参数未配置';
  104. return false;
  105. }
  106. $cacheKey = "caches:dryPay:queryOrder:".md5($nos);
  107. if (RedisService::get($cacheKey . '_lock')) {
  108. $this->error = '充值处理中~';
  109. return false;
  110. }
  111. $param = [
  112. 'userid' => $this->apiClientId, // 商户ID
  113. 'out_trade_nums' => $nos, // 单号
  114. ];
  115. //请求参数
  116. $param['sign'] = $this->makeSign($param);
  117. $url = $this->apiUrl.$this->apiUrls['query'];
  118. $result = httpRequest($url, $param, 'post', '', 5);
  119. $this->saveLog($cacheKey, ['url'=>$url,'param'=>$param,'result'=>$result]);
  120. return $result;
  121. }
  122. /**
  123. * 日志
  124. * @param $key
  125. * @param $data
  126. */
  127. public function saveLog($key, $data)
  128. {
  129. if(env('APP_DEBUG')){
  130. RedisService::set($key,$data, 7200);
  131. }
  132. }
  133. /**
  134. * 生成签名
  135. * @param $param
  136. * @return string
  137. */
  138. public function makeSign($param,$type=1)
  139. {
  140. // 字典排序
  141. ksort($param);
  142. // 拼接签名串
  143. $param = $type==2?urldecode(http_build_query($param)) : http_build_query($param);
  144. $sign_str = $param . '&apikey=' . $this->apiKey;
  145. // 签名
  146. $sign = strtoupper(md5($type==2? $sign_str : urldecode($sign_str)));
  147. return $sign;
  148. }
  149. }