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 getNew |
14
|
|
|
*/ |
15
|
|
|
trait TokensTrait |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public function findOrCreateForMethod($method, TokenInterface $token) |
19
|
|
|
{ |
20
|
|
|
$findToken = $this->findOneByParams( |
|
|
|
|
21
|
|
|
[ |
22
|
|
|
'where' => [ |
23
|
|
|
['id_method =?', $method], |
24
|
|
|
['token_id = ?', $token->getId()], |
25
|
|
|
] |
26
|
|
|
]); |
27
|
|
|
|
28
|
|
|
if ($findToken instanceof Record) { |
29
|
|
|
return $findToken; |
30
|
|
|
} |
31
|
|
|
$token = $this->getNew(); |
32
|
|
|
$token->populateFromPaymentMethod($method); |
33
|
|
|
$token->populateFromToken($token); |
|
|
|
|
34
|
|
|
$token->insert(); |
|
|
|
|
35
|
|
|
return $token; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function initRelations() |
39
|
|
|
{ |
40
|
|
|
parent::initRelations(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function initRelationsCommon() |
44
|
|
|
{ |
45
|
|
|
$this->initRelationsPurchase(); |
46
|
|
|
$this->initRelationsPaymentMethod(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function initRelationsPurchase() |
50
|
|
|
{ |
51
|
|
|
$this->belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function initRelationsPaymentMethod() |
55
|
|
|
{ |
56
|
|
|
$this->belongsTo('PaymentMethod', ['class' => get_class(PaymentsModels::methods())]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $params |
61
|
|
|
*/ |
62
|
|
|
protected function injectParams(&$params = []) |
63
|
|
|
{ |
64
|
|
|
$params['order'][] = ['created', 'desc']; |
65
|
|
|
|
66
|
|
|
parent::injectParams($params); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return mixed|\Nip\Config\Config |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
protected function generateTable() |
75
|
|
|
{ |
76
|
|
|
return config('payments.tables.tokens', Tokens::TABLE); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|