This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
/
FetchTransactionUpdateJob.php
108 lines (95 loc) · 3.24 KB
/
FetchTransactionUpdateJob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace GloCurrency\AccessBank\Jobs;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\App;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Bus\Queueable;
use GloCurrency\MiddlewareBlocks\Enums\QueueTypeEnum as MQueueTypeEnum;
use GloCurrency\AccessBank\Models\Transaction;
use GloCurrency\AccessBank\Exceptions\SendTransactionException;
use GloCurrency\AccessBank\Exceptions\FetchTransactionUpdateException;
use GloCurrency\AccessBank\Enums\TransactionStateCodeEnum;
use BrokeYourBike\AccessBank\Enums\StatusCodeEnum;
use BrokeYourBike\AccessBank\Enums\ErrorCodeEnum;
use BrokeYourBike\AccessBank\Client;
class FetchTransactionUpdateJob implements ShouldQueue, ShouldBeUnique, ShouldBeEncrypted
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 1;
private Transaction $targetTransaction;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Transaction $targetTransaction)
{
$this->targetTransaction = $targetTransaction;
$this->afterCommit();
$this->onQueue(MQueueTypeEnum::SERVICES->value);
}
/**
* The unique ID of the job.
*
* @return string
*/
public function uniqueId()
{
return $this->targetTransaction->id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (TransactionStateCodeEnum::PROCESSING !== $this->targetTransaction->state_code) {
throw FetchTransactionUpdateException::stateNotAllowed($this->targetTransaction);
}
try {
/** @var Client */
$api = App::makeWith(Client::class);
if (in_array($this->targetTransaction->getBankCode(), ['044', '063'])) {
$response = $api->fetchDomesticTransactionStatus((string) Str::uuid(), $this->targetTransaction->getReference());
} else {
$response = $api->fetchOtheBankTransactionStatus((string) Str::uuid(), $this->targetTransaction->getReference());
}
} catch (\Throwable $e) {
report($e);
throw FetchTransactionUpdateException::apiRequestException($e);
}
$errorCode = ErrorCodeEnum::tryFrom($response->errorCode);
if (ErrorCodeEnum::NO_ERROR === $errorCode) {
$statusCode = StatusCodeEnum::tryFrom($response->transactionStatus);
if ($statusCode) {
$this->targetTransaction->state_code = TransactionStateCodeEnum::makeFromStatusCode($statusCode);
$this->targetTransaction->state_code_reason = $response->transactionInformation;
}
$this->targetTransaction->save();
}
}
/**
* Handle a job failure.
*
* @param \Throwable $exception
* @return void
*/
public function failed(\Throwable $exception)
{
report($exception);
}
}