1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\transaction; |
4
|
|
|
|
5
|
|
|
use hiqdev\hiart\ConnectionInterface; |
6
|
|
|
use hiqdev\hiart\ResponseErrorException; |
7
|
|
|
use hiqdev\yii2\merchant\transactions\Transaction; |
8
|
|
|
use hiqdev\yii2\merchant\transactions\TransactionException; |
9
|
|
|
use hiqdev\yii2\merchant\transactions\TransactionRepositoryInterface; |
10
|
|
|
use ReflectionObject; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
|
13
|
|
|
class ApiTransactionRepository implements TransactionRepositoryInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ConnectionInterface |
17
|
|
|
*/ |
18
|
|
|
private $connection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ApiTransactionRepository constructor. |
22
|
|
|
* @param ConnectionInterface $connection |
23
|
|
|
*/ |
24
|
|
|
function __construct(ConnectionInterface $connection) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->connection = $connection; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $id |
31
|
|
|
* @return Transaction |
32
|
|
|
* @throws TransactionException when transaction with the specified ID |
33
|
|
|
* does not exists |
34
|
|
|
*/ |
35
|
|
|
public function findById($id) |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
$data = $this->callWithoutAuth(function () use ($id) { |
39
|
|
|
return $this->connection->createCommand()->perform('merchantTransactionGet', null, ['id' => $id]); |
40
|
|
|
}); |
41
|
|
|
} catch (ResponseErrorException $e) { |
42
|
|
|
throw new TransactionException('Failed to get transaction information'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (empty($data)) { |
46
|
|
|
throw new TransactionException('Transaction not found'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->instantiate($data); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function insert($transaction) |
56
|
|
|
{ |
57
|
|
|
return $this->save($transaction); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function create($id, $merchant, $parameters) |
64
|
|
|
{ |
65
|
|
|
$transaction = new Transaction($id, $merchant); |
66
|
|
|
$transaction->setParameters($parameters); |
67
|
|
|
|
68
|
|
|
return $transaction; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
|
public function save($transaction) |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
$data = $transaction->toArray(); |
78
|
|
|
$data['parameters'] = Json::encode($data['parameters']); |
79
|
|
|
|
80
|
|
|
$this->callWithoutAuth(function () use ($data) { |
81
|
|
|
return $this->connection->createCommand()->perform('merchantTransactionSet', null, $data); |
82
|
|
|
}); |
83
|
|
|
} catch (ResponseErrorException $e) { |
84
|
|
|
throw new TransactionException('Failed to save transaction'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $transaction; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function callWithoutAuth(\Closure $function) |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$this->connection->disableAuth(); |
94
|
|
|
return call_user_func($function); |
95
|
|
|
} finally { |
96
|
|
|
$this->connection->enableAuth(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $data |
102
|
|
|
* @return Transaction |
103
|
|
|
*/ |
104
|
|
|
protected function instantiate($data) |
105
|
|
|
{ |
106
|
|
|
$transaction = $this->create($data['id'], $data['merchant'], $data['parameters']); |
107
|
|
|
|
108
|
|
|
if ($data['success'] !== null) { |
109
|
|
|
$successReflection = (new ReflectionObject($transaction))->getProperty('success'); |
110
|
|
|
$successReflection->setAccessible(true); |
111
|
|
|
$successReflection->setValue($transaction, $data['success']); |
112
|
|
|
$successReflection->setAccessible(false); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $transaction; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.