sifangpay/app/Console/Commands/UsdtAddrGetList.php

183 lines
4.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use App\Models\Order;
use App\Models\UsdtAddr;
use App\ServicePay\PayApiProvidesTrait;
use App\ServicePay\UsdtWalletServices;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Ixudra\Curl\Facades\Curl;
class UsdtAddrGetList extends Command
{
use PayApiProvidesTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:usdt_addr_get_list';
/**
* The console command description.
*
* @var string
*/
protected $description = '获得usdt地址列表';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* The name and signature of the console command.
*
* @var UsdtWalletServices
*/
private $pay_service;
private $pay_gate;
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->checkNeedUpdateAddr();
$this->updateTimeoutAddr();
}
/**
* 请求ustd的地址
* @return void
*
*/
private function checkNeedUpdateAddr(){
$this->setPayService('1017');
if (is_null($this->pay_service)) {
Log::error('通道不存在或支付类型错误 1017');
return;
}
$this->pay_gate = $this->pay_service->getGateway("1017");
if (is_null($this->pay_gate)) {
Log::error('通道不存在 1017');
return;
}
$this->pay_service->payConfig('1007');
//查找地址数据库
$addr_list = UsdtAddr::all();
if(count($addr_list) > 0){
Log::info(count($addr_list));
return;
}
//没有地址数据,请求接口更新数据
$addr = $this->reqAddrList();
if(empty($addr)){
Log::error("没有获得地址数据");
return;
}
//写入数据库
foreach ($addr as $key => $value) {
$data['addr'] = $value;
$data['ustd_type'] = "trc";
$this->insertAddr($data);
}
return;
}
private function reqAddrList(){
$createUrl='http://api.etwallet.net/wallet'.'/v1/api/address/batch';
//商户号
$accountId = $this->pay_service->pay_config['app_id'];
//商户秘钥
$key = $this->pay_service->pay_config['token'];
$payData = [
'num' => 10000,//请求钱包地址数量
'nonce' => strval(mt_rand(10000,99999)),//随机数
'merchantId' => $accountId, //商户id
'timestamp' => get_total_millisecond(),// //异步回调地址
];
$sign= $this->pay_service->sign($payData,$key,[]);
$payData['sign'] = $sign;
$res = Curl::to($createUrl)->withData($payData)->post();
// Log::info('下单接口请求内容' . json_encode($payData, 256), []);
// Log::info('返回内容' . json_encode($res, 256), []);
$res = is_array($res) ? $res : json_decode($res, true);
if (is_array($res) && isset($res['code']) && $res['code'] == 0) {
$data = $res['data'];
//$data = json_decode($data, true);
return explode(',',$data['addressList']);
} else {
$msg = $res['msg'] ?? '未知错误';
Log::info('错误' . $msg, []);
}
return null;
}
private function insertAddr($addr)
{
DB::beginTransaction();
$addrModel = new UsdtAddr();
foreach ($addr as $k => $v) {
$addrModel->$k = $v;
}
$r = $addrModel->save();
if ($r) {
DB::commit();
return 1;
} else {
DB::rollBack();
return false;
}
}
/**
* 从订单表中查找所有超期的订单回收usdt地址
* @return void
*/
public function updateTimeoutAddr(){
$allOrder = Order::where(['pay_type'=>'1017','usdt_addr_status'=>'0'])
->where('order_at','<',date('Y-m-d H:i:s',strtotime("-20 minute")))->limit(100)->get();
if(empty($allOrder)){
return;
}
$allOrder = $allOrder->toArray();
if(!is_array($allOrder)){
return;
}
//查询所有的订单地址
foreach ($allOrder as $key => $value) {
$addr = $value["usdt_addr"];
if (!empty($addr)){
try {
Order::where("id",$value['id'])->firstOrFail()->update(['usdt_addr_status' => 1]);
UsdtAddr::where("addr",$addr)->firstOrFail()->update(['is_checked' => 1]);
}catch(Exception $exception){
Log::error('失败异常内容:' . $exception->getMessage());
}
}
}
}
}