-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubscriptionCheckout.php
119 lines (94 loc) · 3.59 KB
/
SubscriptionCheckout.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
<?php
namespace App\Services\Subscription;
use App\Events\Subscription\UserSubscribed;
use App\Models\PaymentMethod;
use Throwable;
use App\Models\User;
use App\Enums\PaymentType;
use App\Enums\PaymentStatus;
use Illuminate\Support\Facades\DB;
use App\Exceptions\PaymentException;
use App\Models\SubscriptionPlan;
use App\Models\Subscription;
/**
* SubscriptionCheckout represents the checkout for the user subscription purchase.
*
* Checkout binds user with subscription plan, performing online payment via BrainTree.
* User's credit card can be created in the process.
*
* @see \App\Exceptions\PaymentException
*/
class SubscriptionCheckout
{
/**
* @var User user to be subscribed.
*/
private $user;
/**
* @var SubscriptionPlan subscription plan to be applied for the {@link user}
*/
private $subscriptionPlan;
public function __construct(User $user, SubscriptionPlan $subscriptionPlan)
{
$this->user = $user;
$this->subscriptionPlan = $subscriptionPlan;
}
public function process($paymentMethodNonce = null): Subscription
{
DB::beginTransaction();
try {
if ($paymentMethodNonce) {
(new PaymentMethod())->createForUser($this->user, $paymentMethodNonce);
}
$isNewSubscription = ($this->user->activeSubscription === null);
$paymentAmount = $this->calculatePaymentAmount();
if ($paymentAmount > 0) {
if ($this->user->activePaymentMethod === null) {
throw new PaymentException('Unable to perform payment: there is no credit card available.');
}
$payment = $this->user->activePaymentMethod->pay($paymentAmount, PaymentType::SUBSCRIPTION);
if (!$payment->isSuccessful()) {
throw new PaymentException('Unable to perform payment: '.$payment->getErrorMessage());
}
$subscription = $this->subscriptionPlan->subscribe($this->user);
$subscription->payments()->attach($payment->id);
} else {
$subscription = $this->subscriptionPlan->subscribe($this->user);
}
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
throw $e;
}
if ($isNewSubscription) {
event(new UserSubscribed($subscription));
}
return $subscription;
}
/**
* Calculates amount to be paid for the subscription.
* This method takes into account user's active subscription, including discount in case of
* subscription upgrade/downgrade.
* @return float
*/
public function calculatePaymentAmount()
{
if ($this->user->activeSubscription === null) {
return $this->subscriptionPlan->price;
}
$now = now()->startOfDay();
if ($now->gte($this->user->activeSubscription->end_at)) {
return $this->subscriptionPlan->price;
}
$dayDiff = $this->user->activeSubscription->end_at->diffInDays($now);
if ($dayDiff < 1) {
return $this->subscriptionPlan->price;
}
$subscriptionDayLength = $this->user->activeSubscription->end_at->diffInDays($this->user->activeSubscription->begin_at);
$subscriptionPaidAmount = $this->user->activeSubscription->payments()
->where(['status' => PaymentStatus::SUCCESS])
->sum('amount');
$discount = round($subscriptionPaidAmount * $dayDiff / $subscriptionDayLength, 2);
return max($this->subscriptionPlan->price - $discount, 0);
}
}