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