-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLiveStripeClient.php
More file actions
174 lines (150 loc) · 6.17 KB
/
LiveStripeClient.php
File metadata and controls
174 lines (150 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace MatchBot\Client;
use MatchBot\Domain\Money;
use MatchBot\Domain\StripeConfirmationTokenId;
use MatchBot\Domain\StripeCustomerId;
use MatchBot\Domain\StripePaymentMethodId;
use Stripe\BalanceTransaction;
use Stripe\Charge;
use Stripe\ConfirmationToken;
use Stripe\Customer;
use Stripe\CustomerSession;
use Stripe\PaymentIntent;
use Stripe\PaymentMethod;
use Stripe\SetupIntent;
use Stripe\StripeClient;
/**
* Connects to a real Stripe service, either in test mode or production mode.
*
*/
class LiveStripeClient implements Stripe
{
public const array SESSION_COMPONENTS = [
'payment_element' => [
'enabled' => true,
'features' => [
'payment_method_redisplay' => 'enabled',
// include both recently added cards where donor ticked box to have card saved, and older cards
// where we didn't ask them.
'payment_method_allow_redisplay_filters' => ['always', 'unspecified'],
'payment_method_redisplay_limit' => 3, // Keep default 3; 10 is max stripe allows.
// default value – need to ensure it stays off to avoid breaking Regular Giving by mistake,
// since the list can include `off_session` saved cards that may be mandate-linked.
'payment_method_remove' => 'disabled',
'payment_method_save' => 'enabled',
'payment_method_save_usage' => 'on_session',
],
]
];
public function __construct(private StripeClient $stripeClient)
{
}
#[\Override]
public function cancelPaymentIntent(string $paymentIntentId): void
{
$this->stripeClient->paymentIntents->cancel($paymentIntentId);
}
/**
* @psalm-suppress InvalidArgument (see comment inside function)
*/
#[\Override]
public function updatePaymentIntent(string $paymentIntentId, array $updateData): void
{
// see https://github.com/stripe/stripe-php/issues/1854 "The doctype regarding metadata is wrong"
// @phpstan-ignore argument.type
$this->stripeClient->paymentIntents->update($paymentIntentId, $updateData);
}
#[\Override]
public function confirmPaymentIntent(string $paymentIntentId, array $params): PaymentIntent
{
return $this->stripeClient->paymentIntents->confirm($paymentIntentId, $params);
}
#[\Override]
public function retrievePaymentIntent(string $paymentIntentId): PaymentIntent
{
return $this->stripeClient->paymentIntents->retrieve($paymentIntentId);
}
#[\Override]
public function retrieveConfirmationToken(StripeConfirmationTokenId $confirmationTokenId): ConfirmationToken
{
return $this->stripeClient->confirmationTokens->retrieve($confirmationTokenId->stripeConfirmationTokenId);
}
#[\Override]
public function retrieveCharge(string $chargeId): Charge
{
return $this->stripeClient->charges->retrieve($chargeId);
}
#[\Override]
public function createPaymentIntent(array $createPayload): PaymentIntent
{
return $this->stripeClient->paymentIntents->create($createPayload);
}
#[\Override]
public function createSetupIntent(StripeCustomerId $stripeCustomerId): SetupIntent
{
return $this->stripeClient->setupIntents->create([
'customer' => $stripeCustomerId->stripeCustomerId,
'automatic_payment_methods' => ['enabled' => true],
]);
}
#[\Override]
public function createCustomerSession(StripeCustomerId $stripeCustomerId): CustomerSession
{
return $this->stripeClient->customerSessions->create([
'customer' => $stripeCustomerId->stripeCustomerId,
'components' => self::SESSION_COMPONENTS,
]);
}
#[\Override]
public function createRegularGivingCustomerSession(StripeCustomerId $stripeCustomerId): CustomerSession
{
$components = self::SESSION_COMPONENTS;
$components['payment_element']['features']['payment_method_save_usage'] = 'off_session';
$components['payment_element']['features']['payment_method_redisplay'] = 'disabled'; // Ensure method's not removed during non-RG checkout
unset($components['payment_element']['features']['payment_method_allow_redisplay_filters']);
unset($components['payment_element']['features']['payment_method_redisplay_limit']);
return $this->stripeClient->customerSessions->create([
'customer' => $stripeCustomerId->stripeCustomerId,
'components' => $components,
]);
}
#[\Override]
public function retrieveBalanceTransaction(string $id): BalanceTransaction
{
return $this->stripeClient->balanceTransactions->retrieve($id);
}
#[\Override]
public function retrievePaymentMethod(StripeCustomerId $customerId, StripePaymentMethodId $methodId): PaymentMethod
{
return $this->stripeClient->customers->retrievePaymentMethod(
$customerId->stripeCustomerId,
$methodId->stripePaymentMethodId
);
}
#[\Override]
public function detatchPaymentMethod(StripePaymentMethodId $paymentMethodId): void
{
$this->stripeClient->paymentMethods->detach($paymentMethodId->stripePaymentMethodId);
}
#[\Override]
public function deleteCustomer(StripeCustomerId $getStripeCustomerId): void
{
$this->stripeClient->customers->delete($getStripeCustomerId->stripeCustomerId);
}
#[\Override]
public function retrieveCustomer(StripeCustomerId $stripeCustomerId, array $params): Customer
{
return $this->stripeClient->customers->retrieve($stripeCustomerId->stripeCustomerId, $params);
}
#[\Override]
public function refundCustomerBalance(StripeCustomerId $stripeCustomerId, Money $money): void
{
// see https://docs.stripe.com/payments/customer-balance/refunding#create-return-dashboard--api
$this->stripeClient->refunds->create([
'amount' => $money->amountInPence(),
'currency' => $money->currency->isoCode('lower'),
'customer' => $stripeCustomerId->stripeCustomerId,
'origin' => \Stripe\PaymentMethod::TYPE_CUSTOMER_BALANCE,
]);
}
}