Passed
Push — main ( a4f65d...034b57 )
by Michael
04:14
created

StripePaymentProvider::charge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\StripeIntegration;
6
7
use Illuminate\Support\Traits\Conditionable;
8
use Illuminate\Support\Traits\Macroable;
9
use Laravel\Cashier\Exceptions\IncompletePayment;
10
use Laravel\Cashier\Payment;
11
use MichaelRubel\StripeIntegration\Behaviors\ConfiguresCashier;
12
use MichaelRubel\StripeIntegration\Behaviors\ManagesPaymentIntents;
13
use MichaelRubel\StripeIntegration\Behaviors\PreparesPaymentMethods;
14
use MichaelRubel\StripeIntegration\DataTransferObjects\OffsessionChargeData;
15
use MichaelRubel\StripeIntegration\DataTransferObjects\PaymentIntentData;
16
use MichaelRubel\StripeIntegration\DataTransferObjects\StripeChargeData;
17
use Stripe\Exception\ApiErrorException;
18
use Stripe\PaymentIntent;
19
use Stripe\StripeClient;
20
21
class StripePaymentProvider
22
{
23
    use PreparesPaymentMethods,
0 ignored issues
show
introduced by
The trait MichaelRubel\StripeInteg...\PreparesPaymentMethods requires some properties which are not provided by MichaelRubel\StripeInteg...n\StripePaymentProvider: $paymentMethod, $params, $paymentMethods, $options, $id, $customer
Loading history...
introduced by
The trait MichaelRubel\StripeInteg...s\ManagesPaymentIntents requires some properties which are not provided by MichaelRubel\StripeInteg...n\StripePaymentProvider: $model, $paymentIntents, $params, $paymentAmount, $options, $id, $paymentIntent, $intentId
Loading history...
24
        ManagesPaymentIntents,
25
        ConfiguresCashier;
26
27
    /**
28
     * @extendability
29
     */
30
    use Macroable, Conditionable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by MichaelRubel\StripeInteg...n\StripePaymentProvider.
Loading history...
31
32
    /**
33
     * @param  StripeClient  $stripeClient
34
     *
35
     * @return void
36
     */
37 16
    public function __construct(public StripeClient $stripeClient)
38
    {
39
        //
40 16
    }
41
42
    /**
43
     * Perform a simple charge.
44
     *
45
     * @param  StripeChargeData  $data
46
     *
47
     * @return Payment
48
     * @throws IncompletePayment
49
     */
50 3
    public function charge(StripeChargeData $data): Payment
51
    {
52 3
        return call($data->model)->charge(
53 3
            $data->payment_amount->getAmount(),
54 3
            $data->payment_method->id,
55 3
            $data->options,
56 3
        );
57
    }
58
59
    /**
60
     * Perform off-session charge.
61
     *
62
     * @param  OffsessionChargeData  $data
63
     *
64
     * @return PaymentIntent
65
     * @throws ApiErrorException
66
     */
67 1
    public function offsessionCharge(OffsessionChargeData $data): PaymentIntent
68
    {
69 1
        $paymentIntent = $this->createPaymentIntent(new PaymentIntentData(
70 1
            paymentAmount: $data->payment_amount,
71 1
            model: $data->model,
72 1
            params: $data->intent_params,
73 1
            options: $data->intent_options,
74 1
        ));
75
76 1
        $confirmationParams = collect(['payment_method' => call($data->model)->defaultPaymentMethod()->id])
0 ignored issues
show
Bug introduced by
array('payment_method' =...ultPaymentMethod()->id) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

76
        $confirmationParams = collect(/** @scrutinizer ignore-type */ ['payment_method' => call($data->model)->defaultPaymentMethod()->id])
Loading history...
77 1
            ->merge($data->confirmation_params)
0 ignored issues
show
Bug introduced by
$data->confirmation_params of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::merge(). ( Ignorable by Annotation )

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

77
            ->merge(/** @scrutinizer ignore-type */ $data->confirmation_params)
Loading history...
78 1
            ->toArray();
79
80 1
        return $this->confirmPaymentIntent(new PaymentIntentData(
81 1
            paymentIntent: $paymentIntent,
82 1
            params: $confirmationParams,
83 1
            options: $data->confirmation_options,
84 1
        ));
85
    }
86
}
87