|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Models\Subscriptions; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\Properties\Types\Generic as GenericType; |
|
6
|
|
|
use ByTIC\Payments\Models\AbstractModels\HasCustomer\HasCustomerRepository; |
|
7
|
|
|
use ByTIC\Payments\Models\AbstractModels\HasPaymentMethod\HasPaymentMethodRepository; |
|
8
|
|
|
use ByTIC\Payments\Models\AbstractModels\HasToken\HasTokenRepository; |
|
9
|
|
|
use ByTIC\Payments\Subscriptions\ChargeMethods\Internal; |
|
10
|
|
|
use ByTIC\Payments\Utility\PaymentsModels; |
|
11
|
|
|
use Nip\Records\Collections\Collection; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Trait SubscriptionsTrait |
|
15
|
|
|
* @package ByTIC\Payments\Models\Subscriptions |
|
16
|
|
|
* |
|
17
|
|
|
* @method SubscriptionTrait|Subscription getNew |
|
18
|
|
|
*/ |
|
19
|
|
|
trait SubscriptionsTrait |
|
20
|
|
|
{ |
|
21
|
|
|
use HasCustomerRepository; |
|
22
|
|
|
use HasTokenRepository; |
|
23
|
|
|
use HasPaymentMethodRepository; |
|
24
|
|
|
use \ByTIC\Models\SmartProperties\RecordsTraits\HasStatus\RecordsTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param int $count |
|
28
|
|
|
* @return Collection|Subscription[] |
|
29
|
|
|
*/ |
|
30
|
|
|
public function findChargeDue(int $count = 10): Collection |
|
31
|
|
|
{ |
|
32
|
|
|
$query = $this->paramsToQuery(); |
|
|
|
|
|
|
33
|
|
|
$query->where('charge_at IS NOT NULL'); |
|
34
|
|
|
$query->where('charge_at < NOW()'); |
|
35
|
|
|
$query->order(['charge_at', 'ASC']); |
|
36
|
|
|
$query->limit($count); |
|
37
|
|
|
return $this->findByQuery($query); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get all the types array |
|
42
|
|
|
* |
|
43
|
|
|
* @return GenericType[]|null |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getChargeMethods() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->getSmartPropertyItems('ChargeMethods'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get property of a type by name |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $name Type name |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getChargeMethodProperty($name) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getSmartPropertyValues('ChargeMethods', $name); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function initRelations() |
|
63
|
|
|
{ |
|
64
|
|
|
parent::initRelations(); |
|
65
|
|
|
$this->initRelationsPayments(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function initRelationsPayments() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->initRelationsTransactions(); |
|
71
|
|
|
$this->initRelationsLastTransaction(); |
|
72
|
|
|
$this->initRelationsPaymentMethod(); |
|
73
|
|
|
$this->initRelationsToken(); |
|
74
|
|
|
$this->initRelationsTokens(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function initRelationsTransactions() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->hasMany('Transactions', ['class' => get_class(PaymentsModels::transactions())]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function initRelationsLastTransaction() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->hasOne('LastTransaction', ['class' => get_class(PaymentsModels::transactions())]); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function initRelationsTokens() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->hasMany('Tokens', ['class' => get_class(PaymentsModels::tokens())]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function registerSmartProperties() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->registerSmartProperty('charge_method', 'ChargeMethods'); |
|
95
|
|
|
$this->registerSmartPropertyStatus(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getStatusItemsRootNamespace() |
|
102
|
|
|
{ |
|
103
|
|
|
return 'ByTIC\Payments\Subscriptions\Statuses\\'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getStatusItemsDirectory() |
|
110
|
|
|
{ |
|
111
|
|
|
return dirname(dirname(__DIR__)) |
|
112
|
|
|
. DIRECTORY_SEPARATOR . 'Subscriptions' |
|
113
|
|
|
. DIRECTORY_SEPARATOR . 'Statuses'; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getDefaultChargeMethods() |
|
120
|
|
|
{ |
|
121
|
|
|
return Internal::NAME; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getChargeMethodsItemsRootNamespace() |
|
128
|
|
|
{ |
|
129
|
|
|
return 'ByTIC\Payments\Subscriptions\ChargeMethods\\'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getChargeMethodsItemsDirectory() |
|
136
|
|
|
{ |
|
137
|
|
|
return dirname(dirname(__DIR__)) |
|
138
|
|
|
. DIRECTORY_SEPARATOR . 'Subscriptions' |
|
139
|
|
|
. DIRECTORY_SEPARATOR . 'ChargeMethods'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return mixed|\Nip\Config\Config |
|
144
|
|
|
* @throws \Exception |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function generateTable() |
|
147
|
|
|
{ |
|
148
|
|
|
return config('payments.tables.subscriptions', Subscriptions::TABLE); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|