jingcai-php/app/Jobs/HandleAgentAlipayResultNoti...

106 lines
3.8 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\PayState;
use App\Model\AliAgentNotice;
use App\Model\Customer\CustomerRecharge;
use App\Service\CustomerWalletService;
use App\Service\OrderService;
use App\Utils\Helps;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class HandleAgentAlipayResultNotice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
public function __construct($data)
{
$this->data = $data;
$this->queue = config('queue.names.recharge');
}
public function getData()
{
return $this->data;
}
public function handle()
{
$tradeStatus = Arr::get($this->data, 'trade_status');
if ($tradeStatus != 'TRADE_SUCCESS') {
return;
}
if (!isset($this->data['notify_id'])) {
Log::error('HandleAgentAlipayResultNotice 没有notify_id', $this->data);
return;
}
$aliNotice = AliAgentNotice::where('notify_id', $this->data['notify_id'])->first();
if ($aliNotice) {
Log::error('HandleAgentAlipayResultNotice noticeId exist', $this->data);
return;
}
$aliNotice = new AliAgentNotice();
$aliNotice->notify_id = Arr::get($this->data, 'notify_id', '');
$aliNotice->out_trade_no = Arr::get($this->data, 'out_trade_no', '');
$aliNotice->trade_no = Arr::get($this->data, 'trade_no', '');
$aliNotice->trade_status = Arr::get($this->data, 'trade_status', '');
$aliNotice->receipt_amount = Arr::get($this->data, 'receipt_amount', 0);
$aliNotice->buyer_pay_amount = Arr::get($this->data, 'buyer_pay_amount', '');
$aliNotice->total_amount = Arr::get($this->data, 'total_amount', 0);
$aliNotice->buyer_id = Arr::get($this->data, 'buyer_id', '');
$aliNotice->auth_app_id = Arr::get($this->data, 'auth_app_id', '');
$aliNotice->app_id = Arr::get($this->data, 'app_id', '');
$aliNotice->seller_email = Arr::get($this->data, 'seller_email', '');
$aliNotice->gmt_create = Arr::get($this->data, 'gmt_create');
$aliNotice->response = $this->data;
$aliNotice->save();
$rechargeSn = $this->data['out_trade_no'];
$rechargeObj = new CustomerRecharge();
$recharge = $rechargeObj->where('recharge_sn', $rechargeSn)->first();
if (!$recharge) {
Log::error('HandleAgentAlipayResultNotice 表中充值单号', [
'data' => $this->data,
'rechargeSn' => $rechargeSn,
]);
return;
}
if ($aliNotice->trade_status == 'TRADE_SUCCESS') {
$recharge->pay_state = PayState::SUCCESS;
if (!bc_equal(Helps::floatFormat($recharge->pay_money), Helps::floatFormat($this->data['receipt_amount']))) {
$recharge->pay_state = PayState::UNEQUAL;
}
} else {
$recharge->pay_state = PayState::ERROR;
}
$recharge->out_trade_no = $this->data['trade_no'];
$recharge->received_money = $this->data['receipt_amount'];
$recharge->pay_at = $this->data['gmt_create'];
$recharge->save();
if ($recharge->pay_state == PayState::SUCCESS) {
CustomerWalletService::recharge($recharge);
// 如果有订单,则处理订单支付状态
if ($recharge->order_id > 0) {
/** @var OrderService $orderService */
$orderService = app(OrderService::class);
$orderService->setOrderPaySuccess($recharge->order);
}
}
}
}