Donation.php
293 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations\Models; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Exception; |
| 7 | use Give\Donations\DataTransferObjects\DonationQueryData; |
| 8 | use Give\Donations\Factories\DonationFactory; |
| 9 | use Give\Donations\Properties\BillingAddress; |
| 10 | use Give\Donations\ValueObjects\DonationMode; |
| 11 | use Give\Donations\ValueObjects\DonationStatus; |
| 12 | use Give\Donors\Models\Donor; |
| 13 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 14 | use Give\Framework\Models\Contracts\ModelCrud; |
| 15 | use Give\Framework\Models\Contracts\ModelHasFactory; |
| 16 | use Give\Framework\Models\Model; |
| 17 | use Give\Framework\Models\ModelQueryBuilder; |
| 18 | use Give\Framework\Models\ValueObjects\Relationship; |
| 19 | use Give\Framework\PaymentGateways\PaymentGateway; |
| 20 | use Give\Framework\Support\ValueObjects\Money; |
| 21 | use Give\Subscriptions\Models\Subscription; |
| 22 | |
| 23 | /** |
| 24 | * Class Donation |
| 25 | * |
| 26 | * @since 2.20.0 update amount type, fee recovered, and exchange rate |
| 27 | * @since 2.19.6 |
| 28 | * |
| 29 | * @property int $id |
| 30 | * @property int $formId |
| 31 | * @property string $formTitle |
| 32 | * @property DateTime $createdAt |
| 33 | * @property DateTime $updatedAt |
| 34 | * @property DonationStatus $status |
| 35 | * @property DonationMode $mode |
| 36 | * @property Money $amount amount charged to the gateway |
| 37 | * @property Money $feeAmountRecovered |
| 38 | * @property string $exchangeRate |
| 39 | * @property string $gatewayId |
| 40 | * @property int $donorId |
| 41 | * @property string $firstName |
| 42 | * @property string $lastName |
| 43 | * @property string $email |
| 44 | * @property int $parentId |
| 45 | * @property int $subscriptionId |
| 46 | * @property BillingAddress $billingAddress |
| 47 | * @property string $purchaseKey |
| 48 | * @property string $donorIp |
| 49 | * @property bool $anonymous |
| 50 | * @property int $levelId |
| 51 | * @property string $gatewayTransactionId |
| 52 | * @property Donor $donor |
| 53 | * @property Subscription $subscription |
| 54 | */ |
| 55 | class Donation extends Model implements ModelCrud, ModelHasFactory |
| 56 | { |
| 57 | /** |
| 58 | * @inheritdoc |
| 59 | */ |
| 60 | protected $properties = [ |
| 61 | 'id' => 'int', |
| 62 | 'formId' => 'int', |
| 63 | 'formTitle' => 'string', |
| 64 | 'purchaseKey' => 'string', |
| 65 | 'donorIp' => 'string', |
| 66 | 'createdAt' => DateTime::class, |
| 67 | 'updatedAt' => DateTime::class, |
| 68 | 'status' => DonationStatus::class, |
| 69 | 'mode' => DonationMode::class, |
| 70 | 'amount' => Money::class, |
| 71 | 'feeAmountRecovered' => Money::class, |
| 72 | 'exchangeRate' => ['string', '1'], |
| 73 | 'gatewayId' => 'string', |
| 74 | 'donorId' => 'int', |
| 75 | 'firstName' => 'string', |
| 76 | 'lastName' => 'string', |
| 77 | 'email' => 'string', |
| 78 | 'parentId' => ['int', 0], |
| 79 | 'subscriptionId' => ['int', 0], |
| 80 | 'billingAddress' => BillingAddress::class, |
| 81 | 'anonymous' => ['bool', false], |
| 82 | 'levelId' => ['int', 0], |
| 83 | 'gatewayTransactionId' => 'string', |
| 84 | ]; |
| 85 | |
| 86 | /** |
| 87 | * @inheritdoc |
| 88 | */ |
| 89 | protected $relationships = [ |
| 90 | 'donor' => Relationship::BELONGS_TO, |
| 91 | 'subscription' => Relationship::BELONGS_TO, |
| 92 | ]; |
| 93 | |
| 94 | /** |
| 95 | * Find donation by ID |
| 96 | * |
| 97 | * @since 2.19.6 |
| 98 | * |
| 99 | * @param int $id |
| 100 | * |
| 101 | * @return Donation |
| 102 | */ |
| 103 | public static function find($id) |
| 104 | { |
| 105 | return give()->donations->getById($id); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @since 2.20.0 return mutated model instance |
| 110 | * @since 2.19.6 |
| 111 | * |
| 112 | * @param array $attributes |
| 113 | * |
| 114 | * @return Donation |
| 115 | * |
| 116 | * @throws Exception|InvalidArgumentException |
| 117 | */ |
| 118 | public static function create(array $attributes) |
| 119 | { |
| 120 | $donation = new static($attributes); |
| 121 | |
| 122 | give()->donations->insert($donation); |
| 123 | |
| 124 | return $donation; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @since 2.20.0 mutate model in repository and return void |
| 129 | * @since 2.19.6 |
| 130 | * |
| 131 | * @return void |
| 132 | * |
| 133 | * @throws Exception|InvalidArgumentException |
| 134 | */ |
| 135 | public function save() |
| 136 | { |
| 137 | if (!$this->id) { |
| 138 | give()->donations->insert($this); |
| 139 | } else { |
| 140 | give()->donations->update($this); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @since 2.19.6 |
| 146 | * |
| 147 | * @return bool |
| 148 | * |
| 149 | * @throws Exception|InvalidArgumentException |
| 150 | */ |
| 151 | public function delete() |
| 152 | { |
| 153 | return give()->donations->delete($this); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @since 2.19.6 |
| 158 | * |
| 159 | * @return ModelQueryBuilder<Donor> |
| 160 | */ |
| 161 | public function donor() |
| 162 | { |
| 163 | return give()->donors->queryById($this->donorId); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @since 2.19.6 |
| 168 | * |
| 169 | * @return ModelQueryBuilder<Subscription> |
| 170 | */ |
| 171 | public function subscription() |
| 172 | { |
| 173 | if ($this->subscriptionId) { |
| 174 | return give()->subscriptions->queryById($this->subscriptionId); |
| 175 | } |
| 176 | |
| 177 | return give()->subscriptions->queryByDonationId($this->id); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @since 2.19.6 |
| 182 | * |
| 183 | * @return int|null |
| 184 | */ |
| 185 | public function getSequentialId() |
| 186 | { |
| 187 | return give()->donations->getSequentialId($this->id); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @since 2.19.6 |
| 192 | * |
| 193 | * @return object[] |
| 194 | */ |
| 195 | public function getNotes() |
| 196 | { |
| 197 | return give()->donations->getNotesByDonationId($this->id); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns the amount charged in the currency the GiveWP site is set to |
| 202 | * |
| 203 | * @since 2.20.0 |
| 204 | * |
| 205 | * @return Money |
| 206 | */ |
| 207 | public function amountInBaseCurrency() |
| 208 | { |
| 209 | return $this->amount->inBaseCurrency($this->exchangeRate); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns the donation amount the donor "intended", which means it is the amount without recovered fees. So if the |
| 214 | * donor paid $100, but the donation was charged $105 with a $5 fee, this method will return $100. |
| 215 | * |
| 216 | * @since 2.20.0 |
| 217 | * |
| 218 | * @return Money |
| 219 | */ |
| 220 | public function intendedAmount() |
| 221 | { |
| 222 | return $this->feeAmountRecovered === null |
| 223 | ? $this->amount |
| 224 | : $this->amount->subtract($this->feeAmountRecovered); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Returns the amount intended in the currency the GiveWP site is set to |
| 229 | * |
| 230 | * @since 2.20.0 |
| 231 | * |
| 232 | * @return Money |
| 233 | */ |
| 234 | public function intendedAmountInBaseCurrency() |
| 235 | { |
| 236 | return $this->intendedAmount()->inBaseCurrency($this->exchangeRate); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns the gateway instance for this donation |
| 241 | * |
| 242 | * @since 2.20.0 |
| 243 | */ |
| 244 | public function gateway(): PaymentGateway |
| 245 | { |
| 246 | return give()->gateways->getPaymentGateway($this->gatewayId); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @since 2.20.0 |
| 251 | * |
| 252 | * @inheritDoc |
| 253 | */ |
| 254 | protected function getPropertyDefaults() |
| 255 | { |
| 256 | return array_merge(parent::getPropertyDefaults(), [ |
| 257 | 'mode' => give_is_test_mode() ? DonationMode::TEST() : DonationMode::LIVE(), |
| 258 | 'donorIp' => give_get_ip(), |
| 259 | 'billingAddress' => new BillingAddress(), |
| 260 | ]); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @since 2.19.6 |
| 265 | * |
| 266 | * @return ModelQueryBuilder<Donation> |
| 267 | */ |
| 268 | public static function query() |
| 269 | { |
| 270 | return give()->donations->prepareQuery(); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @since 2.19.6 |
| 275 | * |
| 276 | * @param object $object |
| 277 | * |
| 278 | * @return Donation |
| 279 | */ |
| 280 | public static function fromQueryBuilderObject($object) |
| 281 | { |
| 282 | return DonationQueryData::fromObject($object)->toDonation(); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @return DonationFactory<Donation> |
| 287 | */ |
| 288 | public static function factory() |
| 289 | { |
| 290 | return new DonationFactory(static::class); |
| 291 | } |
| 292 | } |
| 293 |