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