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