PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.19.6
GiveWP – Donation Plugin and Fundraising Platform v2.19.6
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 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 All 253 releases
give / src / Subscriptions / Factories / SubscriptionFactory.php
SubscriptionFactory.php
80 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Subscriptions\Factories;
4
5 use Exception;
6 use Give\Donations\Models\Donation;
7 use Give\Donations\ValueObjects\DonationStatus;
8 use Give\Framework\Models\Factories\ModelFactory;
9 use Give\Subscriptions\Models\Subscription;
10 use Give\Subscriptions\ValueObjects\SubscriptionPeriod;
11 use Give\Subscriptions\ValueObjects\SubscriptionStatus;
12
13 class SubscriptionFactory extends ModelFactory
14 {
15
16 /**
17 * @since 2.19.6
18 *
19 * @return array
20 */
21 public function definition()
22 {
23 return [
24 'amount' => $this->faker->numberBetween(25, 50000),
25 'period' => SubscriptionPeriod::MONTH(),
26 'frequency' => $this->faker->numberBetween(1, 4),
27 'donorId' => 1,
28 'installments' => $this->faker->numberBetween(0, 12),
29 'feeAmount' => 0,
30 'status' => SubscriptionStatus::PENDING(),
31 'donationFormId' => 1
32 ];
33 }
34
35 /**
36 * @since 2.19.6
37 *
38 * @throws Exception
39 */
40 public function createRenewal($subscriptionId, array $attributes = [])
41 {
42 return Donation::factory()->create(
43 array_merge([
44 'status' => DonationStatus::RENEWAL(),
45 'subscriptionId' => $subscriptionId,
46 'parentId' => give()->subscriptions->getInitialDonationId($subscriptionId),
47 ], $attributes)
48 );
49 }
50
51 /**
52 * @since 2.19.6
53 *
54 * @param $model
55 * @return void
56 * @throws Exception
57 */
58 public function afterCreating($model)
59 {
60 /** @var Subscription $subscription */
61 $subscription = $model;
62
63 // check if initial donation ID (parent_payment_id has been recorded
64 $initialDonationId = give()->subscriptions->getInitialDonationId($subscription->id);
65
66 // for backwards compatability update the subscription parent_payment_id column
67 if (!$initialDonationId) {
68 $donation = Donation::factory()->create(['donorId' => $subscription->donorId]);
69 give()->donations->updateLegacyDonationMetaAsInitialSubscriptionDonation($donation->id);
70 give()->subscriptions->updateLegacyColumns(
71 $subscription->id,
72 [
73 'parent_payment_id' => $donation->id,
74 'expiration' => $subscription->expiration()
75 ]
76 );
77 }
78 }
79 }
80