Skip to content

Commit

Permalink
feature: add revenue compatability with v3 forms and donation model
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Dec 14, 2023
1 parent 584621b commit 599d8e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
11 changes: 5 additions & 6 deletions src/Revenue/Listeners/UpdateRevenueWhenDonationAmountUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
class UpdateRevenueWhenDonationAmountUpdated
{
/**
* @unreleased updated to accept Donation model
* @since 2.22.1
*
* @param int $donationId The ID of the Donation.
*/
public function __invoke($donationId)
public function __invoke(Donation $donation)
{
give(Revenue::class)->updateRevenueAmount(
Donation::find($donationId)
);
if ($donation->isDirty('amount')) {
give(Revenue::class)->updateRevenueAmount($donation);
}
}
}
12 changes: 11 additions & 1 deletion src/Revenue/RevenueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Give\Revenue;

use Give\Donations\Models\Donation;
use Give\Framework\Migrations\MigrationsRegister;
use Give\Helpers\Hooks;
use Give\Revenue\Listeners\DeleteRevenueWhenDonationDeleted;
use Give\Revenue\Listeners\UpdateRevenueWhenDonationAmountUpdated;
use Give\Revenue\Migrations\AddPastDonationsToRevenueTable;
use Give\Revenue\Migrations\CreateRevenueTable;
use Give\Revenue\Migrations\RemoveRevenueForeignKeys;
use Give\Revenue\Repositories\Revenue;
use Give\ServiceProviders\ServiceProvider;

class RevenueServiceProvider implements ServiceProvider
Expand All @@ -28,6 +30,7 @@ public function register()
/**
* @inheritDoc
*
* @unreleased added support for givewp_donation_updated and updated give_updated_edited_donation implementation
* @since 2.9.0
*/
public function boot()
Expand All @@ -37,7 +40,14 @@ public function boot()
Hooks::addAction('delete_post', DeleteRevenueWhenDonationDeleted::class, '__invoke', 10, 1);
Hooks::addAction('give_insert_payment', DonationHandler::class, 'handle', 999, 1);
Hooks::addAction('give_register_updates', AddPastDonationsToRevenueTable::class, 'register', 10, 1);
Hooks::addAction('give_updated_edited_donation', UpdateRevenueWhenDonationAmountUpdated::class);
Hooks::addAction('givewp_donation_updated', UpdateRevenueWhenDonationAmountUpdated::class);
add_action('give_updated_edited_donation', static function($donationId) {
$donation = Donation::find($donationId);

if ($donation){
(new UpdateRevenueWhenDonationAmountUpdated)($donation);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace Give\Tests\Unit\Revenue\Listeners;

use Give\Donations\Models\Donation;
use Give\Donations\ValueObjects\DonationMode;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Donors\Models\Donor;
use Give\Framework\Database\DB;
use Give\Framework\Support\ValueObjects\Money;
use Give\PaymentGateways\Gateways\TestGateway\TestGateway;
use Give\Revenue\Listeners\UpdateRevenueWhenDonationAmountUpdated;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
Expand All @@ -21,6 +18,7 @@ class UpdateRevenueWhenDonationAmountUpdatedTest extends TestCase
use RefreshDatabase;

/**
* @unreleased updated action to accept Donation model
* @since 2.20.1
*/
public function testRevenueIsUpdatedWhenDonationIsUpdated()
Expand All @@ -34,7 +32,7 @@ public function testRevenueIsUpdatedWhenDonationIsUpdated()
$donation->save();

$listener = new UpdateRevenueWhenDonationAmountUpdated();
$listener->__invoke($donation->id);
$listener->__invoke($donation);

$this->assertEquals(
Money::fromDecimal(25.00, 'USD')->formatToMinorAmount(),
Expand All @@ -43,7 +41,7 @@ public function testRevenueIsUpdatedWhenDonationIsUpdated()
}

/**
* @param Donation $donation
* @param Donation $donation
*
* @return int
*/
Expand Down

0 comments on commit 599d8e9

Please sign in to comment.