1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\Couponables\Traits; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use MichaelRubel\Couponables\Models\Contracts\CouponContract; |
12
|
|
|
use MichaelRubel\Couponables\Services\Contracts\CouponServiceContract; |
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
trait HasCoupons |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CouponServiceContract |
19
|
|
|
*/ |
20
|
|
|
protected CouponServiceContract $couponService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initialize the method binding objects. |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
61 |
|
protected function initializeHasCoupons(): void |
28
|
|
|
{ |
29
|
61 |
|
if (app()->bound(CouponServiceContract::class)) { |
30
|
61 |
|
$this->couponService = app(CouponServiceContract::class); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Polymorphic relation to the coupons. |
36
|
|
|
* |
37
|
|
|
* @return MorphToMany |
38
|
|
|
*/ |
39
|
21 |
|
public function coupons(): MorphToMany |
40
|
|
|
{ |
41
|
21 |
|
return with($this->couponService, function (CouponServiceContract $service) { |
|
|
|
|
42
|
21 |
|
$morphName = Str::singular(config('couponables.pivot_table', 'couponables')); |
43
|
|
|
|
44
|
21 |
|
return $this->morphToMany($service->model, $morphName)->withPivot([ |
|
|
|
|
45
|
21 |
|
$service->pivot->getRedeemedTypeColumn(), |
|
|
|
|
46
|
21 |
|
$service->pivot->getRedeemedIdColumn(), |
47
|
21 |
|
$service->pivot->getRedeemedAtColumn(), |
48
|
21 |
|
$service->pivot->getCreatedAtColumn(), |
49
|
21 |
|
$service->pivot->getUpdatedAtColumn(), |
50
|
21 |
|
]); |
51
|
21 |
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if coupon with this code is already used. |
56
|
|
|
* |
57
|
|
|
* @param string|null $code |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
5 |
|
public function isCouponAlreadyUsed(?string $code): bool |
62
|
|
|
{ |
63
|
5 |
|
$column = $this->couponService->model->getCodeColumn(); |
|
|
|
|
64
|
|
|
|
65
|
5 |
|
return $this->coupons()->where($column, $code)->exists(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check if the coupon is over limit for the model. |
70
|
|
|
* |
71
|
|
|
* @param string|null $code |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
2 |
|
public function isCouponOverLimit(?string $code): bool |
76
|
|
|
{ |
77
|
2 |
|
$coupon = $this->couponService->getCoupon($code); |
78
|
|
|
|
79
|
2 |
|
return ! is_null($coupon) && $coupon->isOverLimitFor($this); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Verify if the coupon is valid. |
84
|
|
|
* |
85
|
|
|
* @param string|null $code |
86
|
|
|
* |
87
|
|
|
* @return CouponContract |
88
|
|
|
*/ |
89
|
8 |
|
public function verifyCoupon(?string $code): CouponContract |
90
|
|
|
{ |
91
|
8 |
|
return $this->couponService->verifyCoupon($code, $this); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Verify and use the coupon. |
96
|
|
|
* |
97
|
|
|
* @param string|null $code |
98
|
|
|
* @param Model|null $for |
99
|
|
|
* |
100
|
|
|
* @return CouponContract |
101
|
|
|
*/ |
102
|
30 |
|
public function redeemCoupon(?string $code, ?Model $for = null): CouponContract |
103
|
|
|
{ |
104
|
30 |
|
$coupon = $this->couponService->verifyCoupon($code, $this); |
|
|
|
|
105
|
|
|
|
106
|
18 |
|
return $this->couponService->applyCoupon($coupon, $this, $for); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Verify the coupon or do something else on fail. |
111
|
|
|
* |
112
|
|
|
* @param string|null $code |
113
|
|
|
* @param Closure|null $callback |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
4 |
|
public function verifyCouponOr(?string $code, ?Closure $callback = null): mixed |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
4 |
|
return $this->verifyCoupon($code); |
121
|
3 |
|
} catch (Throwable $e) { |
122
|
3 |
|
return $callback instanceof Closure ? $callback($code, $e) : throw $e; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Redeem the coupon or do something else on fail. |
128
|
|
|
* |
129
|
|
|
* @param string|null $code |
130
|
|
|
* @param Closure|null $callback |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
4 |
|
public function redeemCouponOr(?string $code, ?Closure $callback = null): mixed |
135
|
|
|
{ |
136
|
|
|
try { |
137
|
4 |
|
return $this->redeemCoupon($code); |
138
|
3 |
|
} catch (Throwable $e) { |
139
|
3 |
|
return $callback instanceof Closure ? $callback($code, $e) : throw $e; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Redeem the code using model. |
145
|
|
|
* |
146
|
|
|
* @param Model $model |
147
|
|
|
* @param string|null $couponCode |
148
|
|
|
* |
149
|
|
|
* @return CouponContract |
150
|
|
|
*/ |
151
|
1 |
|
public function redeemBy(Model $model, ?string $couponCode): CouponContract |
152
|
|
|
{ |
153
|
1 |
|
$coupon = $this->couponService->verifyCoupon($couponCode, $model); |
154
|
|
|
|
155
|
1 |
|
return $this->couponService->applyCoupon($coupon, $model, $this); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|