-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.php
125 lines (109 loc) · 3.8 KB
/
class.php
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
<?
namespace PaySystem;
use Bitrix\Sale\Order;
class Sber
{
private const LOGIN = '*****';
private const PASSWORD = '*****';
private const SBER_URL = 'https://3dsec.sberbank.ru';
/**
* Проверка статуса оплаты заказа
* @param $orderId
* @return array
*/
public static function getOrderStatus(int $orderId) : array
{
$params = [
'userName' => self::LOGIN,
'password' => self::PASSWORD,
'orderId' => $orderId,
];
$ch = curl_init(self::SBER_URL.'/payment/rest/getOrderStatusExtended.do?'.http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, JSON_OBJECT_AS_ARRAY);
return $response;
}
/**
* Регистрация заказа в Сбере
* @param $orderId
* @return array
*/
public function registerOrder(int $orderId) : array
{
$order = $this->getOrder($orderId);
$params = [
'userName' => self::LOGIN,
'password' => self::PASSWORD,
// rand() добавлено, т.к. Сбер регистрирует заказ и повторно получить ссылку на оплату нельзя
'orderNumber' => $orderId.rand(),
'orderBundle' => $order['cart'],
'amount' => $order['amount'] * 100,
'returnUrl' => $this->getReturnUrl([
'ID' => $orderId,
]),
'description' => "Заказ № {$orderId} на сайте example.ru",
];
$ch = curl_init(self::SBER_URL.'/payment/rest/register.do?'.http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, JSON_OBJECT_AS_ARRAY);
return $result;
}
/**
* Получение корзины заказа, который был создан на сайте
* @param $orderId
* @return array
*/
protected function getOrder(int $orderId) : array
{
$order = Order::load($orderId);
$basket = $order->getBasket();
$basketItems = $basket->getBasketItems();
foreach ($basketItems as $num => $basketItem) {
$cart[] = [
'positionId' => $num + 1,
'name' => $basketItem->getField('NAME'),
'quantity' => array(
'value' => $basketItem->getQuantity(),
'measure' => 'шт',
),
'itemAmount' => $basketItem->getFinalPrice() * 100,
'itemCode' => $basketItem->getProductId(),
'tax' => array(
'taxType' => 0,
'taxSum' => 0,
),
'itemPrice' => $basketItem->getPrice() * 100,
];
}
$result['cart'] = json_encode(
[
'cartItems' => [
'items' => $cart,
],
],
JSON_UNESCAPED_UNICODE
);
$result['amount'] = $order->getField("PRICE");
return $result;
}
/**
* Ссылка для возврата после оплаты
* @param $params
* @return string
*/
public function getReturnUrl(array $params) : string
{
return implode('?', [
'https://example.ru/account/orders-history/order_detail.php',
http_build_query($params),
]);
}
}