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
|
|
|
|
12
|
|
|
class ApiTransactionRepository implements TransactionRepositoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ConnectionInterface |
16
|
|
|
*/ |
17
|
|
|
private $connection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ApiTransactionRepository constructor. |
21
|
|
|
* @param ConnectionInterface $connection |
22
|
|
|
*/ |
23
|
|
|
function __construct(ConnectionInterface $connection) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->connection = $connection; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $id |
30
|
|
|
* @return Transaction |
31
|
|
|
* @throws TransactionException when transaction with the specified ID |
32
|
|
|
* does not exists |
33
|
|
|
*/ |
34
|
|
|
public function findById($id) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$data = $this->connection->createCommand()->perform('merchantTransactionGet', null, ['id' => $id]); |
38
|
|
|
} catch (ResponseErrorException $e) { |
39
|
|
|
throw new TransactionException('Failed to get transaction information'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (empty($data)) { |
43
|
|
|
throw new TransactionException('Transaction not found'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->instantiate($data); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function insert($transaction) |
53
|
|
|
{ |
54
|
|
|
return $this->save($transaction); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function create($id, $merchant, $parameters) |
61
|
|
|
{ |
62
|
|
|
$transaction = new Transaction($id, $merchant); |
63
|
|
|
$transaction->setParameters($parameters); |
64
|
|
|
|
65
|
|
|
return $transaction; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function save($transaction) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
$data = $transaction->toArray(); |
75
|
|
|
$data['parameters'] = json_encode($data['parameters']); |
76
|
|
|
|
77
|
|
|
$this->connection->createCommand()->perform('merchantTransactionSet', null, $data); |
78
|
|
|
} catch (ResponseErrorException $e) { |
79
|
|
|
throw new TransactionException('Failed to save transaction'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $transaction; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $data |
87
|
|
|
* @return Transaction |
88
|
|
|
*/ |
89
|
|
|
protected function instantiate($data) |
90
|
|
|
{ |
91
|
|
|
$transaction = $this->create($data['id'], $data['merchant'], $data['parameters']); |
92
|
|
|
|
93
|
|
|
if ($data['status'] !== null) { |
94
|
|
|
$statusReflection = (new ReflectionObject($transaction))->getProperty('status'); |
95
|
|
|
$statusReflection->setAccessible(true); |
96
|
|
|
$statusReflection->setValue($transaction, $data['status']); |
97
|
|
|
$statusReflection->setAccessible(false); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $transaction; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.