Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add revenue compatibility for v3 forms and donation model #7148

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Give\Revenue\LegacyListeners;

use Give\Donations\Models\Donation;
use Give\Revenue\Repositories\Revenue;

/**
* @unreleased
*/
class UpdateRevenueWhenDonationAmountUpdated
{
/**
* @unreleased
*/
public function __invoke(int $donationId)
{
$donation = Donation::find($donationId);

if ($donation){
give(Revenue::class)->updateRevenueAmount($donation);
}
}
}
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);
}
}
}
6 changes: 5 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,8 @@ 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);
Hooks::addAction('give_updated_edited_donation',LegacyListeners\UpdateRevenueWhenDonationAmountUpdated::class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Give\Tests\Unit\Revenue\LegacyListeners;

use Give\Donations\Models\Donation;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Framework\Database\DB;
use Give\Framework\Support\ValueObjects\Money;
use Give\Revenue\LegacyListeners\UpdateRevenueWhenDonationAmountUpdated;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;

/**
* @unreleased
*/
class UpdateRevenueWhenDonationAmountUpdatedTest extends TestCase
jonwaldstein marked this conversation as resolved.
Show resolved Hide resolved
{
use RefreshDatabase;

/**
* @unreleased
*/
public function testRevenueIsUpdatedWhenDonationIsUpdated(): void
{
$donation = Donation::factory()->create([
'status' => DonationStatus::COMPLETE(),
'amount' => Money::fromDecimal(250.00, 'USD'),
]);

$donation->amount = Money::fromDecimal(25.00, 'USD');
$donation->save();

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

$this->assertEquals(
Money::fromDecimal(25.00, 'USD')->formatToMinorAmount(),
$this->getRevenueAmountForDonation($donation)
);
}

/**
* @unreleased
*/
private function getRevenueAmountForDonation(Donation $donation)
{
global $wpdb;
$revenue = DB::get_row("SELECT * FROM {$wpdb->give_revenue} WHERE donation_id = {$donation->id}");

return $revenue->amount;
}
}

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,9 +18,10 @@ class UpdateRevenueWhenDonationAmountUpdatedTest extends TestCase
use RefreshDatabase;

/**
* @unreleased updated action to accept Donation model
* @since 2.20.1
*/
public function testRevenueIsUpdatedWhenDonationIsUpdated()
public function testRevenueIsUpdatedWhenDonationIsUpdated(): void
{
$donation = Donation::factory()->create([
'status' => DonationStatus::COMPLETE(),
Expand All @@ -34,7 +32,7 @@ public function testRevenueIsUpdatedWhenDonationIsUpdated()
$donation->save();

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

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

/**
* @param Donation $donation
*
* @return int
* @since 2.20.1
*/
private function getRevenueAmountForDonation(Donation $donation)
{
Expand Down
Loading