1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Models\Tokens; |
4
|
|
|
|
5
|
|
|
use ByTIC\Omnipay\Common\Models\TokenInterface; |
6
|
|
|
use ByTIC\Payments\Utility\PaymentsModels; |
7
|
|
|
use Nip\Records\AbstractModels\Record; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait TokensTrait |
11
|
|
|
* @package ByTIC\Payments\Models\Tokens |
12
|
|
|
* |
13
|
|
|
* @method TokenTrait|Token getNew |
14
|
|
|
*/ |
15
|
|
|
trait TokensTrait |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param $method |
20
|
|
|
* @param TokenInterface $token |
21
|
|
|
* @return TokenTrait|Record |
22
|
|
|
*/ |
23
|
|
|
public function findOrCreateForMethod($method, TokenInterface $token) |
24
|
|
|
{ |
25
|
|
|
$findToken = $this->findOneByParams( |
|
|
|
|
26
|
|
|
[ |
27
|
|
|
'where' => [ |
28
|
|
|
['id_method =?', is_object($method) ? $method->id : $method], |
29
|
|
|
['token_id = ?', $token->getId()], |
30
|
|
|
] |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
if ($findToken instanceof Record) { |
34
|
|
|
return $findToken; |
35
|
|
|
} |
36
|
|
|
$tokenRecord = $this->getNew(); |
37
|
|
|
$tokenRecord->populateFromPaymentMethod($method); |
38
|
|
|
$tokenRecord->populateFromToken($token); |
39
|
|
|
$tokenRecord->insert(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
return $this->createForMethod($method, $token); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $method |
46
|
|
|
* @param TokenInterface $token |
47
|
|
|
* @return Token|TokenTrait |
48
|
|
|
*/ |
49
|
|
|
protected function createForMethod($method, TokenInterface $token) |
50
|
|
|
{ |
51
|
|
|
$item = $this->getNew(); |
52
|
|
|
$item->populateFromPaymentMethod($method); |
53
|
|
|
$item->populateFromGateway($method->getType()->getGateway()); |
54
|
|
|
$item->populateFromToken($token); |
55
|
|
|
$item->insert(); |
56
|
|
|
return $item; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function initRelations() |
60
|
|
|
{ |
61
|
|
|
parent::initRelations(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function initRelationsCommon() |
65
|
|
|
{ |
66
|
|
|
$this->initRelationsPurchase(); |
67
|
|
|
$this->initRelationsPaymentMethod(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function initRelationsPurchase() |
71
|
|
|
{ |
72
|
|
|
$this->belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function initRelationsPaymentMethod() |
76
|
|
|
{ |
77
|
|
|
$this->belongsTo('PaymentMethod', ['class' => get_class(PaymentsModels::methods())]); |
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
|
|
|
/** |
92
|
|
|
* @return mixed|\Nip\Config\Config |
93
|
|
|
* @throws \Exception |
94
|
|
|
*/ |
95
|
|
|
protected function generateTable() |
96
|
|
|
{ |
97
|
|
|
return config('payments.tables.tokens', Tokens::TABLE); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|