Skip to content

Commit 2793dee

Browse files
authored
Add command (#3)
* Add command to clean pending transactions.
1 parent 0d99ad6 commit 2793dee

File tree

9 files changed

+135
-404
lines changed

9 files changed

+135
-404
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- [Webhooks Events]()
3232
- [Verify webhook signature manually](#Verify-webhook-signature)
3333
- [Validate webhook signature manually](#validate-webhook-signature)
34+
- [Commands](#commands)
3435
- [Testing and test cards](#Testing-and-test-cards)
3536

3637
## Introduction
@@ -565,6 +566,16 @@ class YouCanPayWebhooksController extends Controller
565566
}
566567
```
567568

569+
### Commands
570+
571+
If you need to clean the pending transactions, there's a command
572+
573+
```shell
574+
php artisan youcanpay:clean-pending-transactions
575+
```
576+
577+
You can add it to the scheduler that can run the command every single time, so the clean will be automatic and depend on the `tolerance` value that is defined in the `youcanpay` config file.
578+
568579
## Testing and test cards
569580

570581
| **CARD** | **CVV** | **DATE** | **BEHAVIOUR** |
@@ -604,7 +615,3 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
604615
## Laravel Package Boilerplate
605616

606617
This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).
607-
608-
```
609-
610-
```

config/youcanpay.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515

1616
"success_redirect_uri" => env("SUCCCESS_REDIRECT_URI"),
1717

18-
"fail_redirect_uri" => env("FAIL_REDIRECT_URI")
18+
"fail_redirect_uri" => env("FAIL_REDIRECT_URI"),
19+
20+
/*
21+
* The tolerance value at which to look to the old pending transactions,
22+
* and remove them from the database.
23+
* By default it's set to 48 hours
24+
*/
25+
"transaction" => [
26+
"tolerance" => env('TRANSACTION_TOLERANCE', 60 * 60 * 48),
27+
]
1928

2029
];

database/factories/TransactionFactory.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,19 @@ public function definition()
3737
'payload'=> []
3838
];
3939
}
40+
41+
42+
/**
43+
* Indicate that the transaction is pending.
44+
*
45+
* @return \Illuminate\Database\Eloquent\Factories\Factory
46+
*/
47+
public function pending()
48+
{
49+
return $this->state(function (array $attributes) {
50+
return [
51+
'status' => YouCanPayStatus::pending(),
52+
];
53+
});
54+
}
4055
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
namespace Devinweb\LaravelYouCanPay\Console;
3+
4+
use Devinweb\LaravelYouCanPay\Enums\YouCanPayStatus;
5+
use Devinweb\LaravelYouCanPay\Models\Transaction;
6+
use Illuminate\Console\Command;
7+
use Carbon\Carbon;
8+
9+
class CleanPendingTransactionCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'youcanpay:clean-pending-transactions';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Clean all pending transactions based on the tolerance value get it from the youcanpay config file.';
24+
25+
/**
26+
* Execute the console command.
27+
*
28+
* @return void
29+
*/
30+
public function handle()
31+
{
32+
$tolerance = config('youcanpay.transaction.tolerance');
33+
34+
$transactions = Transaction::whereStatus(YouCanPayStatus::pending())
35+
->where('created_at', '<=', Carbon::now())
36+
->where('created_at', '>=', Carbon::now()->subSeconds($tolerance ?? 60*60*24))
37+
->get();
38+
39+
foreach ($transactions as $transaction) {
40+
$transaction->delete();
41+
}
42+
}
43+
}

src/Facades/LaravelYouCanPay.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
use Illuminate\Support\Facades\Facade;
66

7-
/**
8-
* @see \Devinweb\LaravelYouCanPay\Skeleton\SkeletonClass
9-
*/
107
class LaravelYouCanPay extends Facade
118
{
129
/**

0 commit comments

Comments
 (0)