Passed
Pull Request — main (#1)
by Michael
18:55 queued 15:42
created

StripePaymentProvider::prepareCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\StripeIntegration\Providers;
6
7
use Illuminate\Support\Traits\Macroable;
8
use Laravel\Cashier\Exceptions\IncompletePayment;
9
use Laravel\Cashier\Payment;
10
use MichaelRubel\StripeIntegration\Behaviors\ConfiguresCashier;
11
use MichaelRubel\StripeIntegration\Behaviors\ManagesPaymentIntents;
12
use MichaelRubel\StripeIntegration\Behaviors\PreparesPaymentMethods;
13
use MichaelRubel\StripeIntegration\DataTransferObjects\OffsessionChargeData;
14
use MichaelRubel\StripeIntegration\DataTransferObjects\PaymentIntentData;
15
use MichaelRubel\StripeIntegration\DataTransferObjects\StripeChargeData;
16
use MichaelRubel\StripeIntegration\Providers\Contracts\PaymentProviderContract;
17
use Spatie\DataTransferObject\Exceptions\UnknownProperties;
18
use Stripe\Exception\ApiErrorException;
19
use Stripe\PaymentIntent;
20
use Stripe\StripeClient;
21
22
class StripePaymentProvider implements PaymentProviderContract
23
{
24
    use PreparesPaymentMethods,
0 ignored issues
show
introduced by
The trait MichaelRubel\StripeInteg...s\ManagesPaymentIntents requires some properties which are not provided by MichaelRubel\StripeInteg...s\StripePaymentProvider: $model, $paymentIntents, $params, $paymentAmount, $options, $id, $paymentIntent, $intentId
Loading history...
introduced by
The trait MichaelRubel\StripeInteg...\PreparesPaymentMethods requires some properties which are not provided by MichaelRubel\StripeInteg...s\StripePaymentProvider: $paymentMethod, $params, $paymentMethods, $options, $id, $customer
Loading history...
25
        ManagesPaymentIntents,
26
        ConfiguresCashier;
27
28
    /**
29
     * @extendability
30
     */
31
    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...
32
33
    /**
34
     * @return void
35
     */
36 14
    public function __construct(public StripeClient $stripeClient)
37
    {
38
        //
39
    }
40
41
    /**
42
     * Perform a simple charge.
43
     *
44
     * @param  StripeChargeData  $data
45
     *
46
     * @return Payment
47
     * @throws IncompletePayment
48
     */
49 1
    public function charge(StripeChargeData $data): Payment
50
    {
51 1
        return call($data->model)->charge(
52 1
            $data->payment_amount->getAmount(),
53 1
            $data->payment_method->id,
54 1
            $data->options,
55
        );
56
    }
57
58
    /**
59
     * Perform an offsession charge.
60
     *
61
     * @param  OffsessionChargeData  $data
62
     *
63
     * @return PaymentIntent
64
     * @throws ApiErrorException|UnknownProperties
65
     */
66 1
    public function offsessionCharge(OffsessionChargeData $data): PaymentIntent
67
    {
68 1
        $paymentIntent = $this->createPaymentIntent(new PaymentIntentData(
69 1
            paymentAmount: $data->payment_amount,
70 1
            model: $data->model,
71
        ));
72
73 1
        $confirmation_params = collect(['payment_method' => call($data->model)->defaultPaymentMethod()->id])
74 1
            ->merge($data->confirmation_params)
75 1
            ->toArray();
76
77 1
        return $this->confirmPaymentIntent(new PaymentIntentData(
78
            paymentIntent: $paymentIntent,
79
            params: $confirmation_params,
80 1
            options: $data->confirmation_options,
81
        ));
82
    }
83
}
84