DyrPayService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. ];
  28. public function __construct()
  29. {
  30. $apiUrl = ConfigService::make()->getConfigByCode('pay_api_url');
  31. $this->apiUrl = $apiUrl ? $apiUrl : $this->apiUrl;
  32. $this->apiKey = ConfigService::make()->getConfigByCode('pay_api_key');
  33. $this->apiClientId = ConfigService::make()->getConfigByCode('pay_ userid');
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 查询
  47. * @param $no 快递单号
  48. * @param string $phone // 手机号
  49. * @param string $code 快递公司编号
  50. * @return bool
  51. */
  52. public function recharge($no, $account, $productId, $amount)
  53. {
  54. if (empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiClientId)) {
  55. $this->error = '接口参数未配置';
  56. return false;
  57. }
  58. $cacheKey = "caches:kd100:{$no}_{$productId}_{$account}";
  59. if (RedisService::get($cacheKey . '_lock')) {
  60. $this->error = '充值处理中~';
  61. return false;
  62. }
  63. $param = [
  64. 'userid' => $this->apiClientId, // 商户ID
  65. 'product_id' => $productId, // 产品ID
  66. 'out_trade_num' => $no, // 单号
  67. 'amount' => $amount, // 金额
  68. 'mobile' => $account, // 手机号
  69. 'notify_url' => url('/api/notify/dyr/' . $productId), // 回调地址
  70. ];
  71. //请求参数
  72. $param['sign'] = $this->makeSign($param);
  73. $url = $this->apiUrl.$this->apiUrls['recharge'];
  74. $result = httpRequest($url, $param, 'post', '', 5);
  75. $this->saveLog($cacheKey, ['url'=>$url,'param'=>$param,'result'=>$result]);
  76. return $result;
  77. }
  78. /**
  79. * 日志
  80. * @param $key
  81. * @param $data
  82. */
  83. public function saveLog($key, $data)
  84. {
  85. if(env('APP_DEBUG')){
  86. RedisService::set($key,$data, 7200);
  87. }
  88. }
  89. /**
  90. * 生成签名
  91. * @param $param
  92. * @return string
  93. */
  94. public function makeSign($param,$type=1)
  95. {
  96. // 字典排序
  97. ksort($param);
  98. // 拼接签名串
  99. $param = $type==2?urldecode(http_build_query($param)) : http_build_query($param);
  100. $sign_str = $param . '&apikey=' . $this->apiKey;
  101. // 签名
  102. $sign = strtoupper(md5($type==2? $sign_str : urldecode($sign_str)));
  103. return $sign;
  104. }
  105. }