-
Notifications
You must be signed in to change notification settings - Fork 192
/
TestOffsiteGateway.php
171 lines (150 loc) · 4.32 KB
/
TestOffsiteGateway.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
<?php
namespace Give\PaymentGateways\Gateways\TestOffsiteGateway;
use Give\Donations\Models\Donation;
use Give\Donations\Models\DonationNote;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\Http\Response\Types\RedirectResponse;
use Give\Framework\PaymentGateways\Commands\PaymentRefunded;
use Give\Framework\PaymentGateways\Commands\RedirectOffsite;
use Give\Framework\PaymentGateways\PaymentGateway;
use Give\Framework\Support\Facades\Scripts\ScriptAsset;
use Give\Helpers\Form\Utils as FormUtils;
use Give\Helpers\Language;
use Give\PaymentGateways\Gateways\TestGateway\Views\LegacyFormFieldMarkup;
/**
* Class TestGatewayOffsite
* @since 2.18.0
*/
class TestOffsiteGateway extends PaymentGateway
{
/**
* @inheritDoc
*/
public $secureRouteMethods = [
'securelyReturnFromOffsiteRedirect'
];
/**
* @inheritDoc
*/
public static function id(): string
{
return 'test-offsite-gateway';
}
/**
* @inheritDoc
*/
public function getId(): string
{
return self::id();
}
/**
* @inheritDoc
*/
public function getName(): string
{
return __('Test Gateway (Offsite)', 'give');
}
/**
* @since 3.0.0
*/
public function formSettings(int $formId): array
{
return [
'message' => __(
'There are no fields for this gateway and you will not be charged. This payment option is only for you to test the donation experience.',
'give'
)
];
}
/**
* @inheritDoc
*/
public function getPaymentMethodLabel(): string
{
return __('Test Gateway (Offsite)', 'give');
}
/**
* @since 3.1.0 set translations for script
* @since 2.30.0
*/
public function enqueueScript(int $formId)
{
$scriptAsset = ScriptAsset::get(GIVE_PLUGIN_DIR . 'build/testOffsiteGateway.asset.php');
$handle = $this::id();
wp_enqueue_script(
$this::id(),
GIVE_PLUGIN_URL . 'build/testOffsiteGateway.js',
$scriptAsset['dependencies'],
$scriptAsset['version'],
true
);
Language::setScriptTranslations($handle);
}
/**
* @since 2.18.0
*/
public function getLegacyFormFieldMarkup(int $formId, array $args): string
{
if (FormUtils::isLegacyForm($formId)) {
return false;
}
/** @var LegacyFormFieldMarkup $legacyFormFieldMarkup */
$legacyFormFieldMarkup = give(LegacyFormFieldMarkup::class);
return $legacyFormFieldMarkup();
}
/**
* @inheritDoc
*/
public function createPayment(Donation $donation, $gatewayData)
{
$redirectUrl = $this->generateSecureGatewayRouteUrl(
'securelyReturnFromOffsiteRedirect',
$donation->id,
[
'givewp-donation-id' => $donation->id,
'givewp-return-url' => $gatewayData['successUrl']
]
);
return new RedirectOffsite($redirectUrl);
}
/**
* @since 2.29.0 Return PaymentRefunded instead of a bool value
* @since 2.20.0
* @inerhitDoc
*/
public function refundDonation(Donation $donation): PaymentRefunded
{
return new PaymentRefunded();
}
/**
* An example of using a secureRouteMethod for extending the Gateway API to handle a redirect.
*
* @since 3.0.0
*
* @throws Exception
*/
protected function securelyReturnFromOffsiteRedirect(array $queryParams): RedirectResponse
{
/** @var Donation $donation */
$donation = Donation::find($queryParams['givewp-donation-id']);
$this->updateDonation($donation);
return new RedirectResponse($queryParams['givewp-return-url']);
}
/**
* @param Donation $donation
*
* @return void
* @throws Exception
*/
private function updateDonation(Donation $donation)
{
$donation->status = DonationStatus::COMPLETE();
$donation->gatewayTransactionId = "test-gateway-transaction-id";
$donation->save();
DonationNote::create([
'donationId' => $donation->id,
'content' => 'Donation Completed from Test Gateway Offsite.'
]);
}
}