CregisPayService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Cregis\Dispatch\CregisDispatch;
  13. /**
  14. * Cregis钱包服务管理-服务类
  15. * @author laravel开发员
  16. * @since 2020/11/11
  17. * @package App\Services
  18. */
  19. class CregisPayService extends BaseService
  20. {
  21. // 静态对象
  22. protected static $instance = null;
  23. protected $dispatch = null;
  24. protected static $config = [];
  25. public function __construct()
  26. {
  27. $config = ConfigService::make()->getConfigByGroup(10);
  28. $notifyUrl = isset($config['cregis_callback_url']['value'])? $config['cregis_callback_url']['value'] : '';
  29. $notifyUrl = $notifyUrl? $notifyUrl : '/api/v1/wallet-api/cregisCallback';
  30. self::$config = [
  31. 'project_no' => isset($config['cregis_project_no']['value'])? $config['cregis_project_no']['value'] : '', //商户号
  32. 'api_key' => isset($config['cregis_api_key']['value'])? $config['cregis_api_key']['value'] : '', //apikey
  33. 'endpoint' => isset($config['cregis_endpoint']['value'])? $config['cregis_gateway_address']['value'] : '', //节点
  34. 'callUrl'=> url($notifyUrl,'',true), //回调地址
  35. 'debug' => false //调试模式
  36. ];
  37. $this->dispatch = new CregisDispatch(self::$config);
  38. }
  39. /**
  40. * @return static|null
  41. */
  42. public static function make(){
  43. if(!self::$instance){
  44. self::$instance = new static();
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 获取支持的币种
  50. * @param bool $showBalance 是否返回余额
  51. * @return mixed
  52. */
  53. public function supportCoins($showBalance=true)
  54. {
  55. return $this->dispatch->supportCoins($showBalance);
  56. }
  57. /**
  58. * 创建钱包地址
  59. * @param string $mainCoinType
  60. * @return mixed
  61. */
  62. public function createAddress($mainCoinType='195')
  63. {
  64. $result = $this->dispatch->createAddress(self::$config['project_no'],$mainCoinType);
  65. $code = isset($result['code'])? intval($result['code']) : 0;
  66. $address = isset($result['data']['address'])? trim($result['data']['address']) : '';
  67. if($code == 200 && $address){
  68. return $address;
  69. }else{
  70. RedisService::set("caches:cregisPay:createAddress:error_".date('YmdHis'),['mainCoinType'=>$mainCoinType,'result'=> $result], 7200);
  71. return '';
  72. }
  73. }
  74. /**
  75. * 验证钱包地址合法性
  76. * @param $address 地址
  77. * @param string $mainCoinType 主币
  78. * @return mixed
  79. */
  80. public function checkAddress($address,$mainCoinType='195')
  81. {
  82. return $this->dispatch->addressLegal(self::$config['project_no'],$mainCoinType,$address);
  83. }
  84. /**
  85. * 查询地址是否存在
  86. * @return mixed
  87. */
  88. public function existAddress($address,$mainCoinType='195')
  89. {
  90. return $this->dispatch->addressInner(self::$config['project_no'],$mainCoinType,$address);
  91. }
  92. /**
  93. * 提币
  94. * @return mixed
  95. */
  96. public function withdraw($address, $amount, $orderNo, $mainCoinType='195', $coinType='', $remark='')
  97. {
  98. $callback = '';
  99. $tokenAddress = ConfigService::make()->getConfigByCode('solana_usdt_token','Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB');
  100. $coinType = $coinType? $coinType : $tokenAddress;
  101. return $this->dispatch->payout(self::$config['project_no'],$mainCoinType.'@'.$coinType,$address,$amount,$callback,$orderNo, time(),$remark);
  102. }
  103. /**
  104. * 查询订单
  105. * @param $orderNo 交易单号
  106. * @return mixed
  107. */
  108. public function queryOrder($tradeOutNo)
  109. {
  110. return $this->dispatch->payoutQuery(self::$config['project_no'], $tradeOutNo);
  111. }
  112. /**
  113. * 充值回调
  114. * @return mixed
  115. */
  116. public function callback()
  117. {
  118. $result = $this->dispatch->changeBackUrl();
  119. RedisService::set("caches:cregisPay:recharge_callback",['params'=> request()->all(),'result'=> $result], 3600);
  120. return $result;
  121. }
  122. /**
  123. * 提现回调
  124. * @return mixed
  125. */
  126. public function withdrawCallBack() {
  127. $result = $this->dispatch->withdrawalBackUrl();
  128. RedisService::set("caches:cregisPay:withdraw_callback",['params'=> request()->all(),'result'=> $result], 3600);
  129. return $result;
  130. }
  131. /**
  132. * 回调处理
  133. * @param $params
  134. */
  135. public function catchNotify($params)
  136. {
  137. RedisService::set('caches:cregisPay:notify',$params);
  138. }
  139. }