FetchPaymentsDataFromStripeApi   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 31
c 5
b 0
f 0
dl 0
loc 61
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 31 1
A toReadableAmount() 0 3 1
A getCustomerEmailFromStripe() 0 12 2
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'))
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
            /** @scrutinizer ignore-call */ 
19
            config('dashboard.tiles.stripe.secret_key', env('STRIPE_SECRET'))
Loading history...
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'))
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            /** @scrutinizer ignore-call */ 
65
            config('dashboard.tiles.stripe.secret_key', env('STRIPE_SECRET'))
Loading history...
65
        );
66
67
        return $stripe->customers->retrieve(
68
            $customer,
69
        )->email;
70
    }
71
}
72