Skip to content

Commit d776fe0

Browse files
Merge pull request #101 from cashier-provider/3.x
Disabled logging of failed jobs when checking payments
2 parents e1d6684 + 43f0650 commit d776fe0

File tree

4 files changed

+102
-53
lines changed

4 files changed

+102
-53
lines changed

src/Jobs/Base.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use CashierProvider\Core\Facades\Config\Main;
2626
use CashierProvider\Core\Facades\Config\Payment;
2727
use CashierProvider\Core\Facades\Helpers\Model as ModelHelper;
28+
use Closure;
2829
use DragonCode\Contracts\Cashier\Driver;
2930
use DragonCode\Contracts\Cashier\Helpers\Statuses;
3031
use DragonCode\Contracts\Cashier\Http\Response;
@@ -35,6 +36,8 @@
3536
use Illuminate\Database\Eloquent\Model;
3637
use Illuminate\Queue\InteractsWithQueue;
3738
use Illuminate\Queue\SerializesModels;
39+
use Illuminate\Support\Carbon;
40+
use Throwable;
3841

3942
abstract class Base implements ShouldQueue, ShouldBeUnique
4043
{
@@ -59,11 +62,7 @@ abstract class Base implements ShouldQueue, ShouldBeUnique
5962

6063
protected $event;
6164

62-
abstract public function handle();
63-
64-
abstract protected function process(): Response;
65-
66-
abstract protected function queueName(): ?string;
65+
protected $doneInsteadThrow = false;
6766

6867
public function __construct(Model $model, bool $force_break = false)
6968
{
@@ -78,6 +77,12 @@ public function __construct(Model $model, bool $force_break = false)
7877
$this->queue = $this->queueName();
7978
}
8079

80+
abstract public function handle();
81+
82+
abstract protected function process(): Response;
83+
84+
abstract protected function queueName(): ?string;
85+
8186
public function uniqueId()
8287
{
8388
return $this->model->getKey();
@@ -88,6 +93,11 @@ public function uniqueFor(): int
8893
return Main::getQueue()->getUnique()->getSeconds();
8994
}
9095

96+
public function retryUntil(): ?Carbon
97+
{
98+
return null;
99+
}
100+
91101
protected function hasBreak(): bool
92102
{
93103
return $this->force_break;
@@ -191,4 +201,35 @@ protected function resolveStatuses(): Statuses
191201
{
192202
return $this->resolveDriver()->statuses();
193203
}
204+
205+
protected function call(Closure $callback): void
206+
{
207+
try {
208+
$callback();
209+
}
210+
catch (Throwable $e) {
211+
if (! $this->doneInsteadThrow) {
212+
throw $e;
213+
}
214+
215+
if ($this->retryUntil() && $this->retryUntil() <= Carbon::now()) {
216+
$this->delete();
217+
218+
return;
219+
}
220+
221+
if (! $this->retryUntil() && $this->maxTries() > 0 && $this->attempts() >= $this->maxTries()) {
222+
$this->delete();
223+
224+
return;
225+
}
226+
227+
throw $e;
228+
}
229+
}
230+
231+
protected function maxTries(): int
232+
{
233+
return $this->job->maxTries() ?: config('cashier.queue.tries', 100);
234+
}
194235
}

src/Jobs/Check.php

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,59 @@ class Check extends Base
3030
{
3131
protected $event = Checked::class;
3232

33+
protected $doneInsteadThrow = true;
34+
3335
public function handle()
3436
{
35-
$this->checkExternalId();
37+
$this->call(function () {
38+
$this->checkExternalId();
3639

37-
$response = $this->process();
40+
$response = $this->process();
3841

39-
$status = $response->getStatus();
42+
$status = $response->getStatus();
4043

41-
switch (true) {
42-
case $this->hasFailed($status):
43-
$this->update($response, Status::FAILED);
44-
break;
44+
switch (true) {
45+
case $this->hasFailed($status):
46+
$this->update($response, Status::FAILED);
47+
break;
4548

46-
case $this->hasRefunding($status):
47-
$this->update($response, Status::WAIT_REFUND);
48-
break;
49+
case $this->hasRefunding($status):
50+
$this->update($response, Status::WAIT_REFUND);
51+
break;
4952

50-
case $this->hasRefunded($status):
51-
$this->update($response, Status::REFUND);
52-
break;
53+
case $this->hasRefunded($status):
54+
$this->update($response, Status::REFUND);
55+
break;
5356

54-
case $this->hasSuccess($status):
55-
$this->update($response, Status::SUCCESS);
56-
break;
57+
case $this->hasSuccess($status):
58+
$this->update($response, Status::SUCCESS);
59+
break;
5760

58-
default:
59-
if ($this->hasBreak()) {
60-
return;
61-
}
61+
default:
62+
if ($this->hasBreak()) {
63+
return;
64+
}
6265

63-
$this->returnToQueue();
64-
}
66+
$this->returnToQueue();
67+
}
68+
});
6569
}
6670

67-
public function retryUntil(): Carbon
71+
protected function process(): Response
6872
{
69-
$timeout = Main::getCheckTimeout();
73+
return $this->resolveDriver()->check();
74+
}
7075

71-
return Carbon::now()->addSeconds($timeout);
76+
protected function queueName(): ?string
77+
{
78+
return $this->resolveDriver()->queue()->getCheck();
7279
}
7380

74-
protected function process(): Response
81+
public function retryUntil(): ?Carbon
7582
{
76-
return $this->resolveDriver()->check();
83+
$timeout = Main::getCheckTimeout();
84+
85+
return Carbon::now()->addSeconds($timeout);
7786
}
7887

7988
protected function update(Response $response, string $status): void
@@ -82,11 +91,6 @@ protected function update(Response $response, string $status): void
8291
$this->store($response, false);
8392
}
8493

85-
protected function queueName(): ?string
86-
{
87-
return $this->resolveDriver()->queue()->getCheck();
88-
}
89-
9094
protected function checkExternalId(): void
9195
{
9296
$this->resolveCashier($this->model);

src/Jobs/Refund.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,25 @@ class Refund extends Base
3232

3333
public function handle()
3434
{
35-
$this->checkExternalId();
35+
$this->call(function () {
36+
$this->checkExternalId();
3637

37-
$this->runCheckJob();
38+
$this->runCheckJob();
3839

39-
$this->checkStatus();
40+
$this->checkStatus();
4041

41-
$this->ran();
42+
$this->ran();
43+
});
44+
}
45+
46+
protected function process(): Response
47+
{
48+
return $this->resolveDriver()->refund();
49+
}
50+
51+
protected function queueName(): ?string
52+
{
53+
return $this->resolveDriver()->queue()->getRefund();
4254
}
4355

4456
protected function ran()
@@ -58,11 +70,6 @@ protected function ran()
5870
$this->store($response, false);
5971
}
6072

61-
protected function process(): Response
62-
{
63-
return $this->resolveDriver()->refund();
64-
}
65-
6673
protected function paymentId()
6774
{
6875
return $this->model->getKey();
@@ -94,9 +101,4 @@ protected function checkStatus(): void
94101
);
95102
}
96103
}
97-
98-
protected function queueName(): ?string
99-
{
100-
return $this->resolveDriver()->queue()->getRefund();
101-
}
102104
}

src/Jobs/Start.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ class Start extends Base
2828

2929
public function handle()
3030
{
31-
$data = $this->process();
31+
$this->call(function () {
32+
$data = $this->process();
3233

33-
$this->store($data);
34+
$this->store($data);
35+
});
3436
}
3537

3638
protected function process(): Response

0 commit comments

Comments
 (0)