// +---------------------------------------------------------------------- namespace App\Services; use Cregis\Dispatch\CregisDispatch; /** * Cregis钱包服务管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services */ class CregisPayService extends BaseService { // 静态对象 protected static $instance = null; protected $dispatch = null; protected static $config = []; public function __construct() { $config = ConfigService::make()->getConfigByGroup(10); $notifyUrl = isset($config['cregis_callback_url']['value'])? $config['cregis_callback_url']['value'] : ''; $notifyUrl = $notifyUrl? $notifyUrl : '/api/v1/wallet-api/cregisCallback'; self::$config = [ 'project_no' => isset($config['cregis_project_no']['value'])? $config['cregis_project_no']['value'] : '', //商户号 'api_key' => isset($config['cregis_api_key']['value'])? $config['cregis_api_key']['value'] : '', //apikey 'endpoint' => isset($config['cregis_endpoint']['value'])? $config['cregis_gateway_address']['value'] : '', //节点 'callUrl'=> url($notifyUrl,'',true), //回调地址 'debug' => false //调试模式 ]; $this->dispatch = new CregisDispatch(self::$config); } /** * @return static|null */ public static function make(){ if(!self::$instance){ self::$instance = new static(); } return self::$instance; } /** * 获取支持的币种 * @param bool $showBalance 是否返回余额 * @return mixed */ public function supportCoins($showBalance=true) { return $this->dispatch->supportCoins($showBalance); } /** * 创建钱包地址 * @param string $mainCoinType * @return mixed */ public function createAddress($mainCoinType='195') { $result = $this->dispatch->createAddress(self::$config['project_no'],$mainCoinType); $code = isset($result['code'])? intval($result['code']) : 0; $address = isset($result['data']['address'])? trim($result['data']['address']) : ''; if($code == 200 && $address){ return $address; }else{ RedisService::set("caches:cregisPay:createAddress:error_".date('YmdHis'),['mainCoinType'=>$mainCoinType,'result'=> $result], 7200); return ''; } } /** * 验证钱包地址合法性 * @param $address 地址 * @param string $mainCoinType 主币 * @return mixed */ public function checkAddress($address,$mainCoinType='195') { return $this->dispatch->addressLegal(self::$config['project_no'],$mainCoinType,$address); } /** * 查询地址是否存在 * @return mixed */ public function existAddress($address,$mainCoinType='195') { return $this->dispatch->addressInner(self::$config['project_no'],$mainCoinType,$address); } /** * 提币 * @return mixed */ public function withdraw($address, $amount, $orderNo, $mainCoinType='195', $coinType='', $remark='') { $callback = ''; $tokenAddress = ConfigService::make()->getConfigByCode('solana_usdt_token','Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB'); $coinType = $coinType? $coinType : $tokenAddress; return $this->dispatch->payout(self::$config['project_no'],$mainCoinType.'@'.$coinType,$address,$amount,$callback,$orderNo, time(),$remark); } /** * 查询订单 * @param $orderNo 交易单号 * @return mixed */ public function queryOrder($tradeOutNo) { return $this->dispatch->payoutQuery(self::$config['project_no'], $tradeOutNo); } /** * 充值回调 * @return mixed */ public function callback() { $result = $this->dispatch->changeBackUrl(); RedisService::set("caches:cregisPay:recharge_callback",['params'=> request()->all(),'result'=> $result], 3600); return $result; } /** * 提现回调 * @return mixed */ public function withdrawCallBack() { $result = $this->dispatch->withdrawalBackUrl(); RedisService::set("caches:cregisPay:withdraw_callback",['params'=> request()->all(),'result'=> $result], 3600); return $result; } /** * 回调处理 * @param $params */ public function catchNotify($params) { RedisService::set('caches:cregisPay:notify',$params); } }