forked from payuru/php-payu4
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWidget.php
More file actions
135 lines (126 loc) · 4.99 KB
/
Widget.php
File metadata and controls
135 lines (126 loc) · 4.99 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
<?php
declare(strict_types=1);
namespace Ypmn;
/**
* Класс для работы с виджетом.
*/
final class Widget
{
// Код мерчанта.
public string $merchantCode;
// Код страны.
public string $countryCode;
// Имя.
public string $firstName;
// Фамилия.
public string $lastName;
// Email.
public string $email;
// Телефон.
public string $phone;
// Ссылка возврата.
public string $returnUrl;
/**
* @param string $merchantCode Код мерчанта.
* @param string $countryCode Код страны.
* @param string $firstName Имя.
* @param string $lastName Фамилия.
* @param string $email Email.
* @param string $phone Телефон.
* @param string $returnUrl Ссылка возврата.
*/
public function __construct(
string $merchantCode,
string $countryCode = "RU",
string $firstName = "Not",
string $lastName = "Set",
string $phone = "9990000000",
string $returnUrl = "https://ya.ru/"
) {
$this->merchantCode = $merchantCode;
$this->countryCode = $countryCode;
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->phone = $phone;
$this->returnUrl = $returnUrl;
}
/**
* Метод возвращает кнопку покупки.
*
* @param float $amounts Сумма к оплате.
* @param string $currency Валюта.
* @param string $label Надпись на кнопке.
* @param string $classes Классы кнопки.
*
* @return string
*/
public function makeBuyButton(
float $amount,
string $currency = "RUB",
string $label = "Купить",
string $classes = ""
): string {
$classes = trim("buyBtn " . $classes);
$params = "data-amount='{$amount}' data-currency='{$currency}'";
return "<a href='#' class='{$classes}' {$params}>{$label}</a>";
}
/**
* Метод возвращает скрипты для вставки в html.
*
* @return string
*/
public function makeBuyForm(): string
{
return "<script src='https://secure.ypmn.ru/pay/widget/v1/js/YPMNFrames.js'></script>
<script src='https://secure.ypmn.ru/assets/js/crypto-js/4.2.0/crypto-js.min.js'></script>
<script>
window.document.body.onload = function() {
const payButtons = document.getElementsByClassName('buyBtn');
for (let payButton of payButtons) {
payButton.addEventListener('click', async () => {
const ypmn = new YPMNFrames(
{
merchantCode: '{$this->merchantCode}',
countryCode: '{$this->countryCode}',
currency: payButton.getAttribute('data-currency'),
merchantPaymentReference: '{$this->merchantCode}' + Math.floor(Math.random() * 1000) + Date.now(),
firstName: '{$this->firstName}',
lastName: '{$this->lastName}',
email: '{$this->email}',
phone: '{$this->phone}',
paymentSum: payButton.getAttribute('data-amount'),
returnUrl: '{$this->returnUrl}',
paymentMethod: 'CCVISAMC',
signature: '',
},
document.body.clientWidth,
Math.max(document.body.scrollHeight, window.screen.availHeight)
);
// Подписываем параметры
ypmn.data.payment.signature = doSigned(ypmn.data.payment);
ypmn.start();
});
}
};
if (window.location.hash === '#close') {
window.top.postMessage('SIGHUP', '*');
}
window.addEventListener('hashchange', (event) => {
if (window.location.hash === '#close') {
window.top.postMessage('SIGHUP', '*');
}
});
function doSigned(params) {
let raw = '';
Object.keys(params).forEach(k => {
if (k !== 'returnUrl' && k !== 'signature') {
raw += params[k];
}
});
return CryptoJS.SHA256(raw).toString();
}
</script>";
}
}