DyrPayService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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)
  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. //请求参数
  74. $param['sign'] = $this->makeSign($param);
  75. $url = $this->apiUrl.$this->apiUrls['recharge'];
  76. $result = httpRequest($url, $param, 'post', '', 5);
  77. $this->saveLog($cacheKey, ['url'=>$url,'param'=>$param,'result'=>$result]);
  78. return $result;
  79. }
  80. /**
  81. * 查询订单
  82. * @param $nos 查询单号
  83. * @return bool
  84. */
  85. public function query($nos)
  86. {
  87. if (empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiClientId)) {
  88. $this->error = '接口参数未配置';
  89. return false;
  90. }
  91. $cacheKey = "caches:dryPay:queryOrder:".md5($nos);
  92. if (RedisService::get($cacheKey . '_lock')) {
  93. $this->error = '充值处理中~';
  94. return false;
  95. }
  96. $param = [
  97. 'userid' => $this->apiClientId, // 商户ID
  98. 'out_trade_nums' => $nos, // 单号
  99. ];
  100. //请求参数
  101. $param['sign'] = $this->makeSign($param);
  102. $url = $this->apiUrl.$this->apiUrls['query'];
  103. $result = httpRequest($url, $param, 'post', '', 5);
  104. $this->saveLog($cacheKey, ['url'=>$url,'param'=>$param,'result'=>$result]);
  105. return $result;
  106. }
  107. /**
  108. * 日志
  109. * @param $key
  110. * @param $data
  111. */
  112. public function saveLog($key, $data)
  113. {
  114. if(env('APP_DEBUG')){
  115. RedisService::set($key,$data, 7200);
  116. }
  117. }
  118. /**
  119. * 生成签名
  120. * @param $param
  121. * @return string
  122. */
  123. public function makeSign($param,$type=1)
  124. {
  125. // 字典排序
  126. ksort($param);
  127. // 拼接签名串
  128. $param = $type==2?urldecode(http_build_query($param)) : http_build_query($param);
  129. $sign_str = $param . '&apikey=' . $this->apiKey;
  130. // 签名
  131. $sign = strtoupper(md5($type==2? $sign_str : urldecode($sign_str)));
  132. return $sign;
  133. }
  134. }