-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathPaymentGateway.php
409 lines (354 loc) · 11.8 KB
/
PaymentGateway.php
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
namespace Give\Framework\PaymentGateways;
use Give\Donations\Models\Donation;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\PaymentGateways\Actions\GenerateGatewayRouteUrl;
use Give\Framework\PaymentGateways\Contracts\PaymentGatewayInterface;
use Give\Framework\PaymentGateways\Contracts\Subscription\SubscriptionAmountEditable;
use Give\Framework\PaymentGateways\Contracts\Subscription\SubscriptionDashboardLinkable;
use Give\Framework\PaymentGateways\Contracts\Subscription\SubscriptionPausable;
use Give\Framework\PaymentGateways\Contracts\Subscription\SubscriptionPaymentMethodEditable;
use Give\Framework\PaymentGateways\Contracts\Subscription\SubscriptionTransactionsSynchronizable;
use Give\Framework\PaymentGateways\Routes\RouteSignature;
use Give\Framework\PaymentGateways\Traits\HandleHttpResponses;
use Give\Framework\PaymentGateways\Traits\HasRouteMethods;
use Give\Framework\PaymentGateways\Webhooks\WebhookEvents;
use Give\Framework\Support\ValueObjects\Money;
use Give\Log\Log;
use Give\Subscriptions\Models\Subscription;
use ReflectionException;
use ReflectionMethod;
/**
* @since 2.30.0 added enqueueScript() and formSettings() methods.
* @since 2.18.0
*/
abstract class PaymentGateway implements PaymentGatewayInterface,
SubscriptionDashboardLinkable,
SubscriptionAmountEditable,
SubscriptionPaymentMethodEditable,
SubscriptionTransactionsSynchronizable
{
use HandleHttpResponses;
use HasRouteMethods {
supportsMethodRoute as protected SupportsOwnMethodRoute;
callRouteMethod as protected CallOwnRouteMethod;
}
/**
* @since 2.20.0 Change variable type to SubscriptionModule.
* @var SubscriptionModule $subscriptionModule
*/
public $subscriptionModule;
/**
* @unreleased
*
* @var WebhookEvents $webhookEvents
*/
public $webhookEvents;
/**
* @unreleased Add the webhookEvents property
*
* @since 2.20.0 Change first argument type to SubscriptionModule abstract class.
* @since 2.18.0
*
* @param SubscriptionModule|null $subscriptionModule
*/
public function __construct(SubscriptionModule $subscriptionModule = null)
{
if ($subscriptionModule !== null) {
$subscriptionModule->setGateway($this);
}
$this->subscriptionModule = $subscriptionModule;
$this->webhookEvents = new WebhookEvents($this::id());
}
/**
* @unreleased
*/
public static function webhookEvents(): WebhookEvents
{
$instance = new static();
return $instance->webhookEvents;
}
/**
* @since 2.30.0
*/
public function supportsFormVersions(): array
{
$versions = [];
if (method_exists($this, 'getLegacyFormFieldMarkup') && $this->isFunctionImplementedInGatewayClass(
'getLegacyFormFieldMarkup'
)) {
$versions[] = 2;
}
if (method_exists($this, 'enqueueScript') && $this->isFunctionImplementedInGatewayClass('enqueueScript')) {
$versions[] = 3;
}
return $versions;
}
/**
* Enqueue gateway scripts using WordPress wp_enqueue_script().
*
* @since 2.30.0
*
* @return void
*/
public function enqueueScript(int $formId)
{
//wp_enqueue_scripts();
}
/**
* Convenient way of localizing data to the JS gateway object accessible from `this.settings`.
*
* @since 2.30.0
*/
public function formSettings(int $formId): array
{
return [];
}
/**
* @inheritDoc
*
* @since 2.29.0
*/
public function supportsRefund(): bool
{
return $this->isFunctionImplementedInGatewayClass('refundDonation');
}
/**
* @inheritDoc
*/
public function supportsSubscriptions(): bool
{
return isset($this->subscriptionModule) || $this->isFunctionImplementedInGatewayClass('createSubscription');
}
/**
* If a subscription module isn't wanted this method can be overridden by a child class instead.
* Just make sure to override the supportsSubscriptions method as well.
*
* @inheritDoc
*/
public function createSubscription(
Donation $donation,
Subscription $subscription,
$gatewayData
) {
return $this->subscriptionModule->createSubscription($donation, $subscription, $gatewayData);
}
/**
* @inheritDoc
*/
public function cancelSubscription(Subscription $subscription)
{
$this->subscriptionModule->cancelSubscription($subscription);
}
/**
* @inheritDoc
*
* @since 3.17.0
*/
public function pauseSubscription(Subscription $subscription, array $data = []): void
{
if ($this->subscriptionModule instanceof SubscriptionPausable) {
$this->subscriptionModule->pauseSubscription($subscription, $data);
return;
}
throw new Exception('Gateway does not support pausing the subscription.');
}
/**
* @inheritDoc
*
* @since 3.17.0
*/
public function resumeSubscription(Subscription $subscription): void
{
if ($this->subscriptionModule instanceof SubscriptionPausable) {
$this->subscriptionModule->resumeSubscription($subscription);
return;
}
throw new Exception('Gateway does not support resuming the subscription.');
}
/**
* @inheritDoc
*
* @since 3.17.0
*/
public function canPauseSubscription(): bool
{
if ($this->subscriptionModule instanceof SubscriptionPausable) {
return $this->subscriptionModule->canPauseSubscription();
}
return false;
}
/**
* @since 2.21.2
* @inheritDoc
*/
public function canSyncSubscriptionWithPaymentGateway(): bool
{
if ($this->subscriptionModule) {
return $this->subscriptionModule->canSyncSubscriptionWithPaymentGateway();
}
return $this->isFunctionImplementedInGatewayClass('synchronizeSubscription');
}
/**
* @since 2.21.2
* @inheritDoc
*/
public function canUpdateSubscriptionAmount(): bool
{
if ($this->subscriptionModule) {
return $this->subscriptionModule->canUpdateSubscriptionAmount();
}
return $this->isFunctionImplementedInGatewayClass('updateSubscriptionAmount');
}
/**
* @since 2.21.2
* @inheritDoc
*/
public function canUpdateSubscriptionPaymentMethod(): bool
{
if ($this->subscriptionModule) {
return $this->subscriptionModule->canUpdateSubscriptionPaymentMethod();
}
return $this->isFunctionImplementedInGatewayClass('updateSubscriptionPaymentMethod');
}
/**
* @since 2.25.0 update return logic
* @since 2.21.2
*/
public function hasGatewayDashboardSubscriptionUrl(): bool
{
if ($this->subscriptionModule) {
return $this->subscriptionModule->hasGatewayDashboardSubscriptionUrl();
}
return $this->isFunctionImplementedInGatewayClass('gatewayDashboardSubscriptionUrl');
}
/**
* @since 2.33.0 Return synchronizeSubscription() instead nothing
* @since 2.21.2
* @inheritDoc
* @throws Exception
*/
public function synchronizeSubscription(Subscription $subscription)
{
if ($this->subscriptionModule instanceof SubscriptionTransactionsSynchronizable) {
return $this->subscriptionModule->synchronizeSubscription($subscription);
}
throw new Exception('Gateway does not support syncing subscriptions.');
}
/**
* @since 2.21.2
* @inheritDoc
* @throws Exception
*/
public function updateSubscriptionAmount(Subscription $subscription, Money $newRenewalAmount)
{
if ($this->subscriptionModule instanceof SubscriptionAmountEditable) {
$this->subscriptionModule->updateSubscriptionAmount($subscription, $newRenewalAmount);
return;
}
throw new Exception('Gateway does not support updating the subscription amount.');
}
/**
* @since 2.21.2
* @inheritDoc
* @throws Exception
*/
public function updateSubscriptionPaymentMethod(Subscription $subscription, $gatewayData)
{
if ($this->subscriptionModule instanceof SubscriptionPaymentMethodEditable) {
$this->subscriptionModule->updateSubscriptionPaymentMethod($subscription, $gatewayData);
return;
}
throw new Exception('Gateway does not support updating the subscription payment method.');
}
/**
* @since 2.21.2
* @inheritDoc
*/
public function gatewayDashboardSubscriptionUrl(Subscription $subscription): string
{
if ($this->subscriptionModule instanceof SubscriptionDashboardLinkable) {
return $this->subscriptionModule->gatewayDashboardSubscriptionUrl($subscription);
}
return false;
}
/**
* Generate gateway route url
*
* @since 2.18.0
* @since 2.19.0 remove $donationId param in favor of args
*/
public function generateGatewayRouteUrl(string $gatewayMethod, array $args = []): string
{
return (new GenerateGatewayRouteUrl())(static::id(), $gatewayMethod, $args);
}
/**
* Generate secure gateway route url
*
* @since 2.19.5 replace nonce with hash and expiration
* @since 2.19.4 replace RouteSignature args with unique donationId
* @since 2.19.0
*/
public function generateSecureGatewayRouteUrl(string $gatewayMethod, int $donationId, array $args = []): string
{
$signature = new RouteSignature(static::id(), $gatewayMethod, $donationId);
return (new GenerateGatewayRouteUrl())(
static::id(),
$gatewayMethod,
array_merge($args, [
'give-route-signature' => $signature->toHash(),
'give-route-signature-id' => $donationId,
'give-route-signature-expiration' => $signature->expiration,
])
);
}
/**
* @since 2.20.0
*/
public function supportsMethodRoute(string $method): bool
{
if ($this->subscriptionModule && $this->subscriptionModule->supportsMethodRoute($method)) {
return true;
}
return $this->supportsOwnMethodRoute($method);
}
/**
* @since 2.20.0
*
* @param string $method
*
* @throws Exception
*/
public function callRouteMethod($method, $queryParams)
{
if ($this->subscriptionModule && $this->subscriptionModule->supportsMethodRoute($method)) {
return $this->subscriptionModule->callRouteMethod($method, $queryParams);
}
return $this->callOwnRouteMethod($method, $queryParams);
}
/**
* Checks to see if the provided method is being used by the child gateway class. This is used as a helper in the "can" methods
* to see if the gateway is implementing a recurring feature without using a subscription module.
*
* @since 2.21.2
*/
private function isFunctionImplementedInGatewayClass(string $methodName): bool
{
try {
$reflector = new ReflectionMethod($this, $methodName);
} catch (ReflectionException $e) {
Log::error(
sprintf(
'ReflectionException thrown when trying to check if %s::%s is implemented in the gateway class.',
static::id(),
$methodName
),
[
'exception' => $e,
]
);
return false;
}
return ($reflector->getDeclaringClass()->getName() === get_class($this));
}
}