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