-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfondyHandler.php
More file actions
297 lines (273 loc) · 11.9 KB
/
fondyHandler.php
File metadata and controls
297 lines (273 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
class Shop_Payment_System_HandlerXX extends Shop_Payment_System_Handler
{
// -------------------------------------------------------------------------------------------------
// Настройки модуля
// -------------------------------------------------------------------------------------------------
protected $_fondy_merchant_id = 'ID мерчанта'; // ID мерчанта, в лчином кабинете Fondy
protected $_fondy_redirect_mode = 0; // 1 - с перенаправлением, 0 - без перенаправления
protected $_fondy_secret_key = 'секретный ключ'; // секретный ключ
protected $_fondy_language = 'ru'; // язык, платежной системы
// id валюты, в которой будет производиться платеж
protected $_fondy_currency_id = 1; // 1 - рубли (RUR), 2 - евро (EUR), 3 - доллары (USD)
// -------------------------------------------------------------------------------------------------
// конец настроек
// -------------------------------------------------------------------------------------------------
protected $_fondy_coefficient = 1; // коэффициент перерасчета при оплате
/**
* @return Shop_Payment_System_Handler|Shop_Payment_System_Handler9
* Метод, запускающий выполнение обработчика. Вызывается на 4-ом шаге оформления заказа
*/
public function execute()
{
parent::execute();
$this->printNotification();
return $this;
}
/**
* @return Shop_Payment_System_Handler|Shop_Payment_System_Handler9
*
*/
protected function _processOrder()
{
parent::_processOrder();
// Установка XSL-шаблонов в соответствии с настройками в узле структуры
$this->setXSLs();
// Отправка писем клиенту и пользователю
$this->send();
return $this;
}
/**
* @return mixed
* вычисление суммы товаров заказа
*/
public function getSumWithCoeff()
{
return Shop_Controller::instance()->round(($this->_fondy_currency_id > 0
&& $this->_shopOrder->shop_currency_id > 0
? Shop_Controller::instance()->getCurrencyCoefficientInShopCurrency(
$this->_shopOrder->Shop_Currency,
Core_Entity::factory('Shop_Currency', $this->_fondy_currency_id)
)
: 0) * $this->_shopOrder->getAmount() * $this->_fondy_coefficient);
}
/**
* Обработчик callback
*/
public function checkPaymentBeforeContent()
{
if (isset($_REQUEST['paymentcallback']) and $_REQUEST['paymentcallback'] == 'fondy') {
if (empty($_POST)) {
$input = json_decode(file_get_contents("php://input"));
$_POST = array();
foreach ($input as $key => $val) {
$_POST[$key] = $val;
}
}
if (isset($_POST['order_status']) and $_POST['order_status'] == 'approved') {
$order_id = explode('#', $_POST['order_id']); // Номер заказа
$oShop_Order = Core_Entity::factory('Shop_Order')->find($order_id[0]);
if (!is_null($oShop_Order->id)) {
// Вызов обработчика платежной системы
Shop_Payment_System_Handler::factory($oShop_Order->Shop_Payment_System)
->shopOrder($oShop_Order)
->paymentProcessing();
}
exit();
}
}
}
/**
* @return mixed|void
*/
public function getInvoice()
{
return $this->getNotification();
}
/**
* @return mixed|void
* печатает форму отправки запроса на сайт платёжной системы
*/
public function getNotification()
{
$order_id = $this->_shopOrder->id; // номер заказа
$description = 'Оплата заказа №' . $order_id; // описание покупки
$oShop_Currency = Core_Entity::factory('Shop_Currency')->find($this->_fondy_currency_id);
$currency_code = $oShop_Currency->code; // валюта
if ($currency_code == 'RUR') {
$currency_code = 'RUB';
}
$currency_name = $oShop_Currency->name; // валюта
$oSite_Alias = $this->_shopOrder->Shop->Site->getCurrentAlias();
$site_alias = !is_null($oSite_Alias) ? $oSite_Alias->name : '';
$shop_path = $this->_shopOrder->Shop->Structure->getPath();
$protocol = isset($_SERVER["HTTPS"]) ? 'https://' : 'http://';
$server_callback_url = $protocol . $site_alias . $shop_path . 'cart/?paymentcallback=fondy'; //url на который будет отправлено уведомление о состоянии платежа
$response_url = $protocol . $site_alias . $shop_path . 'cart/?order_id=' . $order_id . '&payment=success'; //url на который будет перенаправлен плательщик после успешной оплаты
$formFields = array(
'order_id' => $order_id . FondyForm::ORDER_SEPARATOR . time(),
'merchant_id' => $this->_fondy_merchant_id, // id мерчанта
'order_desc' => $description,
'currency' => $currency_code,
'server_callback_url' => $server_callback_url,
'response_url' => $response_url,
'lang' => $this->_fondy_language, // язык, который будет использован на мерчанте
);
$formFields['amount'] = round($this->getSumWithCoeff() * 100);
$formFields['signature'] = FondyForm::getSignature($formFields, $this->_fondy_secret_key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.fondy.eu/api/checkout/url/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('request' => $formFields)));
$result = json_decode(curl_exec($ch));
if ($result->response->response_status == 'failure') {
echo $result->response->error_message;
exit;
}
if ($this->_fondy_redirect_mode == 1) {
return '<a id="fdy_pay" class="btn btn-primary" href="'.$result->response->checkout_url .'">Оплатить</a>
<script>document.getElementById("fdy_pay").click();</script>';
} else {
ob_start();
?>
<script src="https://api.fondy.eu/static_common/v1/checkout/ipsp.js"></script>
<div id="checkout">
<div id="checkout_wrapper"></div>
</div>
<script>
var checkoutStyles = {
"html , body": {
"overflow": "hidden"
},
".col.col-shoplogo": {
"display": "none"
},
".col.col-language": {
"display": "none"
},
".pages-checkout": {
"background": "transparent"
},
".col.col-login": {
"display": "none"
},
".pages-checkout .page-section-overview": {
"background": "#fff",
"color": "#252525",
"border-bottom": "1px solid #dfdfdf"
},
".col.col-value.order-content": {
"color": "#252525"
},
".page-section-footer": {
"display": "none"
},
".page-section-tabs": {
"display": "none"
},
".page-section-shopinfo": {
"display": "none"
},
};
function checkoutInit(url, val) {
$ipsp("checkout").scope(function () {
this.setCheckoutWrapper("#checkout_wrapper");
this.addCallback(__DEFAULTCALLBACK__);
this.setCssStyle(checkoutStyles);
this.action("show", function (data) {
$("#checkout_loader").remove();
$("#checkout").show();
});
this.action("hide", function (data) {
$("#checkout").hide();
});
this.action("resize", function (data) {
$("#checkout_wrapper").width("100%").height(data.height);
});
this.loadUrl(url);
});
};
checkoutInit('<?php echo $result->response->checkout_url ?>');
</script>
<?php
return ob_get_clean();
}
}
/**
* @return bool
* обработка ответа от платёжной системы
*/
public function paymentProcessing()
{
$this->ProcessResult();
return TRUE;
}
/**
* @return bool
* оплачивает заказ
*/
function ProcessResult()
{
if (!$_POST['order_status']) {
return false;
}
$oplataSettings = array('merchant_id' => $this->_fondy_merchant_id,
'secret_key' => $this->_fondy_secret_key,
);
$isPaymentValid = FondyForm::isPaymentValid($oplataSettings, $_POST);
if ($isPaymentValid == true) {
$oShop_Order = $this->_shopOrder;
$oShop_Order->system_information = "Заказ оплачен через сервис Fondy. ID заказа в системе: " . $_POST['payment_id'] . "\n";
$oShop_Order->paid();
$this->setXSLs();
$this->send();
echo 'Ok';
} else {
echo $isPaymentValid;
}
}
}
class FondyForm
{
const ORDER_SEPARATOR = '#';
const SIGNATURE_SEPARATOR = '|';
const ORDER_APPROVED = 'approved';
public static function getSignature($data, $password, $encoded = true)
{
$data = array_filter($data, function ($var) {
return $var !== '' && $var !== null;
});
ksort($data);
$str = $password;
foreach ($data as $k => $v) {
$str .= FondyForm::SIGNATURE_SEPARATOR . $v;
}
if ($encoded) {
return sha1($str);
} else {
return $str;
}
}
public static function isPaymentValid($oplataSettings, $response)
{
if ($oplataSettings['merchant_id'] != $response['merchant_id']) {
return 'An error has occurred during payment. Merchant data is incorrect.';
}
if ($response['order_status'] != FondyForm::ORDER_APPROVED) {
return 'An error has occurred during payment. Order is declined.';
}
$responseSignature = $response['signature'];
if (isset($response['response_signature_string'])) {
unset($response['response_signature_string']);
}
if (isset($response['signature'])) {
unset($response['signature']);
}
if (FondyForm::getSignature($response, $oplataSettings['secret_key']) != $responseSignature) {
return 'An error has occurred during payment. Signature is not valid.';
}
return true;
}
}