87 lines
3.0 KiB
PHP
87 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\Bank;
|
|
use App\Enums\WalletToBankLogStatus;
|
|
use App\Exceptions\YeePayException;
|
|
use App\Models\WalletToBankLog;
|
|
use App\Services\YeePayService;
|
|
use Illuminate\Console\Command;
|
|
use Throwable;
|
|
|
|
class WalletToBankCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'wallet-to-bank';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '可提提现';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$yeePayService = new YeePayService(config('services.yeepay'));
|
|
|
|
while (true) {
|
|
WalletToBankLog::where('status', WalletToBankLogStatus::Passed)->chunkById(1, function ($logs) use ($yeePayService) {
|
|
foreach ($logs as $log) {
|
|
try {
|
|
$result = $yeePayService->request('accountpay.behalf.Pay', [
|
|
'payerOutUserId' => '21102510220227100003' ?: config('services.yeepay.partner_id'),
|
|
'merchOrderNo' => $log->pay_sn,
|
|
'tradeName' => '余额提现',
|
|
'payeeUserName' => $log->username,
|
|
'bankCardNo' => $log->bank_number,
|
|
'bankCode' => Bank::tryFromBankName($log->bank_name)?->name,
|
|
'bankCardType' => 'DEBIT_CARD',
|
|
'amount' => bcdiv($log->account_amount, 100, 2),
|
|
'feeRole' => 'PAYER',
|
|
'tradeMemo' => '商城余额提现',
|
|
]);
|
|
|
|
if ($result['orderStatus'] === 'SUCCESS') {
|
|
$log->update([
|
|
'status' => WalletToBankLogStatus::Success,
|
|
'pay_at' => now(),
|
|
'failed_reason' => null,
|
|
]);
|
|
} elseif ($result['orderStatus'] === 'FAIL') {
|
|
$log->update([
|
|
'status' => WalletToBankLogStatus::Failed,
|
|
'failed_reason' => '交易失败',
|
|
]);
|
|
} else {
|
|
$log->update([
|
|
'status' => WalletToBankLogStatus::Paying,
|
|
'failed_reason' => null,
|
|
]);
|
|
}
|
|
} catch (YeePayException $e) {
|
|
$log->update([
|
|
'status' => WalletToBankLogStatus::Failed,
|
|
'failed_reason' => $e->getMessage(),
|
|
]);
|
|
} catch (Throwable $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
});
|
|
|
|
sleep(60);
|
|
}
|
|
}
|
|
}
|