|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Subscriptions; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Payments\Models\BillingRecord\Traits\RecordTrait as BillingRecord; |
|
6
|
|
|
use ByTIC\Payments\Models\Methods\PaymentMethod; |
|
7
|
|
|
use ByTIC\Payments\Models\Methods\Traits\RecordTrait as PaymentMethodTrait; |
|
8
|
|
|
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait; |
|
9
|
|
|
use ByTIC\Payments\Models\Transactions\Transaction; |
|
10
|
|
|
use ByTIC\Payments\Models\Transactions\TransactionTrait; |
|
11
|
|
|
use ByTIC\Payments\Subscriptions\Statuses\NotStarted; |
|
12
|
|
|
use ByTIC\Payments\Utility\PaymentsModels; |
|
13
|
|
|
use Nip\Utility\Date; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class SubscriptionBuilder |
|
17
|
|
|
* @package ByTIC\Payments\Utility |
|
18
|
|
|
*/ |
|
19
|
|
|
class SubscriptionBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
use Builder\ManageBillingCycle; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \ByTIC\Payments\Models\Subscriptions\Subscription |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $subscription; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* SubscriptionBuilder constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->subscription = PaymentsModels::subscriptions()->getNew(); |
|
34
|
|
|
$this->subscription->setPropertyValue('status', NotStarted::NAME); |
|
|
|
|
|
|
35
|
|
|
$this->subscription->start_at = Date::now(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param IsPurchasableModelTrait $purchase |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function fromPurchase($purchase): SubscriptionBuilder |
|
42
|
|
|
{ |
|
43
|
|
|
$builder = new static(); |
|
44
|
|
|
$builder->withPaymentMethod($purchase->getPaymentMethod()); |
|
45
|
|
|
$builder->withCustomer($purchase->getPurchaseBillingRecord()); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$transaction = PaymentsModels::transactions()->findOrCreateForPurchase($purchase); |
|
48
|
|
|
$builder->withLastTransaction($transaction); |
|
49
|
|
|
return $builder; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $status |
|
54
|
|
|
* @return SubscriptionBuilder |
|
55
|
|
|
*/ |
|
56
|
|
|
public function withStatus($status): SubscriptionBuilder |
|
57
|
|
|
{ |
|
58
|
|
|
$this->subscription->setPropertyValue('status', $status); |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param PaymentMethod|PaymentMethodTrait $method |
|
64
|
|
|
*/ |
|
65
|
|
|
public function withPaymentMethod($method): SubscriptionBuilder |
|
66
|
|
|
{ |
|
67
|
|
|
$this->subscription->populateFromPaymentMethod($method); |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Transaction|TransactionTrait $transaction |
|
73
|
|
|
*/ |
|
74
|
|
|
public function withLastTransaction($transaction): SubscriptionBuilder |
|
75
|
|
|
{ |
|
76
|
|
|
$this->subscription->populateFromLastTransaction($transaction); |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param BillingRecord $customer |
|
82
|
|
|
*/ |
|
83
|
|
|
public function withCustomer($customer): SubscriptionBuilder |
|
84
|
|
|
{ |
|
85
|
|
|
$this->subscription->populateFromCustomer($customer); |
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \ByTIC\Payments\Models\Subscriptions\Subscription|\ByTIC\Payments\Models\Subscriptions\SubscriptionTrait |
|
91
|
|
|
*/ |
|
92
|
|
|
public function create() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->subscription->insert(); |
|
95
|
|
|
|
|
96
|
|
|
$lastTransaction = $this->subscription->getLastTransaction(); |
|
|
|
|
|
|
97
|
|
|
$lastTransaction->populateFromSubscription($this->subscription); |
|
|
|
|
|
|
98
|
|
|
$lastTransaction->update(); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return $this->subscription; |
|
101
|
|
|
} |
|
102
|
|
|
} |