jingcai-php/app/Jobs/HandleAlipayResultNotice.php

100 lines
3.5 KiB
PHP
Executable File

<?php
namespace App\Jobs;
use App\Enums\PayState;
use App\Enums\RechargeSnType;
use App\Model\AliNotice;
use App\Model\Seller\ShopRecharge;
use App\Service\CustomerWalletService;
use App\Service\OrderService;
use App\Service\SellerWalletService;
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 HandleAlipayResultNotice 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('HandleAlipayResultNotice not found notify_id', [$this->data]);
return;
}
$aliNotice = AliNotice::where('notify_id', $this->data['notify_id'])->first();
if ($aliNotice) {
Log::error('HandleAlipayResultNotice noticeId exist', $this->data);
return;
}
$aliNotice = new AliNotice();
$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 ShopRecharge();
$recharge = $rechargeObj->where('recharge_sn', $rechargeSn)->first();
if (!$recharge) {
Log::error('HandleAlipayResultNotice 表中充值单号', [
'data' => $this->data,
'rechargeSn' => $rechargeSn,
]);
return;
}
if ($aliNotice->trade_status == 'TRADE_SUCCESS') {
$recharge->pay_state = PayState::SUCCESS;
if (!bc_equal($recharge->pay_money, $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) {
SellerWalletService::recharge($recharge);
}
}
}