1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digikraaft\StripePaymentsTile; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Carbon; |
7
|
|
|
use Stripe\StripeClient; |
8
|
|
|
|
9
|
|
|
class FetchPaymentsDataFromStripeApi extends Command |
10
|
|
|
{ |
11
|
|
|
protected $signature = 'dashboard:fetch-payments-data-from-stripe-api'; |
12
|
|
|
|
13
|
|
|
protected $description = 'Fetch data for Stripe payments tile'; |
14
|
|
|
|
15
|
|
|
public function handle() |
16
|
|
|
{ |
17
|
|
|
$stripe = new StripeClient( |
18
|
|
|
config('dashboard.tiles.stripe.secret_key', env('STRIPE_SECRET')) |
|
|
|
|
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
$this->info('Fetching Stripe payments ...'); |
22
|
|
|
|
23
|
|
|
$payments = $stripe->charges->all( |
24
|
|
|
config('dashboard.tiles.stripe.payments.params') ?? ['limit' => 5] |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
$payments = collect($payments->data) |
28
|
|
|
->map(function ($payment) { |
29
|
|
|
return [ |
30
|
|
|
'id' => $payment->id, |
31
|
|
|
'amount' => $this->toReadableAmount($payment->amount), |
32
|
|
|
'currency' => strtoupper($payment->currency), |
33
|
|
|
'customer' => $this->getCustomerEmailFromStripe($payment->customer), |
34
|
|
|
'status' => $payment->status, |
35
|
|
|
'captured' => $payment->captured, |
36
|
|
|
'createdAt' => Carbon::parse($payment->created) |
37
|
|
|
->format("d.m.Y"), |
38
|
|
|
]; |
39
|
|
|
})->toArray(); |
40
|
|
|
|
41
|
|
|
StripePaymentsStore::make()->setData($payments); |
42
|
|
|
|
43
|
|
|
$this->info('All done!'); |
44
|
|
|
|
45
|
|
|
return 0; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function toReadableAmount($baseAmount) |
49
|
|
|
{ |
50
|
|
|
return $baseAmount / 100; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|null $customer |
55
|
|
|
* @return string|null |
56
|
|
|
* @throws \Stripe\Exception\ApiErrorException |
57
|
|
|
*/ |
58
|
|
|
public function getCustomerEmailFromStripe(?string $customer = null): ?string |
59
|
|
|
{ |
60
|
|
|
if (! $customer) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
$stripe = new StripeClient( |
64
|
|
|
config('dashboard.tiles.stripe.secret_key', env('STRIPE_SECRET')) |
|
|
|
|
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $stripe->customers->retrieve( |
68
|
|
|
$customer, |
69
|
|
|
)->email; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|