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, |
|
|
|
|
25
|
|
|
ManagesPaymentIntents, |
26
|
|
|
ConfiguresCashier; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @extendability |
30
|
|
|
*/ |
31
|
|
|
use Macroable; |
|
|
|
|
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
|
|
|
|