| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Receipt; |
| 4 |
|
| 5 |
use Give\Donations\ValueObjects\DonationStatus; |
| 6 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 7 |
use Give_Payment; |
| 8 |
|
| 9 |
class DonationReceipt extends Receipt |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Receipt donor section id. |
| 13 |
*/ |
| 14 |
const DONORSECTIONID = 'Donor'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Receipt donation section id. |
| 18 |
*/ |
| 19 |
const DONATIONSECTIONID = 'Donation'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Receipt additional information section id. |
| 23 |
*/ |
| 24 |
const ADDITIONALINFORMATIONSECTIONID = 'AdditionalInformation'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Donation id. |
| 28 |
* |
| 29 |
* @since 2.7.0 |
| 30 |
* @var int $donationId |
| 31 |
*/ |
| 32 |
public $donationId; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var Give_Payment |
| 36 |
* @since 2.18.0 |
| 37 |
*/ |
| 38 |
protected $donation; |
| 39 |
|
| 40 |
/** |
| 41 |
* Receipt constructor. |
| 42 |
* |
| 43 |
* @param $donationId |
| 44 |
* |
| 45 |
* @since 2.7.0 |
| 46 |
*/ |
| 47 |
public function __construct($donationId) |
| 48 |
{ |
| 49 |
$this->donationId = $donationId; |
| 50 |
$this->donation = new Give_Payment($donationId); |
| 51 |
|
| 52 |
$this->addDonorSection(); |
| 53 |
$this->addDonationSection(); |
| 54 |
$this->addAdditionalInformationSection(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Add donor section. |
| 59 |
* |
| 60 |
* @since 2.7.0 |
| 61 |
*/ |
| 62 |
private function addDonorSection() |
| 63 |
{ |
| 64 |
$donorSection = $this->addSection([ |
| 65 |
'id' => self::DONORSECTIONID, |
| 66 |
'label' => esc_html__('Donor Details', 'give'), |
| 67 |
]); |
| 68 |
|
| 69 |
$donorSection->addLineItem([ |
| 70 |
'id' => 'fullName', |
| 71 |
'label' => esc_html__('Donor Name', 'give'), |
| 72 |
'value' => trim("{$this->donation->first_name} {$this->donation->last_name}"), |
| 73 |
'icon' => '<i class="fas fa-user"></i>', |
| 74 |
]); |
| 75 |
|
| 76 |
$donorSection->addLineItem([ |
| 77 |
'id' => 'emailAddress', |
| 78 |
'label' => esc_html__('Email Address', 'give'), |
| 79 |
'value' => $this->donation->email, |
| 80 |
'icon' => '<i class="fas fa-envelope"></i>', |
| 81 |
]); |
| 82 |
|
| 83 |
if ($address = $this->getDonorBillingAddress()) { |
| 84 |
$donorSection->addLineItem([ |
| 85 |
'id' => 'billingAddress', |
| 86 |
'label' => esc_html__('Billing Address', 'give'), |
| 87 |
'value' => $address, |
| 88 |
'icon' => '<i class="fas fa-globe-americas"></i>', |
| 89 |
]); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add donation section. |
| 95 |
* |
| 96 |
* @since 2.24.0 Add notice for donations with the "processing" status |
| 97 |
* @since 2.7.0 |
| 98 |
*/ |
| 99 |
private function addDonationSection() |
| 100 |
{ |
| 101 |
$donationSection = $this->addSection([ |
| 102 |
'id' => self::DONATIONSECTIONID, |
| 103 |
'label' => esc_html__('Donation Details', 'give'), |
| 104 |
]); |
| 105 |
|
| 106 |
$donationSection->addLineItem([ |
| 107 |
'id' => 'paymentStatus', |
| 108 |
'label' => esc_html__('Payment Status', 'give'), |
| 109 |
'value' => give_get_payment_statuses()[$this->donation->post_status], |
| 110 |
]); |
| 111 |
|
| 112 |
if (DonationStatus::PROCESSING()->getValue() === $this->donation->post_status) { |
| 113 |
$donationSection->addLineItem([ |
| 114 |
'id' => 'processingStatusNotice', |
| 115 |
'label' => '<div style="text-transform:initial;font-size:0.73rem;color:#3398DB;display:flex;flex-flow:row nowrap;white-space:pre-wrap;margin-top:-0.5rem;">' . |
| 116 |
esc_html__('You will receive a final receipt in your email once it has been completed.', |
| 117 |
'give') . |
| 118 |
'</div>', |
| 119 |
'value' => '⠀', |
| 120 |
]); |
| 121 |
} |
| 122 |
|
| 123 |
$donationSection->addLineItem([ |
| 124 |
'id' => 'paymentMethod', |
| 125 |
'label' => esc_html__('Payment Method', 'give'), |
| 126 |
'value' => give_get_gateway_checkout_label($this->donation->gateway), |
| 127 |
]); |
| 128 |
|
| 129 |
$donationSection->addLineItem([ |
| 130 |
'id' => 'amount', |
| 131 |
'label' => esc_html__('Donation Amount', 'give'), |
| 132 |
'value' => give_currency_filter( |
| 133 |
give_format_amount($this->donation->total, ['donation_id' => $this->donation->ID]), |
| 134 |
[ |
| 135 |
'currency_code' => $this->donation->currency, |
| 136 |
'form_id' => $this->donation->form_id, |
| 137 |
'decode_currency' => true, |
| 138 |
] |
| 139 |
), |
| 140 |
]); |
| 141 |
|
| 142 |
$donationSection->addLineItem([ |
| 143 |
'id' => 'totalAmount', |
| 144 |
'label' => esc_html__('Donation Total', 'give'), |
| 145 |
'value' => give_currency_filter( |
| 146 |
give_format_amount($this->donation->total, ['donation_id' => $this->donation->ID]), |
| 147 |
[ |
| 148 |
'currency_code' => $this->donation->currency, |
| 149 |
'form_id' => $this->donation->form_id, |
| 150 |
'decode_currency' => true, |
| 151 |
] |
| 152 |
), |
| 153 |
]); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Add Additional Information Section |
| 158 |
*/ |
| 159 |
private function addAdditionalInformationSection() |
| 160 |
{ |
| 161 |
$this->addSection([ |
| 162 |
'id' => self::ADDITIONALINFORMATIONSECTIONID, |
| 163 |
'label' => esc_html__('Additional Information', 'give'), |
| 164 |
]); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get donor billing address |
| 169 |
* |
| 170 |
* @return string|null |
| 171 |
*/ |
| 172 |
private function getDonorBillingAddress() |
| 173 |
{ |
| 174 |
$address = give_get_donation_address($this->donationId); |
| 175 |
$formatted = sprintf( |
| 176 |
'%1$s%7$s%2$s%3$s, %4$s%5$s%7$s%6$s', |
| 177 |
$address['line1'], |
| 178 |
! empty($address['line2']) ? $address['line2'] . "\r\n" : '', |
| 179 |
$address['city'], |
| 180 |
$address['state'], |
| 181 |
$address['zip'], |
| 182 |
$address['country'], |
| 183 |
"\r\n" |
| 184 |
); |
| 185 |
|
| 186 |
$hasAddress = (bool) trim(str_replace(',', '', strip_tags($formatted))); |
| 187 |
|
| 188 |
if ($hasAddress) { |
| 189 |
return $formatted; |
| 190 |
} |
| 191 |
|
| 192 |
return null; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Set iterator position to zero when rewind. |
| 197 |
* |
| 198 |
* @since 2.7.0 |
| 199 |
*/ |
| 200 |
public function rewind() |
| 201 |
{ |
| 202 |
$this->position = 0; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Validate section. |
| 207 |
* |
| 208 |
* @param array $array |
| 209 |
* |
| 210 |
* @since 2.7.0 |
| 211 |
*/ |
| 212 |
protected function validateSection($array) |
| 213 |
{ |
| 214 |
$required = [ 'id' ]; |
| 215 |
$array = array_filter($array); // Remove empty values. |
| 216 |
|
| 217 |
if (array_diff($required, array_keys($array))) { |
| 218 |
throw new InvalidArgumentException(esc_html__('Invalid receipt section. Please provide valid section id', 'give')); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|