Skip to content

Commit

Permalink
refactor: update decorator to use model methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Jan 24, 2025
1 parent 2ce51ba commit fbc9ba8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
namespace Give\PaymentGateways\Gateways\Stripe\StripePaymentElementGateway\Webhooks\Decorators;

use Exception;
use Give\Donations\Models\Donation;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Donations\ValueObjects\DonationType;
use Give\Framework\Support\Facades\DateTime\Temporal;
use Give\Framework\Support\ValueObjects\Money;
use Give\Subscriptions\Models\Subscription;
Expand All @@ -26,65 +23,41 @@ public function __construct(Subscription $subscription)
}

/**
* @unreleased updated to use model method
* @since 3.0.0
*/
public function shouldEndSubscription(): bool
{
return $this->subscription->installments !== 0 && (count(
$this->subscription->donations
) >= $this->subscription->installments);
return $this->subscription->shouldEndSubscription();
}

/**
* @unreleased updated to use model method
* @since 3.0.0
*/
public function shouldCreateRenewal(): bool
{
$billTimes = $this->subscription->installments;
$totalPayments = count($this->subscription->donations);

return $this->subscription->status->isActive() && (0 === $billTimes || $totalPayments < $billTimes);
return $this->subscription->shouldCreateRenewal();
}

/**
* @unreleased updated to use model method
* @since 3.0.0
*
* @throws Exception
*/
public function handleRenewal(Invoice $invoice): SubscriptionModelDecorator
{
$initialDonation = $this->subscription->initialDonation();

// create renewal
Donation::create([
$this->subscription->createRenewal([
'amount' => Money::fromDecimal(
give_stripe_cents_to_dollars($invoice->total),
strtoupper($invoice->currency)
),
/**
* TODO: the payment_intent.succeeded event is going to try setting this status as complete
*/
'type' => DonationType::RENEWAL(),
'status' => DonationStatus::COMPLETE(),
'createdAt' => Temporal::toDateTime(date_i18n('Y-m-d H:i:s', $invoice->created)),
'gatewayTransactionId' => $invoice->payment_intent,
'subscriptionId' => $this->subscription->id,
'gatewayId' => $this->subscription->gatewayId,
'donorId' => $this->subscription->donorId,
'formId' => $this->subscription->donationFormId,
/**
* TODO: these details might need to come from $invoice object
* It appears we do not store this on the subscription
* so otherwise would have to reach back to the initial donation to find out (which is how legacy works).
*/
'firstName' => $initialDonation->firstName,
'lastName' => $initialDonation->lastName,
'email' => $initialDonation->email,
]);

$this->subscription->bumpRenewalDate();
$this->subscription->save();

// refresh the subscription model
$subscription = Subscription::find($this->subscription->id);

// return a refreshed subscription model to ensure we have the latest data
Expand All @@ -109,4 +82,4 @@ public function handleSubscriptionCompleted()
$this->subscription->status = SubscriptionStatus::COMPLETED();
$this->subscription->save();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ public function testShouldCompleteSubscription()

//refresh subscription model
$subscription = Subscription::find($subscription->id);
// cache donations
$subscriptionDonations = $subscription->donations;
//refresh donation model
$donation = Donation::find($donation->id);

Expand Down Expand Up @@ -178,7 +176,7 @@ function (PHPUnit_Framework_MockObject_MockBuilder $mockBuilder) {
// Refresh subscription model
$subscription = Subscription::find($subscription->id);

$this->assertCount(1, $subscriptionDonations);
$this->assertSame(1, $subscription->totalDonations());
$this->assertTrue($subscription->status->isCompleted());
}

Expand Down

0 comments on commit fbc9ba8

Please sign in to comment.