1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Models\Tokens; |
4
|
|
|
|
5
|
|
|
use ByTIC\Omnipay\Common\Models\TokenInterface; |
6
|
|
|
use ByTIC\Payments\Models\AbstractModels\HasCustomer\HasCustomerRepository; |
7
|
|
|
use ByTIC\Payments\Models\AbstractModels\HasPaymentMethod\HasPaymentMethodRepository; |
8
|
|
|
use ByTIC\Payments\Utility\PaymentsModels; |
9
|
|
|
use Nip\Records\AbstractModels\Record; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait TokensTrait |
13
|
|
|
* @package ByTIC\Payments\Models\Tokens |
14
|
|
|
* |
15
|
|
|
* @method TokenTrait|Token getNew |
16
|
|
|
*/ |
17
|
|
|
trait TokensTrait |
18
|
|
|
{ |
19
|
|
|
use HasCustomerRepository; |
20
|
|
|
use HasPaymentMethodRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $method |
24
|
|
|
* @param TokenInterface $token |
25
|
|
|
* @return TokenTrait|Record |
26
|
|
|
*/ |
27
|
|
|
public function findOrCreateForMethod($method, TokenInterface $token) |
28
|
|
|
{ |
29
|
|
|
$findToken = $this->findOneByParams( |
|
|
|
|
30
|
|
|
[ |
31
|
|
|
'where' => [ |
32
|
|
|
['id_method =?', is_object($method) ? $method->id : $method], |
33
|
|
|
['token_id = ?', $token->getId()], |
34
|
|
|
] |
35
|
|
|
] |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
if ($findToken instanceof Record) { |
39
|
|
|
return $findToken; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->createForMethod($method, $token); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $method |
47
|
|
|
* @param TokenInterface $token |
48
|
|
|
* @return Token|TokenTrait |
49
|
|
|
*/ |
50
|
|
|
protected function createForMethod($method, TokenInterface $token) |
51
|
|
|
{ |
52
|
|
|
$item = $this->getNew(); |
53
|
|
|
$item->populateFromPaymentMethod($method); |
54
|
|
|
$item->populateFromGateway($method->getType()->getGateway()); |
55
|
|
|
$item->populateFromToken($token); |
56
|
|
|
$item->insert(); |
|
|
|
|
57
|
|
|
return $item; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function initRelations() |
61
|
|
|
{ |
62
|
|
|
parent::initRelations(); |
63
|
|
|
$this->initRelationsCommon(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function initRelationsCommon() |
67
|
|
|
{ |
68
|
|
|
$this->initRelationsTransactions(); |
69
|
|
|
$this->initRelationsPaymentMethod(); |
70
|
|
|
$this->initRelationsCustomer(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function initRelationsTransactions() |
74
|
|
|
{ |
75
|
|
|
$this->hasMany('Transactions', ['class' => get_class(PaymentsModels::transactions()), 'fk' => 'id_token']); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $params |
80
|
|
|
*/ |
81
|
|
|
protected function injectParams(&$params = []) |
82
|
|
|
{ |
83
|
|
|
$params['order'][] = ['created', 'desc']; |
84
|
|
|
|
85
|
|
|
parent::injectParams($params); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return mixed|\Nip\Config\Config |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
|
|
protected function generateTable() |
93
|
|
|
{ |
94
|
|
|
return config('payments.tables.tokens', Tokens::TABLE); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return mixed|\Nip\Config\Config |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
|
protected function generateController() |
102
|
|
|
{ |
103
|
|
|
return Tokens::CONTROLLER; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|