Passed
Push — main ( 2f3a7d...59d393 )
by Michael
12:42
created

StripePaymentProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 42
dl 0
loc 161
ccs 42
cts 42
cp 1
rs 10
c 5
b 1
f 1
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A charge() 0 6 1
A attachPaymentMethodToCustomer() 0 14 1
A configureCashierCurrency() 0 4 1
A createPaymentIntent() 0 7 1
A __construct() 0 3 1
A prepareCustomer() 0 3 1
A setupIntentUsing() 0 7 1
A updatePaymentMethod() 0 3 1
A offsessionCharge() 0 22 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\StripeIntegration\Providers;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Traits\Macroable;
9
use Laravel\Cashier\Exceptions\IncompletePayment;
10
use Laravel\Cashier\Payment;
11
use Laravel\Cashier\PaymentMethod as CashierPaymentMethod;
12
use MichaelRubel\StripeIntegration\DataTransferObjects\OffsessionChargeData;
13
use MichaelRubel\StripeIntegration\DataTransferObjects\StripeChargeData;
14
use MichaelRubel\StripeIntegration\Decorators\StripePaymentAmount;
15
use MichaelRubel\StripeIntegration\Providers\Contracts\PaymentProviderContract;
16
use Money\Currency;
17
use Stripe\Customer;
18
use Stripe\Exception\ApiErrorException;
19
use Stripe\PaymentIntent;
20
use Stripe\PaymentMethod;
21
use Stripe\SetupIntent;
22
use Stripe\StripeClient;
23
24
class StripePaymentProvider implements PaymentProviderContract
25
{
26
    use Macroable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by MichaelRubel\StripeInteg...s\StripePaymentProvider.
Loading history...
27
28
    /**
29
     * @return void
30
     */
31 11
    public function __construct(
32
        protected StripeClient $stripeClient
33
    ) {
34
    }
35
36
    /**
37
     * Set the Cashier's currency.
38
     *
39
     * @param Currency $currency
40
     *
41
     * @return void
42
     */
43 1
    public function configureCashierCurrency(Currency $currency): void
44
    {
45 1
        config([
46 1
            'cashier.currency' => $currency->getCode(),
47
        ]);
48
    }
49
50
    /**
51
     * Create the Stripe setup intent.
52
     *
53
     * @param Model $model
54
     * @param array $options
55
     *
56
     * @return SetupIntent
57
     */
58 1
    public function setupIntentUsing(Model $model, array $options = []): SetupIntent
59
    {
60 1
        $options = collect([
0 ignored issues
show
Bug introduced by
array('usage' => 'off_session') of type array<string,string> 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

60
        $options = collect(/** @scrutinizer ignore-type */ [
Loading history...
61
            'usage' => 'off_session',
62 1
        ])->merge($options)->toArray();
0 ignored issues
show
Bug introduced by
$options 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

62
        ])->merge(/** @scrutinizer ignore-type */ $options)->toArray();
Loading history...
63
64 1
        return call($model)->createSetupIntent($options);
65
    }
66
67
    /**
68
     * Prepare the customer to work with the payment system.
69
     *
70
     * @param Model $model
71
     *
72
     * @return Customer
73
     */
74 3
    public function prepareCustomer(Model $model): Customer
75
    {
76 3
        return call($model)->createOrGetStripeCustomer();
77
    }
78
79
    /**
80
     * Updates the default payment method for model.
81
     *
82
     * @param Model                $model
83
     * @param PaymentMethod|string $paymentMethod
84
     *
85
     * @return CashierPaymentMethod
86
     */
87 3
    public function updatePaymentMethod(Model $model, PaymentMethod|string $paymentMethod): CashierPaymentMethod
88
    {
89 3
        return call($model)->updateDefaultPaymentMethod($paymentMethod);
90
    }
91
92
    /**
93
     * Attaches the payment method to the customer.
94
     *
95
     * @param PaymentMethod|CashierPaymentMethod $paymentMethod
96
     * @param Customer                           $customer
97
     * @param array                              $params
98
     * @param array                              $options
99
     *
100
     * @return PaymentMethod
101
     */
102 3
    public function attachPaymentMethodToCustomer(
103
        PaymentMethod|CashierPaymentMethod $paymentMethod,
104
        Customer $customer,
105
        array $params = [],
106
        array $options = []
107
    ): PaymentMethod {
108 3
        $params = collect([
0 ignored issues
show
Bug introduced by
array('customer' => $customer->id) of type array<string,string> 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

108
        $params = collect(/** @scrutinizer ignore-type */ [
Loading history...
109 3
            'customer' => $customer->id,
110 3
        ])->merge($params)->toArray();
0 ignored issues
show
Bug introduced by
$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

110
        ])->merge(/** @scrutinizer ignore-type */ $params)->toArray();
Loading history...
111
112 3
        return call($this->stripeClient->paymentMethods)->attach(
113 3
            $paymentMethod->id,
114
            $params,
115
            $options
116
        );
117
    }
118
119
    /**
120
     * Creates a payment intent.
121
     *
122
     * @param StripePaymentAmount $paymentAmount
123
     * @param Model               $model
124
     *
125
     * @return PaymentIntent
126
     * @throws ApiErrorException
127
     */
128 1
    public function createPaymentIntent(StripePaymentAmount $paymentAmount, Model $model): PaymentIntent
129
    {
130 1
        return call($this->stripeClient->paymentIntents)->create([
131 1
            'amount'               => $paymentAmount->getAmount(),
132 1
            'currency'             => $paymentAmount->getCurrency()->getCode(),
133 1
            'customer'             => $model->stripe_id,
134
            'payment_method_types' => ['card'],
135
        ]);
136
    }
137
138
    /**
139
     * Simple charge.
140
     *
141
     * @param StripeChargeData $data
142
     *
143
     * @return Payment
144
     * @throws IncompletePayment
145
     */
146 1
    public function charge(StripeChargeData $data): Payment
147
    {
148 1
        return call($data->model)->charge(
149 1
            $data->payment_amount->getAmount(),
150 1
            $data->payment_method->id,
151 1
            $data->options
152
        );
153
    }
154
155
    /**
156
     * Offsession charge.
157
     *
158
     * @param OffsessionChargeData $data
159
     *
160
     * @return PaymentIntent
161
     * @throws ApiErrorException
162
     */
163 1
    public function offsessionCharge(OffsessionChargeData $data): PaymentIntent
164
    {
165 1
        $intent_params = collect([
166 1
            'amount'               => $data->payment_amount->getAmount(),
167 1
            'currency'             => $data->payment_amount->getCurrency()->getCode(),
168 1
            'customer'             => $data->model->stripe_id,
169
            'payment_method_types' => ['card'],
170 1
        ])->merge($data->intent_params)->toArray();
0 ignored issues
show
Bug introduced by
$data->intent_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

170
        ])->merge(/** @scrutinizer ignore-type */ $data->intent_params)->toArray();
Loading history...
171
172 1
        $paymentIntent = call($this->stripeClient->paymentIntents)->create(
173
            $intent_params,
174 1
            $data->intent_options
175
        );
176
177 1
        $confirmation_params = collect([
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

177
        $confirmation_params = collect(/** @scrutinizer ignore-type */ [
Loading history...
178 1
            'payment_method' => call($data->model)->defaultPaymentMethod()->id,
179 1
        ])->merge($data->confirmation_params)->toArray();
180
181 1
        return call($this->stripeClient->paymentIntents)->confirm(
182 1
            $paymentIntent->id,
183
            $confirmation_params,
184 1
            $data->confirmation_options
185
        );
186
    }
187
}
188