DyrPayService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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' => $productId, // 产品ID
  68. 'out_trade_num' => $no, // 单号
  69. 'amount' => $amount, // 金额
  70. 'mobile' => $account, // 手机号
  71. 'notify_url' => url('/api/notify/dyr/' . $productId), // 回调地址
  72. ];
  73. if(isset($params['ytype']) && $params['ytype']){
  74. $param['ytype'] = intval($params['ytype']);
  75. }
  76. if(isset($params['id_card_no']) && $params['id_card_no']){
  77. $param['id_card_no'] = intval($params['id_card_no']);
  78. }
  79. if(isset($params['area']) && $params['area']){
  80. $param['area'] = intval($params['area']);
  81. }
  82. if(isset($params['city']) && $params['city']){
  83. $param['city'] = intval($params['city']);
  84. }
  85. //请求参数
  86. $param['sign'] = $this->makeSign($param);
  87. $url = $this->apiUrl.$this->apiUrls['recharge'];
  88. $result = httpRequest($url, $param, 'post', '', 5);
  89. $this->saveLog($cacheKey, ['url'=>$url,'param'=>$param,'result'=>$result]);
  90. return $result;
  91. }
  92. /**
  93. * 查询订单
  94. * @param $nos 查询单号
  95. * @return bool
  96. */
  97. public function query($nos)
  98. {
  99. if (empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiClientId)) {
  100. $this->error = '接口参数未配置';
  101. return false;
  102. }
  103. $cacheKey = "caches:dryPay:queryOrder:".md5($nos);
  104. if (RedisService::get($cacheKey . '_lock')) {
  105. $this->error = '充值处理中~';
  106. return false;
  107. }
  108. $param = [
  109. 'userid' => $this->apiClientId, // 商户ID
  110. 'out_trade_nums' => $nos, // 单号
  111. ];
  112. //请求参数
  113. $param['sign'] = $this->makeSign($param);
  114. $url = $this->apiUrl.$this->apiUrls['query'];
  115. $result = httpRequest($url, $param, 'post', '', 5);
  116. $this->saveLog($cacheKey, ['url'=>$url,'param'=>$param,'result'=>$result]);
  117. return $result;
  118. }
  119. /**
  120. * 日志
  121. * @param $key
  122. * @param $data
  123. */
  124. public function saveLog($key, $data)
  125. {
  126. if(env('APP_DEBUG')){
  127. RedisService::set($key,$data, 7200);
  128. }
  129. }
  130. /**
  131. * 生成签名
  132. * @param $param
  133. * @return string
  134. */
  135. public function makeSign($param,$type=1)
  136. {
  137. // 字典排序
  138. ksort($param);
  139. // 拼接签名串
  140. $param = $type==2?urldecode(http_build_query($param)) : http_build_query($param);
  141. $sign_str = $param . '&apikey=' . $this->apiKey;
  142. // 签名
  143. $sign = strtoupper(md5($type==2? $sign_str : urldecode($sign_str)));
  144. return $sign;
  145. }
  146. }