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