|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\StripeIntegration\Behaviors; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use MichaelRubel\StripeIntegration\DataTransferObjects\PaymentIntentData; |
|
9
|
|
|
use Stripe\PaymentIntent; |
|
10
|
|
|
use Stripe\SetupIntent; |
|
11
|
|
|
|
|
12
|
|
|
trait ManagesPaymentIntents |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Create the Stripe Setup Intent. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Model $model |
|
18
|
|
|
* @param array $options |
|
19
|
|
|
* |
|
20
|
|
|
* @return SetupIntent |
|
21
|
|
|
*/ |
|
22
|
1 |
|
public function setupIntentUsing(Model $model, array $options = []): SetupIntent |
|
23
|
|
|
{ |
|
24
|
1 |
|
$options = collect(['usage' => 'off_session']) |
|
|
|
|
|
|
25
|
1 |
|
->merge($options) |
|
|
|
|
|
|
26
|
1 |
|
->toArray(); |
|
27
|
|
|
|
|
28
|
1 |
|
return call($model)->createSetupIntent($options); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a payment intent. |
|
33
|
|
|
* |
|
34
|
|
|
* @param PaymentIntentData $data |
|
35
|
|
|
* |
|
36
|
|
|
* @return PaymentIntent |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function createPaymentIntent(PaymentIntentData $data): PaymentIntent |
|
39
|
|
|
{ |
|
40
|
2 |
|
$makeIntentParams = collect([ |
|
|
|
|
|
|
41
|
2 |
|
'amount' => $data->paymentAmount?->getAmount(), |
|
42
|
2 |
|
'currency' => $data->paymentAmount?->getCurrency()->getCode(), |
|
43
|
|
|
'payment_method_types' => ['card'], |
|
44
|
|
|
]) |
|
45
|
2 |
|
->when($data->model?->stripeId(), fn ($params) => $params->merge([ |
|
46
|
1 |
|
'customer' => $data->model?->stripeId(), |
|
47
|
|
|
])) |
|
48
|
2 |
|
->merge($data->params) |
|
|
|
|
|
|
49
|
2 |
|
->toArray(); |
|
50
|
|
|
|
|
51
|
2 |
|
return call($this->stripeClient->paymentIntents)->create($makeIntentParams, $data->options); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Update the payment intent. |
|
56
|
|
|
* |
|
57
|
|
|
* @param PaymentIntentData $data |
|
58
|
|
|
* |
|
59
|
|
|
* @return PaymentIntent |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function updatePaymentIntent(PaymentIntentData $data): PaymentIntent |
|
62
|
|
|
{ |
|
63
|
1 |
|
$updateIntentParams = collect($data->params) |
|
|
|
|
|
|
64
|
1 |
|
->when($data->model?->stripeId(), fn ($params) => $params->merge([ |
|
65
|
1 |
|
'customer' => $data->model?->stripeId(), |
|
66
|
|
|
])) |
|
67
|
1 |
|
->toArray(); |
|
68
|
|
|
|
|
69
|
1 |
|
return call($this->stripeClient->paymentIntents)->update( |
|
70
|
1 |
|
$data->intentId, $updateIntentParams, $data->options, |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieve the payment intent. |
|
76
|
|
|
* |
|
77
|
|
|
* @param PaymentIntentData $data |
|
78
|
|
|
* |
|
79
|
|
|
* @return PaymentIntent |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function retrievePaymentIntent(PaymentIntentData $data): PaymentIntent |
|
82
|
|
|
{ |
|
83
|
1 |
|
return call($this->stripeClient->paymentIntents)->retrieve( |
|
84
|
1 |
|
$data->intentId, $data->params, $data->options, |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Confirm the payment intent. |
|
90
|
|
|
* |
|
91
|
|
|
* @param PaymentIntentData $data |
|
92
|
|
|
* |
|
93
|
|
|
* @return PaymentIntent |
|
94
|
|
|
*/ |
|
95
|
2 |
|
public function confirmPaymentIntent(PaymentIntentData $data): PaymentIntent |
|
96
|
|
|
{ |
|
97
|
2 |
|
return call($this->stripeClient->paymentIntents)->confirm( |
|
98
|
2 |
|
$data->paymentIntent?->id, $data->params, $data->options, |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|