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