UdunpayService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  13. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  14. use Darabonba\OpenApi\Models\Config;
  15. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  16. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  17. use Udun\Dispatch\UdunDispatch;
  18. /**
  19. * udun钱包服务管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * Class UdunpayService
  23. * @package App\Services
  24. */
  25. class UdunpayService extends BaseService
  26. {
  27. // 静态对象
  28. protected static $instance = null;
  29. protected $dispatch = null;
  30. protected static $config = [];
  31. public function __construct()
  32. {
  33. $config = ConfigService::make()->getConfigByGroup(10);
  34. self::$config = [
  35. 'merchant_no' => isset($config['udun_merchant_no']['value'])? $config['udun_merchant_no']['value'] : '', //商户号
  36. 'api_key' => isset($config['udun_api_key']['value'])? $config['udun_api_key']['value'] : '', //apikey
  37. 'gateway_address' => isset($config['udun_gateway_address']['value'])? $config['udun_gateway_address']['value'] : '', //节点
  38. 'callUrl'=> url('/notify/wallet/udunpay','',true), //回调地址
  39. 'debug' => false //调试模式
  40. ];
  41. $this->dispatch = new UdunDispatch(self::$config);
  42. }
  43. /**
  44. * 静态入口
  45. * @return UdunpayService|static|null
  46. */
  47. public static function make(){
  48. if(!self::$instance){
  49. self::$instance = new static();
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 获取支持的币种
  55. * @param bool $showBalance 是否返回余额
  56. * @return mixed
  57. */
  58. public function supportCoins($showBalance=true)
  59. {
  60. return $this->dispatch->supportCoins($showBalance);
  61. }
  62. /**
  63. * 创建钱包地址
  64. * @param string $mainCoinType
  65. * @return mixed
  66. */
  67. public function createAddress($mainCoinType='195')
  68. {
  69. $result = $this->dispatch->createAddress($mainCoinType);
  70. $code = isset($result['code'])? intval($result['code']) : 0;
  71. $address = isset($result['data']['address'])? trim($result['data']['address']) : '';
  72. if($code == 200 && $address){
  73. return $address;
  74. }else{
  75. RedisService::set("caches:udunpay:createAddress:error_".date('YmdHis'),['mainCoinType'=>$mainCoinType,'result'=> $result], 7200);
  76. return '';
  77. }
  78. }
  79. /**
  80. * 验证钱包地址合法性
  81. * @param $address 地址
  82. * @param string $mainCoinType 主币
  83. * @return mixed
  84. */
  85. public function checkAddress($address,$mainCoinType='195')
  86. {
  87. return $this->dispatch->checkAddress($mainCoinType,$address);
  88. }
  89. /**
  90. * 查询地址是否存在
  91. * @return mixed
  92. */
  93. public function existAddress($address,$mainCoinType='195')
  94. {
  95. return $this->dispatch->existAddress($mainCoinType,$address);
  96. }
  97. /**
  98. * 提币
  99. * @return mixed
  100. */
  101. public function withdraw($address, $amount, $orderNo, $mainCoinType='195', $coinType='', $remark='')
  102. {
  103. $coinType = $coinType? $coinType : 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
  104. return $this->dispatch->withdraw($orderNo,$mainCoinType,$coinType,$address,$amount,$remark);
  105. }
  106. /**
  107. * 回调
  108. * @return mixed
  109. */
  110. public function callback()
  111. {
  112. $result = $this->dispatch->callback();
  113. RedisService::set("caches:udunpay:notify",['params'=> request()->all(),'result'=> $result], 3600);
  114. return $result;
  115. }
  116. /**
  117. * 回调处理
  118. * @param $params
  119. */
  120. public function catchNotify($params)
  121. {
  122. RedisService::set('caches:udunpay:notify',$params);
  123. }
  124. }