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
|
11 |
|
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
|
|
|
* Updates 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
|
|
|
* Attaches 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
|
|
|
* Creates a payment intent. |
121
|
|
|
* |
122
|
|
|
* @param StripePaymentAmount $paymentAmount |
123
|
|
|
* @param Model $model |
124
|
|
|
* |
125
|
|
|
* @return PaymentIntent |
126
|
|
|
* @throws ApiErrorException |
127
|
|
|
*/ |
128
|
1 |
|
public function createPaymentIntent(StripePaymentAmount $paymentAmount, Model $model): PaymentIntent |
129
|
|
|
{ |
130
|
1 |
|
return call($this->stripeClient->paymentIntents)->create([ |
131
|
1 |
|
'amount' => $paymentAmount->getAmount(), |
132
|
1 |
|
'currency' => $paymentAmount->getCurrency()->getCode(), |
133
|
1 |
|
'customer' => $model->stripe_id, |
134
|
|
|
'payment_method_types' => ['card'], |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Simple charge. |
140
|
|
|
* |
141
|
|
|
* @param StripeChargeData $data |
142
|
|
|
* |
143
|
|
|
* @return Payment |
144
|
|
|
* @throws IncompletePayment |
145
|
|
|
*/ |
146
|
1 |
|
public function charge(StripeChargeData $data): Payment |
147
|
|
|
{ |
148
|
1 |
|
return call($data->model)->charge( |
149
|
1 |
|
$data->payment_amount->getAmount(), |
150
|
1 |
|
$data->payment_method->id, |
151
|
1 |
|
$data->options |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Offsession charge. |
157
|
|
|
* |
158
|
|
|
* @param OffsessionChargeData $data |
159
|
|
|
* |
160
|
|
|
* @return PaymentIntent |
161
|
|
|
* @throws ApiErrorException |
162
|
|
|
*/ |
163
|
1 |
|
public function offsessionCharge(OffsessionChargeData $data): PaymentIntent |
164
|
|
|
{ |
165
|
1 |
|
$intent_params = collect([ |
166
|
1 |
|
'amount' => $data->payment_amount->getAmount(), |
167
|
1 |
|
'currency' => $data->payment_amount->getCurrency()->getCode(), |
168
|
1 |
|
'customer' => $data->model->stripe_id, |
169
|
|
|
'payment_method_types' => ['card'], |
170
|
1 |
|
])->merge($data->intent_params)->toArray(); |
|
|
|
|
171
|
|
|
|
172
|
1 |
|
$paymentIntent = call($this->stripeClient->paymentIntents)->create( |
173
|
|
|
$intent_params, |
174
|
1 |
|
$data->intent_options |
175
|
|
|
); |
176
|
|
|
|
177
|
1 |
|
$confirmation_params = collect([ |
|
|
|
|
178
|
1 |
|
'payment_method' => call($data->model)->defaultPaymentMethod()->id, |
179
|
1 |
|
])->merge($data->confirmation_params)->toArray(); |
180
|
|
|
|
181
|
1 |
|
return call($this->stripeClient->paymentIntents)->confirm( |
182
|
1 |
|
$paymentIntent->id, |
183
|
|
|
$confirmation_params, |
184
|
1 |
|
$data->confirmation_options |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|