-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-onsite-example-gateway-subscription-module.php
80 lines (71 loc) · 3.14 KB
/
class-onsite-example-gateway-subscription-module.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
<?php
use Give\Donations\Models\Donation;
use Give\Donations\Models\DonationNote;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\PaymentGateways\Commands\SubscriptionComplete;
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException;
use Give\Framework\PaymentGateways\SubscriptionModule;
use Give\Subscriptions\Models\Subscription;
use Give\Subscriptions\ValueObjects\SubscriptionStatus;
/**
* @inheritDoc
*/
class ExampleGatewayOnsiteSubscriptionModuleClass extends SubscriptionModule
{
/**
* @inerhitDoc
*
* @throws Exception|PaymentGatewayException
*/
public function createSubscription(
Donation $donation,
Subscription $subscription,
$gatewayData
) {
try {
// Step 1: Validate any data passed from the gateway fields in $gatewayData. Throw the PaymentGatewayException if the data is invalid.
if (empty($gatewayData['example-gateway-id'])) {
throw new PaymentGatewayException(__('Example payment ID is required.', 'example-give'));
}
// Step 2: Create a subscription with your gateway.
$response = ExampleGatewayApi::createSubscription(['transaction_id' => $gatewayData['example-gateway-id']]);
// Step 3: Return a command to complete the subscription. You can alternatively return SubscriptionProcessing for gateways that require a webhook or similar to confirm that the subscription is complete. SubscriptionProcessing will trigger an email notification, configurable in the settings.
return new SubscriptionComplete($response['transaction_id'], $response['id']);
} catch (Exception $e) {
// Step 4: If an error occurs, you can update the donation status to something appropriate like failed, and finally throw the PaymentGatewayException for the framework to catch the message.
$errorMessage = $e->getMessage();
$donation->status = DonationStatus::FAILED();
$donation->save();
DonationNote::create([
'donationId' => $donation->id,
'content' => sprintf(esc_html__('Donation failed. Reason: %s', 'example-give'), $errorMessage)
]);
throw new PaymentGatewayException($errorMessage);
}
}
/**
* @inerhitDoc
*
* @throws PaymentGatewayException
*/
public function cancelSubscription(Subscription $subscription)
{
try {
// Step 1: cancel the subscription with your gateway.
ExampleGatewayApi::cancelSubscription($subscription->gatewaySubscriptionId);
// Step 2: update the subscription status to cancelled.
$subscription->status = SubscriptionStatus::CANCELLED();
$subscription->save();
} catch (\Exception $exception) {
throw new PaymentGatewayException(
sprintf(
'Unable to cancel subscription. %s',
$exception->getMessage()
),
$exception->getCode(),
$exception
);
}
}
}