Skip to content

Commit

Permalink
Feature: update backwards compatibility for donation level id (#7002)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Waldstein <[email protected]>
  • Loading branch information
jonwaldstein and Jon Waldstein authored Oct 9, 2023
1 parent b78d910 commit f6f2f4d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<?php

namespace Give\DonationForms\Actions;

use Give\DonationForms\DataTransferObjects\DonateControllerData;
use Give\DonationForms\Listeners\AddRedirectUrlsToGatewayData;
use Give\DonationForms\Listeners\StoreCustomFields;
use Give\DonationForms\Listeners\TemporarilyReplaceLegacySuccessPageUri;
use Give\DonationForms\Listeners\UpdateDonationLevelId;
use Give\Donations\Models\Donation;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\FieldsAPI\Exceptions\NameCollisionException;
use Give\Subscriptions\Models\Subscription;

class DispatchDonateControllerDonationCreatedListeners
{
/**
* @since 3.0.0
* @throws NameCollisionException
* @throws NameCollisionException|Exception
*/
public function __invoke(DonateControllerData $formData, Donation $donation, ?Subscription $subscription)
{
(new StoreCustomFields())($formData->getDonationForm(), $donation, $subscription, $formData->getCustomFields());
(new TemporarilyReplaceLegacySuccessPageUri())($formData, $donation);
(new AddRedirectUrlsToGatewayData())($formData, $donation);
(new UpdateDonationLevelId())($formData->getDonationForm(), $donation);
}
}
42 changes: 42 additions & 0 deletions src/DonationForms/Listeners/UpdateDonationLevelId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Give\DonationForms\Listeners;

use Give\DonationForms\Models\DonationForm;
use Give\Donations\Models\Donation;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\FieldsAPI\Amount;
use Give\Framework\FieldsAPI\Exceptions\NameCollisionException;

class UpdateDonationLevelId
{
/**
* if the intended donation amount matches a donation level from the amount block settings,
* this will update the donation level ID meta with the level array key,
* which is used in the donation details screen.
*
* @unreleased
*
* @throws NameCollisionException|Exception
*/
public function __invoke(DonationForm $donationForm, Donation $donation)
{
/** @var Amount $amountField */
$amountField = $donationForm->schema()->getNodeByName('amount');

if (!$amountField) {
return;
}

$donationLevel = array_search(
(float)$donation->intendedAmount()->formatToDecimal(),
$amountField->getLevels(),
true
);

if ($donationLevel !== false) {
$donation->levelId = (string)$donationLevel;
$donation->save();
}
}
}

0 comments on commit f6f2f4d

Please sign in to comment.