1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\StripeIntegration\Providers; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Traits\Conditionable; |
8
|
|
|
use Illuminate\Support\Traits\Macroable; |
9
|
|
|
use Laravel\Cashier\Exceptions\IncompletePayment; |
10
|
|
|
use Laravel\Cashier\Payment; |
11
|
|
|
use MichaelRubel\StripeIntegration\Behaviors\ConfiguresCashier; |
12
|
|
|
use MichaelRubel\StripeIntegration\Behaviors\ManagesPaymentIntents; |
13
|
|
|
use MichaelRubel\StripeIntegration\Behaviors\PreparesPaymentMethods; |
14
|
|
|
use MichaelRubel\StripeIntegration\DataTransferObjects\OffsessionChargeData; |
15
|
|
|
use MichaelRubel\StripeIntegration\DataTransferObjects\PaymentIntentData; |
16
|
|
|
use MichaelRubel\StripeIntegration\DataTransferObjects\StripeChargeData; |
17
|
|
|
use MichaelRubel\StripeIntegration\Providers\Contracts\PaymentProviderContract; |
18
|
|
|
use Spatie\DataTransferObject\Exceptions\UnknownProperties; |
19
|
|
|
use Stripe\Exception\ApiErrorException; |
20
|
|
|
use Stripe\PaymentIntent; |
21
|
|
|
use Stripe\StripeClient; |
22
|
|
|
|
23
|
|
|
class StripePaymentProvider implements PaymentProviderContract |
24
|
|
|
{ |
25
|
|
|
use PreparesPaymentMethods, |
|
|
|
|
26
|
|
|
ManagesPaymentIntents, |
27
|
|
|
ConfiguresCashier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @extendability |
31
|
|
|
*/ |
32
|
|
|
use Macroable, Conditionable; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param StripeClient $stripeClient |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
16 |
|
public function __construct(public StripeClient $stripeClient) |
40
|
|
|
{ |
41
|
|
|
// |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Perform a simple charge. |
46
|
|
|
* |
47
|
|
|
* @param StripeChargeData $data |
48
|
|
|
* |
49
|
|
|
* @return Payment |
50
|
|
|
* @throws IncompletePayment |
51
|
|
|
*/ |
52
|
3 |
|
public function charge(StripeChargeData $data): Payment |
53
|
|
|
{ |
54
|
3 |
|
return call($data->model)->charge( |
55
|
3 |
|
$data->payment_amount->getAmount(), |
56
|
3 |
|
$data->payment_method->id, |
57
|
3 |
|
$data->options, |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Perform off-session charge. |
63
|
|
|
* |
64
|
|
|
* @param OffsessionChargeData $data |
65
|
|
|
* |
66
|
|
|
* @return PaymentIntent |
67
|
|
|
* @throws ApiErrorException|UnknownProperties |
68
|
|
|
*/ |
69
|
1 |
|
public function offsessionCharge(OffsessionChargeData $data): PaymentIntent |
70
|
|
|
{ |
71
|
1 |
|
$paymentIntent = $this->createPaymentIntent(new PaymentIntentData( |
72
|
1 |
|
paymentAmount: $data->payment_amount, |
73
|
1 |
|
model: $data->model, |
74
|
|
|
)); |
75
|
|
|
|
76
|
1 |
|
$confirmation_params = collect(['payment_method' => call($data->model)->defaultPaymentMethod()->id]) |
77
|
1 |
|
->merge($data->confirmation_params) |
78
|
1 |
|
->toArray(); |
79
|
|
|
|
80
|
1 |
|
return $this->confirmPaymentIntent(new PaymentIntentData( |
81
|
|
|
paymentIntent: $paymentIntent, |
82
|
|
|
params: $confirmation_params, |
83
|
1 |
|
options: $data->confirmation_options, |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|