PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 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 All 257 releases
give / src / Framework / Receipts / Actions / GenerateConfirmationPageReceipt.php

GenerateConfirmationPageReceipt.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Framework/Receipts/Actions/GenerateConfirmationPageReceipt.php

394 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\Receipts\Actions;
4
5 use Exception;
6 use Give\DonationForms\Models\DonationForm;
7 use Give\DonationForms\Repositories\DonationFormRepository;
8 use Give\Donations\Models\Donation;
9 use Give\Framework\FieldsAPI\Concerns\HasLabel;
10 use Give\Framework\FieldsAPI\Concerns\HasName;
11 use Give\Framework\FieldsAPI\Field;
12 use Give\Framework\PaymentGateways\PaymentGatewayRegister;
13 use Give\Framework\Receipts\DonationReceipt;
14 use Give\Framework\Receipts\Properties\ReceiptDetail;
15 use Give\Framework\TemplateTags\DonationTemplateTags;
16 use Give\Log\Log;
17
18 class GenerateConfirmationPageReceipt
19 {
20 /**
21 * @since 3.0.0
22 */
23 public function __invoke(DonationReceipt $receipt): DonationReceipt
24 {
25 $this->fillSettings($receipt);
26 $this->fillDonorDetails($receipt);
27 $this->fillDonationDetails($receipt);
28 $this->fillEventTicketsDetails($receipt);
29 $this->fillSubscriptionDetails($receipt);
30 $this->fillAdditionalDetails($receipt);
31
32 return $receipt;
33 }
34
35 /**
36 * @since 4.3.0 return early if the form model is not found
37 * @since 3.4.0 updated to check for metaKey first and then fallback to name
38 * @since 3.3.0 updated conditional to check for scopes and added support for retrieving values programmatically with Fields API
39 * @since 3.0.0
40 */
41 protected function getCustomFields(Donation $donation): array
42 {
43 /** @var DonationForm $form */
44 $form = DonationForm::find($donation->formId);
45
46 if (!$form) {
47 return [];
48 }
49
50 $customFields = array_filter($form->schema()->getFields(), static function (Field $field) {
51 return $field->shouldShowInReceipt();
52 });
53
54 $receiptDetails = [];
55 foreach ($customFields as $field) {
56 /** @var Field|HasLabel|HasName $field */
57 if ($field->hasReceiptValue()) {
58 try {
59 $value = $field->isReceiptValueCallback() ? $field->getReceiptValue()($field, $donation) : null;
60 } catch (Exception $e) {
61 $value = null;
62
63 Log::error('Error getting receipt value for field', [
64 'field' => $field->getName(),
65 'error' => $e->getMessage(),
66 'trace' => $e->getTraceAsString(),
67 ]);
68 }
69 } elseif ($field->getScope()->isDonor()) {
70 if (!metadata_exists('donor', $donation->donor->id, $field->getMetaKey() ?? $field->getName())) {
71 continue;
72 }
73
74 $value = give()->donor_meta->get_meta(
75 $donation->donor->id,
76 $field->getMetaKey() ?? $field->getName(),
77 true
78 );
79 } elseif ($field->getScope()->isDonation()) {
80 if (!metadata_exists('donation', $donation->id, $field->getMetaKey() ?? $field->getName())) {
81 continue;
82 }
83
84 $value = give()->payment_meta->get_meta($donation->id, $field->getMetaKey() ?? $field->getName(), true);
85 } else {
86 $value = null;
87 }
88
89 if (is_null($value)) {
90 continue;
91 }
92
93 $value = apply_filters(
94 sprintf("givewp_donation_confirmation_page_field_value_for_%s", $field->getName()),
95 $value,
96 $field,
97 $donation
98 );
99
100 $label = apply_filters(
101 sprintf("givewp_donation_confirmation_page_field_label_for_%s", $field->getName()),
102 $field->hasReceiptLabel() ? $field->getReceiptLabel() : $field->getLabel(),
103 $field,
104 $donation
105 );
106
107 if (!empty($label)) {
108 $receiptDetails[] = new ReceiptDetail(
109 $label,
110 $value ?? ''
111 );
112 }
113 }
114
115 return $receiptDetails;
116 }
117
118 /**
119 * @since 3.6.0 added support for event tickets
120 * @since 3.0.0
121 *
122 * @return void
123 */
124 private function fillDonationDetails(DonationReceipt $receipt)
125 {
126 /** @var PaymentGatewayRegister $paymentGatewayRegistrar */
127 $paymentGatewayRegistrar = give(PaymentGatewayRegister::class);
128 $paymentMethodLabel = give_get_gateway_checkout_label($receipt->donation->gatewayId, null);
129
130 if (empty($paymentMethodLabel) || $paymentMethodLabel === $receipt->donation->gatewayId) {
131 $paymentMethodLabel = $paymentGatewayRegistrar->hasPaymentGateway(
132 $receipt->donation->gatewayId
133 ) ? $paymentGatewayRegistrar->getPaymentGateway($receipt->donation->gatewayId)->getPaymentMethodLabel(
134 ) : $receipt->donation->gatewayId;
135 }
136
137 $receipt->donationDetails->addDetails([
138 new ReceiptDetail(
139 __('Payment Status', 'give'),
140 $receipt->donation->status->label()
141 ),
142 new ReceiptDetail(
143 __('Payment Method', 'give'),
144 $paymentMethodLabel
145 ),
146 new ReceiptDetail(
147 __('Donation Amount', 'give'),
148 ['amount' => apply_filters('givewp_generate_confirmation_page_receipt_detail_donation_amount', $receipt->donation->intendedAmount()->formatToDecimal(), $receipt)]
149 ),
150 ]
151 );
152
153 if ($receipt->donation->feeAmountRecovered) {
154 $receipt->donationDetails->addDetail(
155 new ReceiptDetail(
156 __('Processing Fee', 'give'),
157 ['amount' => $receipt->donation->feeAmountRecovered->formatToDecimal()]
158 )
159 );
160 }
161
162 do_action('givewp_generate_confirmation_page_receipt_before_donation_total', $receipt);
163
164 $receipt->donationDetails->addDetail(
165 new ReceiptDetail(
166 __('Donation Total', 'give'),
167 ['amount' => $receipt->donation->amount->formatToDecimal()]
168 )
169 );
170 }
171
172 /**
173 * @since 3.9.0 Add phone number to donor details
174 * @since 3.0.0
175 *
176 * @return void
177 */
178 private function fillDonorDetails(DonationReceipt $receipt)
179 {
180 $details = [
181 new ReceiptDetail(
182 __('Donor Name', 'give'),
183 trim("{$receipt->donation->firstName} {$receipt->donation->lastName}")
184 ),
185 new ReceiptDetail(
186 __('Email Address', 'give'),
187 $receipt->donation->email
188 ),
189 ];
190
191 if ($receipt->donation->phone) {
192 $details[] = new ReceiptDetail(
193 __('Phone Number', 'give'),
194 $receipt->donation->phone
195 );
196 }
197
198 if ($receipt->donation->billingAddress->country) {
199 $details[] = new ReceiptDetail(
200 __('Billing Address', 'give'),
201 $receipt->donation->billingAddress->address1 . ' ' . $receipt->donation->billingAddress->address2 . PHP_EOL .
202 $receipt->donation->billingAddress->city . ($receipt->donation->billingAddress->state ? ', ' . $receipt->donation->billingAddress->state : '') . ' ' . $receipt->donation->billingAddress->zip . PHP_EOL .
203 $receipt->donation->billingAddress->country . PHP_EOL
204 );
205 }
206
207 $receipt->donorDetails->addDetails($details);
208 }
209
210 /**
211 * @since 3.0.0
212 *
213 * @return void
214 */
215 private function fillAdditionalDetails(DonationReceipt $receipt)
216 {
217 if ($receipt->donation->company) {
218 $receipt->additionalDetails->addDetail(
219 new ReceiptDetail(
220 __('Company Name', 'give'),
221 $receipt->donation->company
222 )
223 );
224 }
225
226 if ($receipt->donation->comment) {
227 $receipt->additionalDetails->addDetail(
228 new ReceiptDetail(
229 __('Comment', 'give'),
230 $receipt->donation->comment
231 )
232 );
233 }
234
235 if ($receipt->donation->anonymous) {
236 $receipt->additionalDetails->addDetail(
237 new ReceiptDetail(
238 __('Anonymous Donation', 'give'),
239 'Yes'
240 )
241 );
242 }
243
244 if ($customFields = $this->getCustomFields($receipt->donation)) {
245 $receipt->additionalDetails->addDetails($customFields);
246 }
247 }
248
249 /**
250 * @since 3.0.0
251 *
252 * @return void
253 */
254 private function fillSubscriptionDetails(DonationReceipt $receipt)
255 {
256 if ($receipt->donation->subscriptionId) {
257 $subscription = $receipt->donation->subscription;
258 $subscriptionAmountLabel = sprintf(
259 $subscription->period->label($subscription->frequency),
260 $subscription->frequency
261 );
262
263 $receipt->subscriptionDetails->addDetails([
264 new ReceiptDetail(
265 __('Subscription', 'give'),
266 [
267 'amount' =>
268 sprintf(
269 '%s / %s',
270 $subscription->amount->formatToDecimal(),
271 $subscriptionAmountLabel
272 )
273 ]
274 ),
275 new ReceiptDetail(
276 __('Subscription Status', 'give'),
277 $subscription->status->label()
278 ),
279 new ReceiptDetail(
280 __('Renewal Date', 'give'),
281 $subscription->renewsAt->format('F j, Y')
282 ),
283 new ReceiptDetail(
284 __('Progress', 'give'),
285 sprintf(
286 '%s / %s',
287 count($subscription->donations),
288 $subscription->installments > 0 ? $subscription->installments : __('Ongoing', 'give')
289 )
290 ),
291 ]);
292 }
293 }
294
295 /**
296 * @since 3.0.0
297 */
298 private function fillSettings(DonationReceipt $receipt)
299 {
300 $donationForm = $this->getDonationForm($receipt->donation->formId);
301
302 $receipt->settings->addSetting(
303 'heading',
304 nl2br($this->getHeading($receipt, $donationForm))
305 );
306
307 $receipt->settings->addSetting(
308 'description',
309 nl2br($this->getDescription($receipt, $donationForm))
310 );
311
312 $receipt->settings->addSetting('currency', $receipt->donation->amount->getCurrency()->getCode());
313 $receipt->settings->addSetting('donorDashboardUrl', get_permalink(give_get_option('donor_dashboard_page')));
314
315 $receipt->settings->addSetting(
316 'pdfReceiptLink',
317 apply_filters('givewp_confirmation_page_receipt_settings_pdfReceiptLink', '', $receipt)
318 );
319 }
320
321 /**
322 * @since 3.0.0
323 *
324 * @param int $formId
325 * @return DonationForm|null
326 */
327 protected function getDonationForm(int $formId)
328 {
329 if (give(DonationFormRepository::class)->isLegacyForm($formId)) {
330 return null;
331 }
332
333 return DonationForm::find($formId);
334 }
335
336 /**
337 * @since 4.17.0 Declare the nullable parameter explicitly.
338 * @since 4.16.8 Apply legacy tags before donor-supplied V3 tag values.
339 * @since 3.0.0
340 */
341 protected function getHeading(DonationReceipt $receipt, ?DonationForm $donationForm = null): string
342 {
343 if (!$donationForm) {
344 $content = __("Hey {first_name}, thanks for your donation!", 'give');
345 } else {
346 $content = $donationForm->settings->receiptHeading;
347 }
348
349 return (new DonationTemplateTags(
350 $receipt->donation,
351 $this->transformV2FormTags($content, $receipt->donation)
352 ))->getContent();
353 }
354
355 /**
356 * @since 4.17.0 Declare the nullable parameter explicitly.
357 * @since 4.16.8 Apply legacy tags before donor-supplied V3 tag values.
358 * @since 3.0.0
359 */
360 protected function getDescription(DonationReceipt $receipt, ?DonationForm $donationForm = null): string
361 {
362 if (!$donationForm) {
363 $content = __(
364 "{first_name}, your contribution means a lot and will be put to good use in making a difference. We’ve sent your donation receipt to {email}.",
365 'give'
366 );
367 } else {
368 $content = $donationForm->settings->receiptDescription;
369 }
370
371 return (new DonationTemplateTags(
372 $receipt->donation,
373 $this->transformV2FormTags($content, $receipt->donation)
374 ))->getContent();
375 }
376
377 /**
378 * @since 3.0.0
379 */
380 protected function transformV2FormTags(string $content, Donation $donation): string
381 {
382 return give_do_email_tags($content, ['payment_id' => $donation->id, 'form_id' => $donation->formId]
383 );
384 }
385
386 /**
387 * @since 3.6.0
388 */
389 private function fillEventTicketsDetails(DonationReceipt $receipt): void
390 {
391 do_action('givewp_generate_confirmation_page_receipt_fill_event_ticket_details', $receipt);
392 }
393 }
394