forked from laravel/cashier-mollie
/
SwapSubscriptionPlanTest.php
265 lines (217 loc) · 11 KB
/
SwapSubscriptionPlanTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace Laravel\Cashier\Tests;
use Illuminate\Support\Facades\Event;
use Laravel\Cashier\Events\SubscriptionPlanSwapped;
use Laravel\Cashier\Mollie\Contracts\CreateMolliePayment;
use Laravel\Cashier\Mollie\Contracts\GetMollieMandate;
use Laravel\Cashier\Mollie\Contracts\GetMollieMethodMinimumAmount;
use Laravel\Cashier\Mollie\GetMollieCustomer;
use Laravel\Cashier\Order\OrderItem;
use Laravel\Cashier\Subscription;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Customer;
use Mollie\Api\Resources\Mandate;
use Mollie\Api\Resources\Payment;
class SwapSubscriptionPlanTest extends BaseTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->withPackageMigrations();
$this->withTestNow('2019-1-1');
$this->withConfiguredPlans();
Event::fake();
}
/** @test */
public function canSwapToAnotherPlan()
{
$now = now();
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();
$this->withMockedGetMollieMethodMinimumAmount();
$this->withMockedCreateMolliePayment();
$user = $this->getUserWithZeroBalance();
$subscription = $this->getSubscriptionForUser($user);
$original_order_item = $subscription->scheduleNewOrderItemAt($now->copy()->subWeeks(2));
$this->assertTrue($subscription->scheduledOrderItem->is($original_order_item));
// Swap to new plan
$subscription = $subscription->swap('weekly-20-1')->fresh();
$this->assertEquals('weekly-20-1', $subscription->plan);
$this->assertEquals('weekly-20-1', $subscription->name);
$this->assertCarbon($now->copy(), $subscription->cycle_started_at);
$this->assertCarbon($now->copy()->addWeek(), $subscription->cycle_ends_at);
// Assert that the original scheduled OrderItem has been removed
$this->assertFalse(OrderItem::whereId($original_order_item->id)->exists());
// Assert that another OrderItem was scheduled for the new subscription plan
$new_order_item = $subscription->scheduledOrderItem;
$this->assertFalse($new_order_item->is($original_order_item));
$this->assertCarbon($now->copy()->addWeek(), $new_order_item->process_at, 1);
$this->assertMoneyEURCents(2200, $new_order_item->getTotal());
$this->assertMoneyEURCents(200, $new_order_item->getTax());
$this->assertEquals('Twice as expensive monthly subscription', $new_order_item->description);
$this->assertFalse($new_order_item->isProcessed());
// Assert that the amount "overpaid" for the old plan results in an additional OrderItem with negative total_amount
$credit_item = OrderItem::where('unit_price', '<', 0)->first();
$this->assertNotNull($credit_item);
$this->assertCarbon($now->copy(), $credit_item->process_at, 1);
$this->assertMoneyEURCents(-603, $credit_item->getTotal());
$this->assertMoneyEURCents(-55, $credit_item->getTax());
$this->assertEquals('Monthly payment', $credit_item->description);
$this->assertTrue($credit_item->isProcessed());
// Assert that one OrderItem has already been processed
$processed_item = OrderItem::whereNotIn('id', [
$new_order_item->id,
$original_order_item->id,
$credit_item->id,
])->first();
$this->assertNotNull($processed_item);
$this->assertCarbon($now->copy(), $processed_item->process_at, 1);
$this->assertMoneyEURCents(2200, $processed_item->getTotal());
$this->assertMoneyEURCents(200, $processed_item->getTax());
$this->assertEquals('Twice as expensive monthly subscription', $processed_item->description);
$this->assertTrue($processed_item->isProcessed());
Event::assertDispatched(SubscriptionPlanSwapped::class, function (SubscriptionPlanSwapped $event) use ($subscription) {
return $subscription->is($event->subscription);
});
$new_order_item->process();
$subscription = $subscription->fresh();
$this->assertCarbon($now->copy()->addWeek(), $subscription->cycle_started_at);
$this->assertCarbon($now->copy()->addWeeks(2), $subscription->cycle_ends_at);
$scheduled_order_item = $subscription->scheduledOrderItem;
$this->assertCarbon($now->copy()->addWeeks(2), $scheduled_order_item->process_at, 1);
$this->assertMoneyEURCents(2200, $scheduled_order_item->getTotal());
$this->assertMoneyEURCents(200, $scheduled_order_item->getTax());
$this->assertEquals('Twice as expensive monthly subscription', $scheduled_order_item->description);
$this->assertFalse($scheduled_order_item->isProcessed());
}
/** @test */
public function swappingACancelledSubscriptionResumesIt()
{
$subscription = $this->getUser()->subscriptions()->save(
factory(Subscription::class)->make([
'ends_at' => now()->addWeek(),
'plan' => 'monthly-20-1',
'name' => 'monthly-20-1',
])
);
$subscription->cancel();
$this->assertTrue($subscription->cancelled());
$subscription->swap('weekly-20-1', false);
$this->assertFalse($subscription->cancelled());
}
/** @test */
public function canSwapNextCycle()
{
$user = $this->getUserWithZeroBalance();
$subscription = $this->getSubscriptionForUser($user);
$original_order_item = $subscription->scheduleNewOrderItemAt(now()->subWeeks(2));
$this->assertTrue($subscription->scheduledOrderItem->is($original_order_item));
// Swap to new plan
$subscription = $subscription->swapNextCycle('weekly-20-1')->fresh();
$this->assertEquals('monthly-10-1', $subscription->plan);
$this->assertEquals('monthly-10-1', $subscription->name);
$this->assertEquals('weekly-20-1', $subscription->next_plan);
// Check that the billing cycle remains intact
$cycle_should_have_started_at = now()->subWeeks(2);
$cycle_should_end_at = $cycle_should_have_started_at->copy()->addMonth();
$this->assertCarbon($cycle_should_have_started_at, $subscription->cycle_started_at);
$this->assertCarbon($cycle_should_end_at, $subscription->cycle_ends_at);
// Assert that the original scheduled OrderItem has been removed
// And assert that another OrderItem was scheduled for the new subscription plan
$this->assertFalse(OrderItem::whereId($original_order_item->id)->exists());
$new_order_item = $subscription->scheduledOrderItem;
$this->assertFalse($new_order_item->is($original_order_item));
$this->assertCarbon($cycle_should_end_at, $new_order_item->process_at, 1); // based on previous plan's cycle
$this->assertEquals(2200, $new_order_item->total);
$this->assertEquals(200, $new_order_item->tax);
$this->assertEquals('Twice as expensive monthly subscription', $new_order_item->description);
$this->assertFalse($user->fresh()->hasCredit());
Event::assertNotDispatched(SubscriptionPlanSwapped::class);
$this->assertEquals('monthly-10-1', $subscription->plan);
$this->assertEquals('monthly-10-1', $subscription->name);
$this->assertEquals('weekly-20-1', $subscription->next_plan);
Subscription::processOrderItem($new_order_item);
$subscription = $subscription->fresh();
$this->assertNull($subscription->next_plan);
$this->assertEquals('weekly-20-1', $subscription->plan);
$this->assertEquals('weekly-20-1', $subscription->name);
// Assert that the subscription cycle reflects the new plan
$cycle_should_have_started_at = $cycle_should_end_at->copy();
$cycle_should_end_at = $cycle_should_have_started_at->copy()->addWeek();
$this->assertCarbon($cycle_should_have_started_at, $subscription->cycle_started_at);
$this->assertCarbon($cycle_should_end_at, $subscription->cycle_ends_at);
Event::assertDispatched(SubscriptionPlanSwapped::class, function (SubscriptionPlanSwapped $event) use ($subscription) {
return $subscription->is($event->subscription);
});
}
protected function getUserWithZeroBalance()
{
$user = $this->getMandatedUser(true, [
'tax_percentage' => 10,
'mollie_customer_id' => 'cst_unique_customer_id',
'mollie_mandate_id' => 'mdt_unique_mandate_id',
]);
$this->assertEquals(0, $user->credits()->whereCurrency('EUR')->count());
return $user;
}
/**
* @param $user
* @return Subscription
*/
protected function getSubscriptionForUser($user)
{
return $user->subscriptions()->save(factory(Subscription::class)->make([
"name" => "monthly-10-1",
"plan" => "monthly-10-1",
"cycle_started_at" => now()->subWeeks(2),
"cycle_ends_at" => now()->subWeeks(2)->addMonth(),
"tax_percentage" => 10,
]));
}
protected function withMockedGetMollieCustomer($customerIds = ['cst_unique_customer_id'], $times = 1): void
{
$this->mock(GetMollieCustomer::class, function ($mock) use ($customerIds, $times) {
foreach ($customerIds as $id) {
$customer = new Customer(new MollieApiClient);
$customer->id = $id;
$mock->shouldReceive('execute')->with($id)->times($times)->andReturn($customer);
}
return $mock;
});
}
protected function withMockedGetMollieMandate($attributes = [[
'mandateId' => 'mdt_unique_mandate_id',
'customerId' => 'cst_unique_customer_id',
]], $times = 1): void
{
$this->mock(GetMollieMandate::class, function ($mock) use ($times, $attributes) {
foreach ($attributes as $data) {
$mandate = new Mandate(new MollieApiClient);
$mandate->id = $data['mandateId'];
$mandate->status = 'valid';
$mandate->method = 'directdebit';
$mock->shouldReceive('execute')->with($data['customerId'], $data['mandateId'])->times($times)->andReturn($mandate);
}
return $mock;
});
}
protected function withMockedGetMollieMethodMinimumAmount($times = 1): void
{
$this->mock(GetMollieMethodMinimumAmount::class, function ($mock) use ($times) {
return $mock->shouldReceive('execute')->with('directdebit', 'EUR')->times($times)->andReturn(money(100, 'EUR'));
});
}
protected function withMockedCreateMolliePayment($times = 1): void
{
$this->mock(CreateMolliePayment::class, function ($mock) use ($times) {
$payment = new Payment($this->getMollieClientMock());
$payment->id = 'tr_dummy_id';
$payment->amount = (object) [
'currency' => 'EUR',
'value' => '10.00',
];
$payment->mandateId = 'mdt_dummy_mandate_id';
return $mock->shouldReceive('execute')->times($times)->andReturn($payment);
});
}
}