PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.15.5
GiveWP – Donation Plugin and Fundraising Platform v4.15.5
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / DonationForms / DataTransferObjects / DonateControllerData.php

DonateControllerData.php in GiveWP – Donation Plugin and Fundraising Platform 4.15.5, at src/DonationForms/DataTransferObjects/DonateControllerData.php

366 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonationForms\DataTransferObjects;
4
5 use Exception;
6 use Give\DonationForms\Actions\GenerateDonationConfirmationReceiptUrl;
7 use Give\DonationForms\Actions\GenerateDonationConfirmationReceiptViewRouteUrl;
8 use Give\DonationForms\Models\DonationForm;
9 use Give\Donations\Models\Donation;
10 use Give\Donations\Properties\BillingAddress;
11 use Give\Donations\ValueObjects\DonationStatus;
12 use Give\Donations\ValueObjects\DonationType;
13 use Give\Framework\PaymentGateways\PaymentGateway;
14 use Give\Framework\PaymentGateways\PaymentGatewayRegister;
15 use Give\Framework\Support\ValueObjects\Money;
16 use Give\Subscriptions\Models\Subscription;
17 use Give\Subscriptions\ValueObjects\SubscriptionMode;
18 use Give\Subscriptions\ValueObjects\SubscriptionPeriod;
19 use Give\Subscriptions\ValueObjects\SubscriptionStatus;
20
21 class DonateControllerData
22 {
23 /**
24 * @var float
25 */
26 public $amount;
27 /**
28 * @var bool|null
29 */
30 public $anonymous;
31 /**
32 * @var string
33 */
34 public $gatewayId;
35 /**
36 * @var string
37 */
38 public $currency;
39 /**
40 * @var string
41 */
42 public $firstName;
43 /**
44 * @var string
45 */
46 public $lastName;
47 /**
48 * @var string
49 */
50 public $email;
51 /**
52 * @var string|null
53 */
54 public $phone;
55 /**
56 * @var int
57 */
58 public $wpUserId;
59 /**
60 * @var int
61 */
62 public $formId;
63 /**
64 * @var string
65 */
66 public $formTitle;
67 /**
68 * @var string|null
69 */
70 public $company;
71 /**
72 * @var string|null
73 */
74 public $honorific;
75 /**
76 * @var string
77 */
78 public $originUrl;
79 /**
80 * @var string|null
81 */
82 public $embedId;
83 /**
84 * @var bool
85 */
86 public $isEmbed;
87 /**
88 * @var DonationType
89 */
90 public $donationType;
91 /**
92 * @var SubscriptionPeriod|null
93 */
94 public $subscriptionPeriod;
95 /**
96 * @var int|null
97 */
98 public $subscriptionFrequency;
99 /**
100 * @var int|null
101 */
102 public $subscriptionInstallments;
103 /**
104 * @var string|null
105 */
106 public $country;
107 /**
108 * @var string|null
109 */
110 public $address1;
111 /**
112 * @var string|null
113 */
114 public $address2;
115 /**
116 * @var string|null
117 */
118 public $city;
119 /**
120 * @var string|null
121 */
122 public $state;
123 /**
124 * @var string|null
125 */
126 public $zip;
127 /**
128 * @var string|null
129 */
130 public $comment;
131
132 /**
133 * @since 3.9.0 Added phone property
134 * @since 3.2.0 added honorific property
135 * @since 3.0.0
136 */
137 public function toDonation(int $donorId): Donation
138 {
139 $form = $this->getDonationForm();
140
141 return new Donation([
142 'status' => DonationStatus::PENDING(),
143 'gatewayId' => $this->gatewayId,
144 'amount' => $this->amount(),
145 'anonymous' => $this->anonymous,
146 'donorId' => $donorId,
147 'honorific' => $this->honorific,
148 'firstName' => $this->firstName,
149 'lastName' => $this->lastName,
150 'email' => $this->email,
151 'phone' => $this->phone,
152 'formId' => $this->formId,
153 'formTitle' => $form->title,
154 'company' => $this->company,
155 'comment' => $this->comment,
156 'type' => DonationType::SINGLE(),
157 'billingAddress' => $this->getBillingAddress(),
158 ]);
159 }
160
161 /**
162 * @since 3.9.0 Added phone property
163 * @since 3.0.0
164 */
165 public function toInitialSubscriptionDonation(int $donorId, int $subscriptionId): Donation
166 {
167 $form = $this->getDonationForm();
168
169 return new Donation([
170 'status' => DonationStatus::PENDING(),
171 'gatewayId' => $this->gatewayId,
172 'amount' => $this->amount(),
173 'anonymous' => $this->anonymous,
174 'donorId' => $donorId,
175 'firstName' => $this->firstName,
176 'lastName' => $this->lastName,
177 'email' => $this->email,
178 'phone' => $this->phone,
179 'formId' => $this->formId,
180 'formTitle' => $form->title,
181 'company' => $this->company,
182 'comment' => $this->comment,
183 'type' => DonationType::SUBSCRIPTION(),
184 'subscriptionId' => $subscriptionId,
185 'billingAddress' => $this->getBillingAddress(),
186 ]);
187 }
188
189 /**
190 * @since 3.16.0 Added "givewp_donation_confirmation_page_redirect_enabled" filter
191 * @since 3.0.0
192 */
193 public function getSuccessUrl(Donation $donation): string
194 {
195 $form = $this->getDonationForm();
196
197 if (apply_filters('givewp_donation_confirmation_page_redirect_enabled', $form->settings->enableReceiptConfirmationPage, $donation->formId)) {
198 return $this->getDonationConfirmationPageFromSettings($donation);
199 }
200
201 return $this->isEmbed ?
202 $this->getDonationConfirmationReceiptUrl($donation) :
203 $this->getDonationConfirmationReceiptViewRouteUrl($donation);
204 }
205
206 /**
207 * @since 3.0.0
208 *
209 * TODO: add params to route for flash message
210 */
211 public function getCancelUrl(): string
212 {
213 return $this->originUrl;
214 }
215
216 /**
217 * @since 3.0.0
218 */
219 public function getDonationConfirmationReceiptViewRouteUrl(Donation $donation): string
220 {
221 return (new GenerateDonationConfirmationReceiptViewRouteUrl())($donation->purchaseKey);
222 }
223
224 /**
225 * @since 3.0.0
226 */
227 public function getDonationConfirmationReceiptUrl(Donation $donation): string
228 {
229 return (new GenerateDonationConfirmationReceiptUrl())($donation, $this->originUrl, $this->embedId);
230 }
231
232 /**
233 * @since 3.16.0
234 */
235 public function getDonationConfirmationPageFromSettings(Donation $donation): string
236 {
237 $settings = give_get_settings();
238
239 $page = isset($settings['success_page'])
240 ? get_permalink(absint($settings['success_page']))
241 : get_bloginfo('url');
242
243 $page = apply_filters('givewp_donation_confirmation_page_redirect_permalink', $page, $donation->formId);
244
245 return esc_url_raw(add_query_arg(['receipt-id' => $donation->purchaseKey], $page));
246 }
247
248 /**
249 * @since 3.0.0
250 */
251 public function getDonationForm(): DonationForm
252 {
253 return DonationForm::find($this->formId);
254 }
255
256 /**
257 * This is a hard-coded way of filtering through our dynamic properties
258 * and only returning custom fields.
259 *
260 * TODO: figure out a less static way of doing this
261 *
262 * @since 3.0.0
263 */
264 public function getCustomFields(): array
265 {
266 $properties = get_object_vars($this);
267
268 return array_filter($properties, static function ($param) {
269 return !in_array(
270 $param,
271 array_merge(
272 Donation::propertyKeys(),
273 Subscription::propertyKeys(),
274 [
275 'currency',
276 'wpUserId',
277 'honorific',
278 'originUrl',
279 'isEmbed',
280 'embedId',
281 'donationType',
282 'subscriptionPeriod',
283 'subscriptionFrequency',
284 'subscriptionInstallments',
285 'country',
286 'address1',
287 'address2',
288 'city',
289 'state',
290 'zip',
291 ]
292 ),
293 true
294 );
295 }, ARRAY_FILTER_USE_KEY);
296 }
297
298 /**
299 * @since 3.0.0
300 *
301 * @throws Exception
302 */
303 public function toSubscription(int $donorId): Subscription
304 {
305 return new Subscription([
306 'amount' => $this->amount(),
307 'period' => $this->subscriptionPeriod,
308 'frequency' => (int)$this->subscriptionFrequency,
309 'donorId' => $donorId,
310 'installments' => (int)$this->subscriptionInstallments,
311 'status' => SubscriptionStatus::PENDING(),
312 'mode' => give_is_test_mode() ? SubscriptionMode::TEST() : SubscriptionMode::LIVE(),
313 'donationFormId' => $this->formId,
314 ]);
315 }
316
317 /**
318 * @return Money
319 */
320 public function amount(): Money
321 {
322 return Money::fromDecimal($this->amount, $this->currency);
323 }
324
325 /**
326 * @since 3.0.0
327 */
328 public function getGateway(): PaymentGateway
329 {
330 return give(PaymentGatewayRegister::class)->getPaymentGateway($this->gatewayId);
331 }
332
333 /**
334 * @since 3.0.0
335 */
336 public function getBillingAddress(): BillingAddress
337 {
338 return BillingAddress::fromArray([
339 'country' => $this->country,
340 'address1' => $this->address1,
341 'address2' => $this->address2,
342 'city' => $this->city,
343 'state' => $this->state,
344 'zip' => $this->zip,
345 ]);
346 }
347
348 /**
349 * @since 3.0.0
350 */
351 public function has(string $name): bool
352 {
353 return isset($this->{$name});
354 }
355
356 /**
357 * @since 3.0.0
358 *
359 * @return mixed|null
360 */
361 public function get(string $name)
362 {
363 return property_exists($this, $name) ? $this->{$name} : null;
364 }
365 }
366