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 |