|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Models\Transactions; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Payments\Models\AbstractModels\HasPaymentMethod\HasPaymentMethodRepository; |
|
6
|
|
|
use ByTIC\Payments\Models\AbstractModels\HasToken\HasTokenRepository; |
|
7
|
|
|
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait; |
|
8
|
|
|
use ByTIC\Payments\Models\Purchases\Purchase; |
|
9
|
|
|
use ByTIC\Payments\Utility\PaymentsModels; |
|
10
|
|
|
use Nip\MailModule\Models\EmailsTable\EmailTrait; |
|
11
|
|
|
use Nip\Records\AbstractModels\Record; |
|
12
|
|
|
use Nip\Records\EventManager\Events\Event; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait TransactionsTrait |
|
16
|
|
|
* @package ByTIC\Payments\Models\Transactions |
|
17
|
|
|
* |
|
18
|
|
|
* @method TransactionTrait getNew |
|
19
|
|
|
*/ |
|
20
|
|
|
trait TransactionsTrait |
|
21
|
|
|
{ |
|
22
|
|
|
use \ByTIC\Models\SmartProperties\RecordsTraits\HasStatus\RecordsTrait; |
|
23
|
|
|
use HasTokenRepository; |
|
24
|
|
|
use HasPaymentMethodRepository; |
|
25
|
|
|
|
|
26
|
|
|
public function bootTransactionsTrait() |
|
27
|
|
|
{ |
|
28
|
|
|
static::creating(function (Event $event) { |
|
29
|
|
|
|
|
30
|
|
|
/** @var EmailTrait|\Nip\Records\Record $record */ |
|
31
|
|
|
$record = $event->getRecord(); |
|
32
|
|
|
|
|
33
|
|
|
$record->setIf('metadata', '{}', function () use ($record) { |
|
34
|
|
|
return count($record->metadata) < 1; |
|
|
|
|
|
|
35
|
|
|
}); |
|
36
|
|
|
}); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Purchase|IsPurchasableModelTrait $purchase |
|
41
|
|
|
* @return TransactionTrait |
|
42
|
|
|
*/ |
|
43
|
|
|
public function findOrCreateForPurchase($purchase) |
|
44
|
|
|
{ |
|
45
|
|
|
$transaction = $this->findForPurchase($purchase); |
|
46
|
|
|
if ($transaction instanceof Record) { |
|
47
|
|
|
return $transaction; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
return $this->createForPurchase($purchase); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Purchase|IsPurchasableModelTrait $purchase |
|
54
|
|
|
* @return TransactionTrait|null |
|
55
|
|
|
*/ |
|
56
|
|
|
public function findForPurchase($purchase) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->findOneByField('id_purchase', $purchase->id); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getStatusItemsRootNamespace() |
|
65
|
|
|
{ |
|
66
|
|
|
return '\ByTIC\Payments\Models\Transactions\Statuses\\'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getStatusItemsDirectory() |
|
73
|
|
|
{ |
|
74
|
|
|
return __DIR__ . DIRECTORY_SEPARATOR . 'Statuses'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Purchase|IsPurchasableModelTrait $purchase |
|
79
|
|
|
* @return TransactionTrait |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function createForPurchase($purchase) |
|
82
|
|
|
{ |
|
83
|
|
|
$transaction = $this->getNew(); |
|
84
|
|
|
$transaction->populateFromPayment($purchase); |
|
85
|
|
|
$transaction->populateFromGateway($purchase->getPaymentMethod()->getType()->getGateway()); |
|
|
|
|
|
|
86
|
|
|
$transaction->insert(); |
|
|
|
|
|
|
87
|
|
|
return $transaction; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function initRelations() |
|
91
|
|
|
{ |
|
92
|
|
|
parent::initRelations(); |
|
93
|
|
|
$this->initRelationsCommon(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function initRelationsCommon() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->initRelationsPurchase(); |
|
99
|
|
|
$this->initRelationsPaymentMethod(); |
|
100
|
|
|
$this->initRelationsSubscription(); |
|
101
|
|
|
$this->initRelationsToken(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function initRelationsPurchase() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function initRelationsSubscription() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->belongsTo('Subscription', ['class' => get_class(PaymentsModels::subscriptions())]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $params |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function injectParams(&$params = []) |
|
118
|
|
|
{ |
|
119
|
|
|
$params['order'][] = ['created', 'desc']; |
|
120
|
|
|
|
|
121
|
|
|
parent::injectParams($params); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return mixed|\Nip\Config\Config |
|
126
|
|
|
* @throws \Exception |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function generateTable() |
|
129
|
|
|
{ |
|
130
|
|
|
return config('payments.tables.transactions', \ByTIC\Payments\Models\Transactions\Transactions::TABLE); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|