Passed
Push — main ( 59d393...bedff5 )
by Michael
03:43
created

StripePaymentProvider::updatePaymentIntent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 2
cts 2
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\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 13
    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
     * Update 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
     * Attach 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
     * Perform a simple charge.
121
     *
122
     * @param StripeChargeData $data
123
     *
124
     * @return Payment
125
     * @throws IncompletePayment
126
     */
127 1
    public function charge(StripeChargeData $data): Payment
128
    {
129 1
        return call($data->model)->charge(
130 1
            $data->payment_amount->getAmount(),
131 1
            $data->payment_method->id,
132 1
            $data->options
133
        );
134
    }
135
136
    /**
137
     * Create a payment intent.
138
     *
139
     * @param StripePaymentAmount $paymentAmount
140
     * @param Model               $model
141
     * @param array               $intent_params
142
     * @param array               $intent_options
143
     *
144
     * @return PaymentIntent
145
     */
146 2
    public function createPaymentIntent(
147
        StripePaymentAmount $paymentAmount,
148
        Model $model,
149
        array $intent_params = [],
150
        array $intent_options = []
151
    ): PaymentIntent {
152 2
        $intent_params = collect([
153 2
            'amount'               => $paymentAmount->getAmount(),
154 2
            'currency'             => $paymentAmount->getCurrency()->getCode(),
155 2
            'customer'             => $model->stripe_id,
156
            'payment_method_types' => ['card'],
157 2
        ])->merge($intent_params)->toArray();
0 ignored issues
show
Bug introduced by
$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

157
        ])->merge(/** @scrutinizer ignore-type */ $intent_params)->toArray();
Loading history...
158
159 2
        return call($this->stripeClient->paymentIntents)->create(
160
            $intent_params,
161
            $intent_options
162
        );
163
    }
164
165
    /**
166
     * Update the payment intent.
167
     *
168
     * @param string $intent_id
169
     * @param array  $params
170
     * @param array  $options
171
     *
172
     * @return PaymentIntent
173
     *
174
     * @throws ApiErrorException
175
     */
176 1
    public function updatePaymentIntent(string $intent_id, array $params = [], array $options = []): PaymentIntent
177
    {
178 1
        return call($this->stripeClient->paymentIntents)->update(
179
            $intent_id,
180
            $params,
181
            $options
182
        );
183
    }
184
185
    /**
186
     * Confirm the payment intent.
187
     *
188
     * @param PaymentIntent $paymentIntent
189
     * @param array         $confirmation_params
190
     * @param array         $confirmation_options
191
     *
192
     * @return PaymentIntent
193
     */
194 2
    public function confirmPaymentIntent(
195
        PaymentIntent $paymentIntent,
196
        array $confirmation_params = [],
197
        array $confirmation_options = []
198
    ): PaymentIntent {
199 2
        return call($this->stripeClient->paymentIntents)->confirm(
200 2
            $paymentIntent->id,
201
            $confirmation_params,
202
            $confirmation_options
203
        );
204
    }
205
206
    /**
207
     * Perform an offsession charge.
208
     *
209
     * @param OffsessionChargeData $data
210
     *
211
     * @return PaymentIntent
212
     * @throws ApiErrorException
213
     */
214 1
    public function offsessionCharge(OffsessionChargeData $data): PaymentIntent
215
    {
216 1
        $paymentIntent = $this->createPaymentIntent(
217 1
            $data->payment_amount,
218 1
            $data->model
219
        );
220
221 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

221
        $confirmation_params = collect(/** @scrutinizer ignore-type */ [
Loading history...
222 1
            'payment_method' => call($data->model)->defaultPaymentMethod()->id,
223 1
        ])->merge($data->confirmation_params)->toArray();
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

223
        ])->merge(/** @scrutinizer ignore-type */ $data->confirmation_params)->toArray();
Loading history...
224
225 1
        return $this->confirmPaymentIntent(
226
            $paymentIntent,
227
            $confirmation_params,
228 1
            $data->confirmation_options
229
        );
230
    }
231
}
232