Skip to content

Commit 216a1a1

Browse files
committed
v.1.0.0
1 parent 4490082 commit 216a1a1

8 files changed

Lines changed: 286 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

unitpay.zip

8.81 KB
Binary file not shown.
4.15 KB
Loading
610 Bytes
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
return array(
3+
'name' => 'Unitpay',
4+
'description' => 'Платежная система <a href="https://unitpay.ru" target="_blank">Unitpay</a>',
5+
'icon' => 'img/unitpay16.png',
6+
'logo' => 'img/unitpay.png',
7+
'vendor' => 'webasyst',
8+
'version' => '1.0.0',
9+
'locale' => array('ru_RU',),
10+
'type' => waPayment::TYPE_ONLINE,
11+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return array(
4+
'unit_public_key' => array(
5+
'value' => '',
6+
'title' => 'PUBLIC KEY',
7+
'description' => 'Скопируйте PUBLIC KEY со страницы проекта в системе Unitpay',
8+
'control_type' => 'input',
9+
'class' => 'keys',
10+
),
11+
'unit_secret_key' => array(
12+
'value' => '',
13+
'title' => 'SECRET KEY',
14+
'description' => 'Скопируйте SECRET KEY со страницы проекта в системе Unitpay',
15+
'control_type' => 'input',
16+
'class' => 'keys',
17+
));
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
<?php
2+
class unitpayPayment extends waPayment implements waIPayment
3+
{
4+
public function allowedCurrency()
5+
{
6+
$default = array(
7+
'RUB',
8+
'USD',
9+
'EUR',
10+
);
11+
return $default;
12+
}
13+
14+
public function payment($payment_form_data, $order_data, $auto_submit = false)
15+
{
16+
$result = null;
17+
$order = waOrder::factory($order_data);
18+
19+
$public_key = $this->unit_public_key;
20+
$sum = $order->total;
21+
$account = $order->id;
22+
$desc = $order->description;
23+
24+
$view = wa()->getView();
25+
$view->assign('public_key', $public_key);
26+
$view->assign('sum', $sum);
27+
$view->assign('account', $account);
28+
$view->assign('desc', $desc);
29+
30+
return $view->fetch($this->path.'/templates/payment.html');
31+
}
32+
33+
protected function callbackInit($request)
34+
{
35+
36+
$params = $request['params'];
37+
$this->order_id = $params['account'];
38+
return parent::callbackInit($request);
39+
}
40+
41+
protected function callbackHandler($data)
42+
{
43+
44+
45+
$method = '';
46+
$params = [];
47+
48+
if ((isset($data['params'])) && (isset($data['method'])) && (isset($data['params']['signature']))){
49+
$params = $data['params'];
50+
$method = $data['method'];
51+
$signature = $params['signature'];
52+
53+
if (empty($signature)){
54+
$status_sign = false;
55+
}else{
56+
$status_sign = $this->verifySignature($params, $method);
57+
}
58+
59+
}else{
60+
$status_sign = false;
61+
}
62+
63+
if ($status_sign){
64+
switch ($method) {
65+
case 'check':
66+
$result = $this->check( $params );
67+
break;
68+
case 'pay':
69+
$result = $this->pay( $params );
70+
break;
71+
case 'error':
72+
$result = $this->error( $params );
73+
74+
break;
75+
default:
76+
$result = array('error' =>
77+
array('message' => 'неверный метод')
78+
);
79+
break;
80+
}
81+
}else{
82+
$result = array('error' =>
83+
array('message' => 'неверная сигнатура')
84+
);
85+
}
86+
87+
$this->hardReturnJson($result);
88+
89+
}
90+
91+
function check( $params )
92+
{
93+
require_once __DIR__.'/../../../../wa-apps/shop/lib/model/shopOrder.model.php';
94+
$order_model = new shopOrderModel();
95+
$order_id = $this->order_id;
96+
$order = $order_model->getById( $order_id );
97+
98+
if (is_null($order_id)){
99+
$result = array('error' =>
100+
array('message' => '1заказа не существует')
101+
);
102+
}elseif ((float)$order['total'] != (float)$params['orderSum']) {
103+
$result = array('error' =>
104+
array('message' => 'не совпадает сумма заказа')
105+
);
106+
}elseif ($order['currency'] != $params['orderCurrency']) {
107+
$result = array('error' =>
108+
array('message' => 'не совпадает валюта заказа')
109+
);
110+
}
111+
else{
112+
$result = array('result' =>
113+
array('message' => 'Запрос успешно обработан')
114+
);
115+
}
116+
117+
return $result;
118+
119+
}
120+
121+
function pay( $params )
122+
{
123+
124+
require_once __DIR__.'/../../../../wa-apps/shop/lib/model/shopOrder.model.php';
125+
$order_model = new shopOrderModel();
126+
$order_id = $this->order_id;
127+
$order = $order_model->getById( $order_id );
128+
129+
if (is_null($order_id)){
130+
$result = array('error' =>
131+
array('message' => 'заказа не существует')
132+
);
133+
}elseif ((float)$order['total'] != (float)$params['orderSum']) {
134+
$result = array('error' =>
135+
array('message' => 'не совпадает сумма заказа')
136+
);
137+
}elseif ($order['currency'] != $params['orderCurrency']) {
138+
$result = array('error' =>
139+
array('message' => 'не совпадает валюта заказа')
140+
);
141+
}
142+
else{
143+
144+
/*$transaction_data = $this->formalizeData($params);
145+
$transaction_data['order_id'] = $order_id;
146+
$transaction_data['amount'] = $order['total'];
147+
$transaction_data['currency_id'] = $order['currency'];
148+
$transaction_data['plugin'] = 'unitpay';
149+
150+
$this->execAppCallback(waPayment::CALLBACK_PAYMENT, $transaction_data);*/
151+
152+
$update_order = [];
153+
$update_order['state_id'] = 'paid';
154+
$update_order = array_merge($update_order, [
155+
'paid_date' => date('Y-m-d'),
156+
'paid_year' => date('Y'),
157+
'paid_quarter' => floor((date('m') - 1) / 3) + 1,
158+
'paid_month' => (int)date('m'),
159+
]);
160+
161+
$order_model->updateById($order_id, $update_order);
162+
163+
$logs[] = array(
164+
'order_id' => $order_id,
165+
'action_id' => 'pay',
166+
'before_state_id' => $order['state_id'],
167+
'after_state_id' => $update_order['state_id'],
168+
'text' => '',
169+
// 'params' => array('merged_order_id' => $master_id),
170+
);
171+
172+
#add log records
173+
require_once __DIR__.'/../../../../wa-apps/shop/lib/model/shopOrderLog.model.php';
174+
$log_model = new shopOrderLogModel();
175+
foreach ($logs as $log) {
176+
$log_model->add($log);
177+
}
178+
179+
$result = array('result' =>
180+
array('message' => 'Запрос успешно обработан')
181+
);
182+
183+
}
184+
185+
return $result;
186+
187+
}
188+
189+
190+
function error( $params )
191+
{
192+
require_once __DIR__.'/../../../../wa-apps/shop/lib/model/shopOrder.model.php';
193+
$order_model = new shopOrderModel();
194+
$order_id = $this->order_id;
195+
$order = $order_model->getById( $order_id );
196+
197+
if (is_null($order['id'])){
198+
$result = array('error' =>
199+
array('message' => 'заказа не существует')
200+
);
201+
}
202+
else{
203+
204+
/*$transaction_data = $this->formalizeData($params);
205+
$transaction_data['order_id'] = $order_id;
206+
$transaction_data['amount'] = $order['total'];
207+
$transaction_data['currency_id'] = $order['currency'];
208+
$transaction_data['plugin'] = 'unitpay';
209+
210+
$this->execAppCallback(waPayment::CALLBACK_DECLINE, $transaction_data);*/
211+
212+
$result = array('result' =>
213+
array('message' => 'Запрос успешно обработан')
214+
);
215+
216+
}
217+
218+
return $result;
219+
}
220+
221+
222+
function verifySignature($params, $method)
223+
{
224+
225+
require_once __DIR__.'/../../../../wa-apps/shop/lib/model/shopPluginSettings.model.php';
226+
require_once __DIR__.'/../../../../wa-apps/shop/lib/model/shopSortable.model.php';
227+
require_once __DIR__.'/../../../../wa-apps/shop/lib/model/shopPlugin.model.php';
228+
$settings_model = new shopPluginSettingsModel();
229+
$plugin_model = new shopPluginModel();
230+
$plugin = $plugin_model->getByField('plugin', $this->id);
231+
232+
$secret = $settings_model->get($plugin['id'], 'unit_secret_key');
233+
234+
return $params['signature'] == $this->getSignature($method, $params, $secret);
235+
}
236+
237+
function getSignature($method, array $params, $secretKey)
238+
{
239+
ksort($params);
240+
unset($params['sign']);
241+
unset($params['signature']);
242+
array_push($params, $secretKey);
243+
array_unshift($params, $method);
244+
245+
return hash('sha256', join('{up}', $params));
246+
}
247+
248+
protected function hardReturnJson( $arr )
249+
{
250+
header('Content-Type: application/json');
251+
$result = json_encode($arr);
252+
die($result);
253+
}
254+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="pay-button">
2+
<a href="https://unitpay.ru/pay/{$public_key}?sum={$sum}&account={$account}&desc={$desc}" style="background-color: #ffa500; padding: 10px 20px; color: white">Оплатить</a>
3+
</div>

0 commit comments

Comments
 (0)