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

Refactor: update Stripe subscription model decorator to use subscription model methods directly #7696

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonwaldstein It makes me wonder... In Stripe, is it possible for a renewal to have a different value from the subscription? In which scenarios it can happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glaubersilva that's a great question! Ideally there should not be a different value from the subscription in Give. My train of thought here was to use Stripe as the source of truth in case there is a different value - so we are recording the actual paid amount. The 2 scenarios where this would be possible (yet, very unlikely) are:

  1. The subscription was somehow updated in Stripe and not in Give
  2. The amount was recorded wrong in Give

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
Loading