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->initRelationsPurchase(); |
69
|
|
|
$this->initRelationsPaymentMethod(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function initRelationsPurchase() |
73
|
|
|
{ |
74
|
|
|
$this->belongsTo('Purchase', ['class' => get_class(PaymentsModels::purchases())]); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $params |
79
|
|
|
*/ |
80
|
|
|
protected function injectParams(&$params = []) |
81
|
|
|
{ |
82
|
|
|
$params['order'][] = ['created', 'desc']; |
83
|
|
|
|
84
|
|
|
parent::injectParams($params); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed|\Nip\Config\Config |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
protected function generateTable() |
92
|
|
|
{ |
93
|
|
|
return config('payments.tables.tokens', Tokens::TABLE); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return mixed|\Nip\Config\Config |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
|
|
protected function generateController() |
101
|
|
|
{ |
102
|
|
|
return Tokens::CONTROLLER; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|