| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Subscriptions\Factories; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Donations\Models\Donation; |
| 7 |
use Give\Donations\ValueObjects\DonationMode; |
| 8 |
use Give\Donations\ValueObjects\DonationStatus; |
| 9 |
use Give\Donations\ValueObjects\DonationType; |
| 10 |
use Give\Donors\Models\Donor; |
| 11 |
use Give\Framework\Models\Factories\ModelFactory; |
| 12 |
use Give\Framework\Support\ValueObjects\Money; |
| 13 |
use Give\PaymentGateways\Gateways\TestGateway\TestGateway; |
| 14 |
use Give\Subscriptions\Actions\GenerateNextRenewalForSubscription; |
| 15 |
use Give\Subscriptions\Models\Subscription; |
| 16 |
use Give\Subscriptions\ValueObjects\SubscriptionMode; |
| 17 |
use Give\Subscriptions\ValueObjects\SubscriptionPeriod; |
| 18 |
use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 19 |
|
| 20 |
class SubscriptionFactory extends ModelFactory |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @since 4.11.0 add campaignId property |
| 24 |
* @since 2.24.0 add mode property |
| 25 |
* @since 2.20.0 update default donorId to create factory |
| 26 |
* @since 2.19.6 |
| 27 |
* |
| 28 |
* @return array |
| 29 |
* @throws Exception |
| 30 |
*/ |
| 31 |
public function definition(): array |
| 32 |
{ |
| 33 |
$frequency = $this->faker->numberBetween(1, 4); |
| 34 |
|
| 35 |
return [ |
| 36 |
'amount' => new Money($this->faker->numberBetween(25, 50000), 'USD'), |
| 37 |
'period' => SubscriptionPeriod::MONTH(), |
| 38 |
'gatewayId' => TestGateway::id(), |
| 39 |
'frequency' => $frequency, |
| 40 |
'donorId' => Donor::factory()->create()->id, |
| 41 |
'installments' => $this->faker->numberBetween(0, 12), |
| 42 |
'feeAmountRecovered' => new Money(0, 'USD'), |
| 43 |
'status' => SubscriptionStatus::PENDING(), |
| 44 |
'renewsAt' => give(GenerateNextRenewalForSubscription::class)(SubscriptionPeriod::MONTH(), $frequency), |
| 45 |
'donationFormId' => 1, |
| 46 |
'campaignId' => 1, |
| 47 |
'mode' => SubscriptionMode::TEST(), |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @since 4.8.0 Respect subscription mode property |
| 53 |
* @since 4.0.0 Add $donationAttributes parameter and merge it with the default attributes when creating a donation |
| 54 |
* @since 2.23.0 |
| 55 |
* |
| 56 |
* @return Subscription|Subscription[] |
| 57 |
* @throws Exception |
| 58 |
*/ |
| 59 |
public function createWithDonation(array $subscriptionAttributes = [], array $donationAttributes = []) |
| 60 |
{ |
| 61 |
$subscriptions = $this->create($subscriptionAttributes); |
| 62 |
|
| 63 |
if ($this->count === 1) { |
| 64 |
$subscriptions = [$subscriptions]; |
| 65 |
} |
| 66 |
|
| 67 |
foreach ($subscriptions as $subscription) { |
| 68 |
$defaultDonationAttributes = [ |
| 69 |
'amount' => $subscription->amount, |
| 70 |
'type' => DonationType::SUBSCRIPTION(), |
| 71 |
'status' => DonationStatus::COMPLETE(), |
| 72 |
'gatewayId' => $subscription->gatewayId, |
| 73 |
'subscriptionId' => $subscription->id, |
| 74 |
'mode' => $subscription->mode->isTest() ? DonationMode::TEST() : DonationMode::LIVE(), |
| 75 |
]; |
| 76 |
|
| 77 |
$donation = Donation::factory()->create( |
| 78 |
array_merge($defaultDonationAttributes, $donationAttributes) |
| 79 |
); |
| 80 |
|
| 81 |
give()->subscriptions->updateLegacyParentPaymentId($subscription->id, $donation->id); |
| 82 |
} |
| 83 |
|
| 84 |
return $this->count === 1 ? $subscriptions[0] : $subscriptions; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @since 2.23.0 pass subscription model and update parentId property |
| 89 |
* @since 2.19.6 |
| 90 |
* |
| 91 |
* @throws Exception |
| 92 |
*/ |
| 93 |
public function createRenewal(Subscription $subscription, int $count = 1, array $attributes = []) |
| 94 |
{ |
| 95 |
return Donation::factory()->count($count)->create( |
| 96 |
array_merge([ |
| 97 |
'amount' => $subscription->amount, |
| 98 |
'status' => DonationStatus::COMPLETE(), |
| 99 |
'type' => DonationType::RENEWAL(), |
| 100 |
'subscriptionId' => $subscription->id, |
| 101 |
], $attributes) |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
|