message = $message; $this->time = $time; $this->user = $user; } /** * Execute the job. * * @return void */ public function handle(MBBank $bank) { if ($this->time < time()) { Event(new Notification_Expiry_Time('Hết thời gian chờ')); } else { $history = $bank->GetTransaction(); $status = 0; $message = ''; if ($history === false) { $login = $bank->Login(); $history = $bank->GetTransaction(); } if ($history) { foreach ($history as $item) { if (empty($item->tracingType) && str_contains($item['description'], $this->message)) { $transaction = str_replace("\\BNK", "", $item['refNo']); // firstOrCreate is atomic against the unique(customer_id, trans, type) index — // a duplicate bank-statement poll (job retried, matched twice) cannot credit twice. $created = Transaction::firstOrCreate( ['trans' => $transaction, 'customer_id' => $this->user], ['amount' => (int) $item['creditAmount']] ); if ($created->wasRecentlyCreated) { Customer::find($this->user)->increment('credit', (int)$item['creditAmount']); Customer::find($this->user)->increment('total', (int)$item['creditAmount']); $message = number_format($item['creditAmount'], 0, ',', '.') . "đ vào tài khoản"; $status = 1; break; } } } } if ($status === 1) { Event(new Notification_Pay_Success("+ $message", $this->user)); } else { dispatch(new GetTransactionPayment($this->message, $this->time, $this->user))->delay(now()->addSeconds(30)); } } } }