-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-onsite-example-gateway.php
132 lines (115 loc) · 4.4 KB
/
class-onsite-example-gateway.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
<?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\GatewayCommand;
use Give\Framework\PaymentGateways\Commands\PaymentComplete;
use Give\Framework\PaymentGateways\Commands\PaymentRefunded;
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException;
use Give\Framework\PaymentGateways\PaymentGateway;
/**
* @inheritDoc
*/
class ExampleGatewayOnsiteClass extends PaymentGateway
{
/**
* @inheritDoc
*/
public static function id(): string
{
return 'onsite-example-test-gateway';
}
/**
* @inheritDoc
*/
public function getId(): string
{
return self::id();
}
/**
* @inheritDoc
*/
public function getName(): string
{
return __('Example Gateway - Onsite', 'example-give');
}
/**
* @inheritDoc
*/
public function getPaymentMethodLabel(): string
{
return __('Example Gateway - Onsite', 'example-give');
}
/**
* Display gateway fields for v2 donation forms
*/
public function getLegacyFormFieldMarkup(int $formId, array $args): string
{
// Step 1: add any gateway fields to the form using html. In order to retrieve this data later the name of the input must be inside the key gatewayData (name='gatewayData[input_name]').
// Step 2: you can alternatively send this data to the $gatewayData param using the filter `givewp_create_payment_gateway_data_{gatewayId}`.
return "<div><input type='text' name='gatewayData[example-gateway-id]' placeholder='Example gateway field' /></div>";
}
/**
* Register a js file to display gateway fields for v3 donation forms
*/
public function enqueueScript(int $formId)
{
wp_enqueue_script('onsite-example-gateway', plugin_dir_url(__FILE__) . 'js/onsite-example-gateway.js', ['react', 'wp-element'], '1.0.0', true);
}
/**
* Send form settings to the js gateway counterpart
*/
public function formSettings(int $formId): array
{
return [
'clientKey' => '1234567890'
];
}
/**
* @inheritDoc
*/
public function createPayment(Donation $donation, $gatewayData): GatewayCommand
{
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 payment with your gateway.
$response = $this->exampleRequest(['transaction_id' => $gatewayData['example-gateway-id']]);
// Step 3: Return a command to complete the donation. You can alternatively return PaymentProcessing for gateways that require a webhook or similar to confirm that the payment is complete. PaymentProcessing will trigger a Payment Processing email notification, configurable in the settings.
return new PaymentComplete($response['transaction_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
*/
public function refundDonation(Donation $donation): PaymentRefunded
{
// Step 1: refund the donation with your gateway.
// Step 2: return a command to complete the refund.
return new PaymentRefunded();
}
/**
* Example request to gateway
*/
private function exampleRequest(array $data): array
{
return array_merge([
'success' => true,
'transaction_id' => '1234567890',
'subscription_id' => '0987654321',
], $data);
}
}