PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.20.2
GiveWP – Donation Plugin and Fundraising Platform v2.20.2
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / Subscriptions / Repositories / SubscriptionRepository.php
give / src / Subscriptions / Repositories Last commit date
SubscriptionRepository.php 4 years ago
SubscriptionRepository.php
358 lines
1 <?php
2
3 namespace Give\Subscriptions\Repositories;
4
5 use Exception;
6 use Give\Donations\ValueObjects\DonationMetaKeys;
7 use Give\Framework\Database\DB;
8 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
9 use Give\Framework\Models\ModelQueryBuilder;
10 use Give\Framework\Support\Facades\DateTime\Temporal;
11 use Give\Helpers\Hooks;
12 use Give\Log\Log;
13 use Give\Subscriptions\Models\Subscription;
14
15 /**
16 * @since 2.19.6
17 */
18 class SubscriptionRepository
19 {
20
21 /**
22 * @var string[]
23 */
24 private $requiredSubscriptionProperties = [
25 'donorId',
26 'period',
27 'frequency',
28 'amount',
29 'status',
30 'donationFormId',
31 ];
32
33 /**
34 * @since 2.19.6
35 *
36 * @param int $subscriptionId
37 *
38 * @return Subscription
39 */
40 public function getById($subscriptionId)
41 {
42 return $this->queryById($subscriptionId)->get();
43 }
44
45 /**
46 * @since 2.19.6
47 *
48 * @param int $subscriptionId
49 *
50 * @return ModelQueryBuilder
51 */
52 public function queryById($subscriptionId)
53 {
54 return $this->prepareQuery()
55 ->where('id', $subscriptionId);
56 }
57
58 /**
59 * @since 2.19.6
60 *
61 * @param int $donationId
62 *
63 * @return ModelQueryBuilder
64 */
65 public function queryByDonationId($donationId)
66 {
67 return $this->prepareQuery()
68 ->where('parent_payment_id', $donationId);
69 }
70
71 /**
72 * @since 2.19.6
73 *
74 * @param int $donorId
75 *
76 * @return ModelQueryBuilder
77 */
78 public function queryByDonorId($donorId)
79 {
80 return $this->prepareQuery()
81 ->where('customer_id', $donorId);
82 }
83
84 /**
85 * @since 2.19.6
86 *
87 * @param int $id
88 *
89 * @return object[]
90 */
91 public function getNotesBySubscriptionId($id)
92 {
93 $notes = DB::table('comments')
94 ->select(
95 ['comment_content', 'note'],
96 ['comment_date', 'date']
97 )
98 ->where('comment_post_ID', $id)
99 ->where('comment_type', 'give_sub_note')
100 ->orderBy('comment_date', 'DESC')
101 ->getAll();
102
103 if (!$notes) {
104 return [];
105 }
106
107 return $notes;
108 }
109
110 /**
111 * @since 2.19.6
112 *
113 * @param Subscription $subscription
114 *
115 * @return void
116 * @throws Exception
117 */
118 public function insert(Subscription $subscription)
119 {
120 $this->validateSubscription($subscription);
121
122 Hooks::doAction('give_subscription_creating', $subscription);
123
124 $dateCreated = Temporal::withoutMicroseconds($subscription->createdAt ?: Temporal::getCurrentDateTime());
125
126 DB::query('START TRANSACTION');
127
128 try {
129 DB::table('give_subscriptions')->insert([
130 'created' => Temporal::getFormattedDateTime($dateCreated),
131 'status' => $subscription->status->getValue(),
132 'profile_id' => $subscription->gatewaySubscriptionId ?? '',
133 'customer_id' => $subscription->donorId,
134 'period' => $subscription->period->getValue(),
135 'frequency' => $subscription->frequency,
136 'initial_amount' => $subscription->amount->formatToDecimal(),
137 'recurring_amount' => $subscription->amount->formatToDecimal(),
138 'recurring_fee_amount' => $subscription->feeAmountRecovered !== null ? $subscription->feeAmountRecovered->formatToDecimal() : 0,
139 'bill_times' => $subscription->installments,
140 'transaction_id' => $subscription->transactionId ?? '',
141 'product_id' => $subscription->donationFormId,
142 ]);
143 } catch (Exception $exception) {
144 DB::query('ROLLBACK');
145
146 Log::error('Failed creating a subscription', compact('subscription'));
147
148 throw new $exception('Failed creating a subscription');
149 }
150
151 DB::query('COMMIT');
152
153 $subscriptionId = DB::last_insert_id();
154
155 $subscription->id = $subscriptionId;
156 $subscription->createdAt = $dateCreated;
157
158 if (!isset($subscription->expiresAt)) {
159 $subscription->expiresAt = null;
160 }
161
162 Hooks::doAction('give_subscription_created', $subscription);
163 }
164
165 /**
166 * @since 2.19.6
167 *
168 * @param Subscription $subscription
169 *
170 * @return void
171 * @throws Exception
172 */
173 public function update(Subscription $subscription)
174 {
175 $this->validateSubscription($subscription);
176
177 Hooks::doAction('give_subscription_updating', $subscription);
178
179 DB::query('START TRANSACTION');
180
181 try {
182 DB::table('give_subscriptions')
183 ->where('id', $subscription->id)
184 ->update([
185 'status' => $subscription->status->getValue(),
186 'profile_id' => $subscription->gatewaySubscriptionId,
187 'customer_id' => $subscription->donorId,
188 'period' => $subscription->period->getValue(),
189 'frequency' => $subscription->frequency,
190 'initial_amount' => $subscription->amount->formatToDecimal(),
191 'recurring_amount' => $subscription->amount->formatToDecimal(),
192 'recurring_fee_amount' => isset($subscription->feeAmountRecovered) ? $subscription->feeAmountRecovered->formatToDecimal() : 0,
193 'bill_times' => $subscription->installments,
194 'transaction_id' => $subscription->transactionId ?? '',
195 'product_id' => $subscription->donationFormId,
196 ]);
197 } catch (Exception $exception) {
198 DB::query('ROLLBACK');
199
200 Log::error('Failed updating a subscription', compact('subscription'));
201
202 throw new $exception('Failed updating a subscription');
203 }
204
205 DB::query('COMMIT');
206
207 Hooks::doAction('give_subscription_updating', $subscription);
208 }
209
210 /**
211 * @since 2.20.0 consolidate meta deletion into a single query
212 * @since 2.19.6
213 *
214 * @param Subscription $subscription
215 *
216 * @return bool
217 *
218 * @throws Exception
219 */
220 public function delete(Subscription $subscription)
221 {
222 Hooks::doAction('give_subscription_deleting', $subscription);
223
224 DB::query('START TRANSACTION');
225
226 try {
227 DB::table('give_subscriptions')
228 ->where('id', $subscription->id)
229 ->delete();
230
231 DB::table('give_subscriptionmeta')
232 ->where('subscription_id', $subscription->id)
233 ->delete();
234 } catch (Exception $exception) {
235 DB::query('ROLLBACK');
236
237 Log::error('Failed deleting a subscription', compact('subscription'));
238
239 throw new $exception('Failed deleting a subscription');
240 }
241
242 DB::query('COMMIT');
243
244 Hooks::doAction('give_subscription_model_deleted', $subscription);
245
246 return true;
247 }
248
249 /**
250 * @since 2.19.6
251 *
252 * @param int $subscriptionId
253 * @param array $columns
254 *
255 * @return bool
256 * @throws Exception
257 */
258 public function updateLegacyColumns($subscriptionId, $columns)
259 {
260 foreach (Subscription::propertyKeys() as $key) {
261 if (array_key_exists($key, $columns)) {
262 throw new InvalidArgumentException("'$key' is not a legacy column.");
263 }
264 }
265
266 DB::query('START TRANSACTION');
267
268 try {
269 DB::table('give_subscriptions')
270 ->where('id', $subscriptionId)
271 ->update($columns);
272 } catch (Exception $exception) {
273 DB::query('ROLLBACK');
274
275 Log::error('Failed updating a subscription', compact('subscriptionId', 'columns'));
276
277 throw new $exception('Failed updating a subscription');
278 }
279
280 DB::query('COMMIT');
281
282 return true;
283 }
284
285 /**
286 * @since 2.19.6
287 *
288 * @param int $subscriptionId
289 *
290 * @return int|null
291 */
292 public function getInitialDonationId($subscriptionId)
293 {
294 $query = DB::table('give_subscriptions')
295 ->where('id', $subscriptionId)
296 ->select(['parent_payment_id', 'initialDonationId'])
297 ->get();
298
299 if (!$query) {
300 return null;
301 }
302
303 return (int)$query->initialDonationId;
304 }
305
306 /**
307 * @param Subscription $subscription
308 *
309 * @return void
310 */
311 private function validateSubscription(Subscription $subscription)
312 {
313 foreach ($this->requiredSubscriptionProperties as $key) {
314 if (!isset($subscription->$key)) {
315 throw new InvalidArgumentException("'$key' is required.");
316 }
317 }
318
319 if (!$subscription->donor) {
320 throw new InvalidArgumentException("Invalid donorId, Donor does not exist");
321 }
322 }
323
324 /**
325 * @since 2.19.6
326 *
327 * @return ModelQueryBuilder<Subscription>
328 */
329 public function prepareQuery()
330 {
331 $builder = new ModelQueryBuilder(Subscription::class);
332
333 return $builder->from('give_subscriptions')
334 ->select(
335 'id',
336 ['created', 'createdAt'],
337 ['expiration', 'expiresAt'],
338 ['customer_id', 'donorId'],
339 'period',
340 ['frequency', 'frequency'],
341 ['bill_times', 'installments'],
342 ['transaction_id', 'transactionId'],
343 ['recurring_amount', 'amount'],
344 ['recurring_fee_amount', 'feeAmount'],
345 'status',
346 ['profile_id', 'gatewaySubscriptionId'],
347 ['product_id', 'donationFormId']
348 )
349 ->attachMeta(
350 'give_donationmeta',
351 'parent_payment_id',
352 'donation_id',
353 [DonationMetaKeys::GATEWAY, 'gatewayId'],
354 [DonationMetaKeys::CURRENCY, 'currency']
355 );
356 }
357 }
358