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 Stripe\Exception\ApiErrorException; |
19
|
|
|
use Stripe\PaymentIntent; |
20
|
|
|
use Stripe\StripeClient; |
21
|
|
|
|
22
|
|
|
class StripePaymentProvider implements PaymentProviderContract |
23
|
|
|
{ |
24
|
|
|
use PreparesPaymentMethods, |
|
|
|
|
25
|
|
|
ManagesPaymentIntents, |
26
|
|
|
ConfiguresCashier; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @extendability |
30
|
|
|
*/ |
31
|
|
|
use Macroable, Conditionable; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param StripeClient $stripeClient |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
16 |
|
public function __construct(public StripeClient $stripeClient) |
39
|
|
|
{ |
40
|
|
|
// |
41
|
16 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Perform a simple charge. |
45
|
|
|
* |
46
|
|
|
* @param StripeChargeData $data |
47
|
|
|
* |
48
|
|
|
* @return Payment |
49
|
|
|
* @throws IncompletePayment |
50
|
|
|
*/ |
51
|
3 |
|
public function charge(StripeChargeData $data): Payment |
52
|
|
|
{ |
53
|
3 |
|
return call($data->model)->charge( |
54
|
3 |
|
$data->payment_amount->getAmount(), |
55
|
3 |
|
$data->payment_method->id, |
56
|
3 |
|
$data->options, |
57
|
3 |
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Perform off-session charge. |
62
|
|
|
* |
63
|
|
|
* @param OffsessionChargeData $data |
64
|
|
|
* |
65
|
|
|
* @return PaymentIntent |
66
|
|
|
* @throws ApiErrorException |
67
|
|
|
*/ |
68
|
1 |
|
public function offsessionCharge(OffsessionChargeData $data): PaymentIntent |
69
|
|
|
{ |
70
|
1 |
|
$paymentIntent = $this->createPaymentIntent(new PaymentIntentData( |
71
|
1 |
|
paymentAmount: $data->payment_amount, |
72
|
1 |
|
model: $data->model, |
73
|
1 |
|
params: $data->intent_params, |
74
|
1 |
|
options: $data->intent_options, |
75
|
1 |
|
)); |
76
|
|
|
|
77
|
1 |
|
$confirmationParams = collect(['payment_method' => call($data->model)->defaultPaymentMethod()->id]) |
|
|
|
|
78
|
1 |
|
->merge($data->confirmation_params) |
|
|
|
|
79
|
1 |
|
->toArray(); |
80
|
|
|
|
81
|
1 |
|
return $this->confirmPaymentIntent(new PaymentIntentData( |
82
|
1 |
|
paymentIntent: $paymentIntent, |
83
|
1 |
|
params: $confirmationParams, |
84
|
1 |
|
options: $data->confirmation_options, |
85
|
1 |
|
)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|