Skip to content

Commit

Permalink
Merge pull request #42 from ciungulete/FixNextPaymentAt
Browse files Browse the repository at this point in the history
Fix #25
  • Loading branch information
sandervanhooft authored Jan 18, 2022
2 parents 7005516 + 4723b1a commit 7f6569b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/FirstPayment/Actions/StartSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static function createFromPayload(array $payload, Model $owner)
$action->trialUntil(Carbon::parse($payload['trialUntil']));
}

if (isset($payload['nextPaymentAt'])) {
$action->nextPaymentAt(Carbon::parse($payload['nextPaymentAt']));
}

if (isset($payload['trialDays'])) {
$action->trialDays($payload['trialDays']);
}
Expand Down Expand Up @@ -287,6 +291,7 @@ public function withCoupon(string $coupon)
public function nextPaymentAt(Carbon $nextPaymentAt)
{
$this->nextPaymentAt = $nextPaymentAt;
$this->builder()->nextPaymentAt($nextPaymentAt);

return $this;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/FirstPayment/Actions/StartSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,45 @@ public function canStartSubscriptionWithQuantityAndTrialUntil()
$this->assertEquals(5 * 1000, $scheduledItem->total);
}

/** @test */
public function canStartSubscriptionUsingNextPaymentAt()
{
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();
$user = $this->getMandatedUser();

$this->assertFalse($user->subscribed('default'));

$action = new StartSubscription(
$user,
'default',
'monthly-10-1'
);

$action->nextPaymentAt(now()->addWeeks(2));

// Returns the OrderItem ready for processing right away.
// Behind the scenes another OrderItem is scheduled for the next billing cycle.
$items = $action->execute();
$item = $items->first();
$user = $user->fresh();

$this->assertTrue($user->subscribed('default'));
$this->assertInstanceOf(OrderItemCollection::class, $items);
$this->assertCount(1, $items);
$this->assertInstanceOf(OrderItem::class, $item);
$this->assertFalse($item->isProcessed());
$this->assertCarbon(now(), $item->process_at);

$subscription = $user->subscription('default');
$this->assertEquals(2, $subscription->orderItems()->count());
$this->assertCarbon(now(), $subscription->cycle_started_at);
$this->assertCarbon(now()->addWeeks(2), $subscription->cycle_ends_at);

$scheduledItem = $subscription->orderItems()->orderByDesc('process_at')->first();
$this->assertCarbon(now()->addWeeks(2), $scheduledItem->process_at);
}

/** @test */
public function canStartSubscriptionWithCouponNoTrial()
{
Expand Down

0 comments on commit 7f6569b

Please sign in to comment.