1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\StripeIntegration\Providers; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Traits\Macroable; |
9
|
|
|
use Laravel\Cashier\Exceptions\IncompletePayment; |
10
|
|
|
use Laravel\Cashier\Payment; |
11
|
|
|
use Laravel\Cashier\PaymentMethod as CashierPaymentMethod; |
12
|
|
|
use MichaelRubel\StripeIntegration\DataTransferObjects\OffsessionChargeData; |
13
|
|
|
use MichaelRubel\StripeIntegration\DataTransferObjects\StripeChargeData; |
14
|
|
|
use MichaelRubel\StripeIntegration\Decorators\StripePaymentAmount; |
15
|
|
|
use MichaelRubel\StripeIntegration\Providers\Contracts\PaymentProviderContract; |
16
|
|
|
use Money\Currency; |
17
|
|
|
use Stripe\Customer; |
18
|
|
|
use Stripe\Exception\ApiErrorException; |
19
|
|
|
use Stripe\PaymentIntent; |
20
|
|
|
use Stripe\PaymentMethod; |
21
|
|
|
use Stripe\SetupIntent; |
22
|
|
|
use Stripe\StripeClient; |
23
|
|
|
|
24
|
|
|
class StripePaymentProvider implements PaymentProviderContract |
25
|
|
|
{ |
26
|
|
|
use Macroable; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
13 |
|
public function __construct( |
32
|
|
|
protected StripeClient $stripeClient |
33
|
|
|
) { |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the Cashier's currency. |
38
|
|
|
* |
39
|
|
|
* @param Currency $currency |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
1 |
|
public function configureCashierCurrency(Currency $currency): void |
44
|
|
|
{ |
45
|
1 |
|
config([ |
46
|
1 |
|
'cashier.currency' => $currency->getCode(), |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create the Stripe setup intent. |
52
|
|
|
* |
53
|
|
|
* @param Model $model |
54
|
|
|
* @param array $options |
55
|
|
|
* |
56
|
|
|
* @return SetupIntent |
57
|
|
|
*/ |
58
|
1 |
|
public function setupIntentUsing(Model $model, array $options = []): SetupIntent |
59
|
|
|
{ |
60
|
1 |
|
$options = collect([ |
|
|
|
|
61
|
|
|
'usage' => 'off_session', |
62
|
1 |
|
])->merge($options)->toArray(); |
|
|
|
|
63
|
|
|
|
64
|
1 |
|
return call($model)->createSetupIntent($options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Prepare the customer to work with the payment system. |
69
|
|
|
* |
70
|
|
|
* @param Model $model |
71
|
|
|
* |
72
|
|
|
* @return Customer |
73
|
|
|
*/ |
74
|
3 |
|
public function prepareCustomer(Model $model): Customer |
75
|
|
|
{ |
76
|
3 |
|
return call($model)->createOrGetStripeCustomer(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Update the default payment method for model. |
81
|
|
|
* |
82
|
|
|
* @param Model $model |
83
|
|
|
* @param PaymentMethod|string $paymentMethod |
84
|
|
|
* |
85
|
|
|
* @return CashierPaymentMethod |
86
|
|
|
*/ |
87
|
3 |
|
public function updatePaymentMethod(Model $model, PaymentMethod|string $paymentMethod): CashierPaymentMethod |
88
|
|
|
{ |
89
|
3 |
|
return call($model)->updateDefaultPaymentMethod($paymentMethod); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Attach the payment method to the customer. |
94
|
|
|
* |
95
|
|
|
* @param PaymentMethod|CashierPaymentMethod $paymentMethod |
96
|
|
|
* @param Customer $customer |
97
|
|
|
* @param array $params |
98
|
|
|
* @param array $options |
99
|
|
|
* |
100
|
|
|
* @return PaymentMethod |
101
|
|
|
*/ |
102
|
3 |
|
public function attachPaymentMethodToCustomer( |
103
|
|
|
PaymentMethod|CashierPaymentMethod $paymentMethod, |
104
|
|
|
Customer $customer, |
105
|
|
|
array $params = [], |
106
|
|
|
array $options = [] |
107
|
|
|
): PaymentMethod { |
108
|
3 |
|
$params = collect([ |
|
|
|
|
109
|
3 |
|
'customer' => $customer->id, |
110
|
3 |
|
])->merge($params)->toArray(); |
|
|
|
|
111
|
|
|
|
112
|
3 |
|
return call($this->stripeClient->paymentMethods)->attach( |
113
|
3 |
|
$paymentMethod->id, |
114
|
|
|
$params, |
115
|
|
|
$options |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Perform a simple charge. |
121
|
|
|
* |
122
|
|
|
* @param StripeChargeData $data |
123
|
|
|
* |
124
|
|
|
* @return Payment |
125
|
|
|
* @throws IncompletePayment |
126
|
|
|
*/ |
127
|
1 |
|
public function charge(StripeChargeData $data): Payment |
128
|
|
|
{ |
129
|
1 |
|
return call($data->model)->charge( |
130
|
1 |
|
$data->payment_amount->getAmount(), |
131
|
1 |
|
$data->payment_method->id, |
132
|
1 |
|
$data->options |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Create a payment intent. |
138
|
|
|
* |
139
|
|
|
* @param StripePaymentAmount $paymentAmount |
140
|
|
|
* @param Model $model |
141
|
|
|
* @param array $intent_params |
142
|
|
|
* @param array $intent_options |
143
|
|
|
* |
144
|
|
|
* @return PaymentIntent |
145
|
|
|
*/ |
146
|
2 |
|
public function createPaymentIntent( |
147
|
|
|
StripePaymentAmount $paymentAmount, |
148
|
|
|
Model $model, |
149
|
|
|
array $intent_params = [], |
150
|
|
|
array $intent_options = [] |
151
|
|
|
): PaymentIntent { |
152
|
2 |
|
$intent_params = collect([ |
153
|
2 |
|
'amount' => $paymentAmount->getAmount(), |
154
|
2 |
|
'currency' => $paymentAmount->getCurrency()->getCode(), |
155
|
2 |
|
'customer' => $model->stripe_id, |
156
|
|
|
'payment_method_types' => ['card'], |
157
|
2 |
|
])->merge($intent_params)->toArray(); |
|
|
|
|
158
|
|
|
|
159
|
2 |
|
return call($this->stripeClient->paymentIntents)->create( |
160
|
|
|
$intent_params, |
161
|
|
|
$intent_options |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Update the payment intent. |
167
|
|
|
* |
168
|
|
|
* @param string $intent_id |
169
|
|
|
* @param array $params |
170
|
|
|
* @param array $options |
171
|
|
|
* |
172
|
|
|
* @return PaymentIntent |
173
|
|
|
* |
174
|
|
|
* @throws ApiErrorException |
175
|
|
|
*/ |
176
|
1 |
|
public function updatePaymentIntent(string $intent_id, array $params = [], array $options = []): PaymentIntent |
177
|
|
|
{ |
178
|
1 |
|
return call($this->stripeClient->paymentIntents)->update( |
179
|
|
|
$intent_id, |
180
|
|
|
$params, |
181
|
|
|
$options |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Confirm the payment intent. |
187
|
|
|
* |
188
|
|
|
* @param PaymentIntent $paymentIntent |
189
|
|
|
* @param array $confirmation_params |
190
|
|
|
* @param array $confirmation_options |
191
|
|
|
* |
192
|
|
|
* @return PaymentIntent |
193
|
|
|
*/ |
194
|
2 |
|
public function confirmPaymentIntent( |
195
|
|
|
PaymentIntent $paymentIntent, |
196
|
|
|
array $confirmation_params = [], |
197
|
|
|
array $confirmation_options = [] |
198
|
|
|
): PaymentIntent { |
199
|
2 |
|
return call($this->stripeClient->paymentIntents)->confirm( |
200
|
2 |
|
$paymentIntent->id, |
201
|
|
|
$confirmation_params, |
202
|
|
|
$confirmation_options |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Perform an offsession charge. |
208
|
|
|
* |
209
|
|
|
* @param OffsessionChargeData $data |
210
|
|
|
* |
211
|
|
|
* @return PaymentIntent |
212
|
|
|
* @throws ApiErrorException |
213
|
|
|
*/ |
214
|
1 |
|
public function offsessionCharge(OffsessionChargeData $data): PaymentIntent |
215
|
|
|
{ |
216
|
1 |
|
$paymentIntent = $this->createPaymentIntent( |
217
|
1 |
|
$data->payment_amount, |
218
|
1 |
|
$data->model |
219
|
|
|
); |
220
|
|
|
|
221
|
1 |
|
$confirmation_params = collect([ |
|
|
|
|
222
|
1 |
|
'payment_method' => call($data->model)->defaultPaymentMethod()->id, |
223
|
1 |
|
])->merge($data->confirmation_params)->toArray(); |
|
|
|
|
224
|
|
|
|
225
|
1 |
|
return $this->confirmPaymentIntent( |
226
|
|
|
$paymentIntent, |
227
|
|
|
$confirmation_params, |
228
|
1 |
|
$data->confirmation_options |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|