| 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 |
* @var string|null |
| 133 |
*/ |
| 134 |
public $levelId; |
| 135 |
|
| 136 |
/** |
| 137 |
* @since 4.16.5 Added levelId property |
| 138 |
* @since 3.9.0 Added phone property |
| 139 |
* @since 3.2.0 added honorific property |
| 140 |
* @since 3.0.0 |
| 141 |
*/ |
| 142 |
public function toDonation(int $donorId): Donation |
| 143 |
{ |
| 144 |
$form = $this->getDonationForm(); |
| 145 |
|
| 146 |
return new Donation([ |
| 147 |
'status' => DonationStatus::PENDING(), |
| 148 |
'gatewayId' => $this->gatewayId, |
| 149 |
'amount' => $this->amount(), |
| 150 |
'anonymous' => $this->anonymous, |
| 151 |
'donorId' => $donorId, |
| 152 |
'honorific' => $this->honorific, |
| 153 |
'firstName' => $this->firstName, |
| 154 |
'lastName' => $this->lastName, |
| 155 |
'email' => $this->email, |
| 156 |
'phone' => $this->phone, |
| 157 |
'formId' => $this->formId, |
| 158 |
'formTitle' => $form->title, |
| 159 |
'company' => $this->company, |
| 160 |
'comment' => $this->comment, |
| 161 |
'type' => DonationType::SINGLE(), |
| 162 |
'billingAddress' => $this->getBillingAddress(), |
| 163 |
'levelId' => $this->levelId, |
| 164 |
]); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @since 3.9.0 Added phone property |
| 169 |
* @since 3.0.0 |
| 170 |
*/ |
| 171 |
public function toInitialSubscriptionDonation(int $donorId, int $subscriptionId): Donation |
| 172 |
{ |
| 173 |
$form = $this->getDonationForm(); |
| 174 |
|
| 175 |
return new Donation([ |
| 176 |
'status' => DonationStatus::PENDING(), |
| 177 |
'gatewayId' => $this->gatewayId, |
| 178 |
'amount' => $this->amount(), |
| 179 |
'anonymous' => $this->anonymous, |
| 180 |
'donorId' => $donorId, |
| 181 |
'firstName' => $this->firstName, |
| 182 |
'lastName' => $this->lastName, |
| 183 |
'email' => $this->email, |
| 184 |
'phone' => $this->phone, |
| 185 |
'formId' => $this->formId, |
| 186 |
'formTitle' => $form->title, |
| 187 |
'company' => $this->company, |
| 188 |
'comment' => $this->comment, |
| 189 |
'type' => DonationType::SUBSCRIPTION(), |
| 190 |
'subscriptionId' => $subscriptionId, |
| 191 |
'billingAddress' => $this->getBillingAddress(), |
| 192 |
'levelId' => $this->levelId, |
| 193 |
]); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* @since 3.16.0 Added "givewp_donation_confirmation_page_redirect_enabled" filter |
| 198 |
* @since 3.0.0 |
| 199 |
*/ |
| 200 |
public function getSuccessUrl(Donation $donation): string |
| 201 |
{ |
| 202 |
$form = $this->getDonationForm(); |
| 203 |
|
| 204 |
if (apply_filters('givewp_donation_confirmation_page_redirect_enabled', $form->settings->enableReceiptConfirmationPage, $donation->formId)) { |
| 205 |
return $this->getDonationConfirmationPageFromSettings($donation); |
| 206 |
} |
| 207 |
|
| 208 |
return $this->isEmbed ? |
| 209 |
$this->getDonationConfirmationReceiptUrl($donation) : |
| 210 |
$this->getDonationConfirmationReceiptViewRouteUrl($donation); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* @since 3.0.0 |
| 215 |
* |
| 216 |
* TODO: add params to route for flash message |
| 217 |
*/ |
| 218 |
public function getCancelUrl(): string |
| 219 |
{ |
| 220 |
return $this->originUrl; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* @since 3.0.0 |
| 225 |
*/ |
| 226 |
public function getDonationConfirmationReceiptViewRouteUrl(Donation $donation): string |
| 227 |
{ |
| 228 |
return (new GenerateDonationConfirmationReceiptViewRouteUrl())($donation->purchaseKey); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @since 3.0.0 |
| 233 |
*/ |
| 234 |
public function getDonationConfirmationReceiptUrl(Donation $donation): string |
| 235 |
{ |
| 236 |
return (new GenerateDonationConfirmationReceiptUrl())($donation, $this->originUrl, $this->embedId); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* @since 3.16.0 |
| 241 |
*/ |
| 242 |
public function getDonationConfirmationPageFromSettings(Donation $donation): string |
| 243 |
{ |
| 244 |
$settings = give_get_settings(); |
| 245 |
|
| 246 |
$page = isset($settings['success_page']) |
| 247 |
? get_permalink(absint($settings['success_page'])) |
| 248 |
: get_bloginfo('url'); |
| 249 |
|
| 250 |
$page = apply_filters('givewp_donation_confirmation_page_redirect_permalink', $page, $donation->formId); |
| 251 |
|
| 252 |
return esc_url_raw(add_query_arg(['receipt-id' => $donation->purchaseKey], $page)); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* @since 3.0.0 |
| 257 |
*/ |
| 258 |
public function getDonationForm(): DonationForm |
| 259 |
{ |
| 260 |
return DonationForm::find($this->formId); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* This is a hard-coded way of filtering through our dynamic properties |
| 265 |
* and only returning custom fields. |
| 266 |
* |
| 267 |
* TODO: figure out a less static way of doing this |
| 268 |
* |
| 269 |
* @since 3.0.0 |
| 270 |
*/ |
| 271 |
public function getCustomFields(): array |
| 272 |
{ |
| 273 |
$properties = get_object_vars($this); |
| 274 |
|
| 275 |
return array_filter($properties, static function ($param) { |
| 276 |
return !in_array( |
| 277 |
$param, |
| 278 |
array_merge( |
| 279 |
Donation::propertyKeys(), |
| 280 |
Subscription::propertyKeys(), |
| 281 |
[ |
| 282 |
'currency', |
| 283 |
'wpUserId', |
| 284 |
'honorific', |
| 285 |
'originUrl', |
| 286 |
'isEmbed', |
| 287 |
'embedId', |
| 288 |
'donationType', |
| 289 |
'subscriptionPeriod', |
| 290 |
'subscriptionFrequency', |
| 291 |
'subscriptionInstallments', |
| 292 |
'country', |
| 293 |
'address1', |
| 294 |
'address2', |
| 295 |
'city', |
| 296 |
'state', |
| 297 |
'zip', |
| 298 |
] |
| 299 |
), |
| 300 |
true |
| 301 |
); |
| 302 |
}, ARRAY_FILTER_USE_KEY); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* @since 3.0.0 |
| 307 |
* |
| 308 |
* @throws Exception |
| 309 |
*/ |
| 310 |
public function toSubscription(int $donorId): Subscription |
| 311 |
{ |
| 312 |
return new Subscription([ |
| 313 |
'amount' => $this->amount(), |
| 314 |
'period' => $this->subscriptionPeriod, |
| 315 |
'frequency' => (int)$this->subscriptionFrequency, |
| 316 |
'donorId' => $donorId, |
| 317 |
'installments' => (int)$this->subscriptionInstallments, |
| 318 |
'status' => SubscriptionStatus::PENDING(), |
| 319 |
'mode' => give_is_test_mode() ? SubscriptionMode::TEST() : SubscriptionMode::LIVE(), |
| 320 |
'donationFormId' => $this->formId, |
| 321 |
]); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* @return Money |
| 326 |
*/ |
| 327 |
public function amount(): Money |
| 328 |
{ |
| 329 |
return Money::fromDecimal($this->amount, $this->currency); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* @since 3.0.0 |
| 334 |
*/ |
| 335 |
public function getGateway(): PaymentGateway |
| 336 |
{ |
| 337 |
return give(PaymentGatewayRegister::class)->getPaymentGateway($this->gatewayId); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* @since 3.0.0 |
| 342 |
*/ |
| 343 |
public function getBillingAddress(): BillingAddress |
| 344 |
{ |
| 345 |
return BillingAddress::fromArray([ |
| 346 |
'country' => $this->country, |
| 347 |
'address1' => $this->address1, |
| 348 |
'address2' => $this->address2, |
| 349 |
'city' => $this->city, |
| 350 |
'state' => $this->state, |
| 351 |
'zip' => $this->zip, |
| 352 |
]); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* @since 3.0.0 |
| 357 |
*/ |
| 358 |
public function has(string $name): bool |
| 359 |
{ |
| 360 |
return isset($this->{$name}); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* @since 3.0.0 |
| 365 |
* |
| 366 |
* @return mixed|null |
| 367 |
*/ |
| 368 |
public function get(string $name) |
| 369 |
{ |
| 370 |
return property_exists($this, $name) ? $this->{$name} : null; |
| 371 |
} |
| 372 |
} |
| 373 |
|