Passed
Push — main ( cade56...9c07c4 )
by Michael
05:36 queued 02:40
created

StripePaymentProvider::attachPaymentMethodToCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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