PluginProbe
Robokassa payment gateway for Woocommerce / trunk
Robokassa payment gateway for Woocommerce vtrunk
1.8.8 1.8.9 trunk
robokassa / blocks.js

blocks.js in Robokassa payment gateway for Woocommerce trunk, at blocks.js

237 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 const paymentMethods = [
2 {
3 name: 'robokassa',
4 fallback: window.wp.i18n.__('Оплата через Robokassa', 'robokassa'),
5 },
6 {
7 name: 'robokassa_credit',
8 fallback: window.wp.i18n.__('Рассрочка или кредит', 'robokassa'),
9 },
10 {
11 name: 'robokassa_podeli',
12 fallback: window.wp.i18n.__('Robokassa Х Подели', 'robokassa'),
13 },
14 {
15 name: 'robokassa_mokka',
16 fallback: window.wp.i18n.__('Robokassa Х Mokka', 'robokassa'),
17 },
18 {
19 name: 'robokassa_split',
20 fallback: window.wp.i18n.__('Robokassa X Яндекс Сплит', 'robokassa'),
21 },
22 {
23 name: 'robokassa_sbp',
24 fallback: window.wp.i18n.__('Оплата через QR-код СБП', 'robokassa'),
25 },
26 ];
27
28 paymentMethods.forEach((config) => {
29 const settings = window.wc.wcSettings.getSetting(`${config.name}_data`, null);
30 if (!settings) {
31 return;
32 }
33
34 const label = window.wp.htmlEntities.decodeEntities(
35 settings.title || config.fallback
36 );
37
38 const Content = () => {
39 const description = window.wp.htmlEntities.decodeEntities(settings.description || '');
40
41 if (!description) {
42 return null;
43 }
44
45 if (window.wp.element.RawHTML) {
46 return Object(window.wp.element.createElement)(
47 window.wp.element.RawHTML,
48 null,
49 description
50 );
51 }
52
53 return Object(window.wp.element.createElement)('div', {
54 dangerouslySetInnerHTML: { __html: description },
55 });
56 };
57
58 window.wc.wcBlocksRegistry.registerPaymentMethod({
59 name: config.name,
60 label,
61 content: Object(window.wp.element.createElement)(Content, null),
62 edit: Object(window.wp.element.createElement)(Content, null),
63 canMakePayment: () => true,
64 ariaLabel: label,
65 supports: {
66 features: settings.supports || [],
67 },
68 });
69 });
70
71 const ROBOKASSA_QUERY_MAPPING = {
72 podeli: 'robokassa_podeli',
73 otp: 'robokassa_credit',
74 mokka: 'robokassa_mokka',
75 yandexpaysplit: 'robokassa_split',
76 sbp: 'robokassa_sbp',
77 };
78
79 function robokassaNormalizeGateway(label) {
80 if (!label) {
81 return '';
82 }
83
84 const normalized = String(label).toLowerCase();
85
86 if (Object.prototype.hasOwnProperty.call(ROBOKASSA_QUERY_MAPPING, normalized)) {
87 return ROBOKASSA_QUERY_MAPPING[normalized];
88 }
89
90 return normalized.indexOf('robokassa') === 0 ? normalized : '';
91 }
92
93 function robokassaGetGatewayFromSearch(searchParams) {
94 const fromParam = robokassaNormalizeGateway(searchParams.get('payment_method'));
95
96 if (fromParam) {
97 return fromParam;
98 }
99
100 return searchParams.get('source') === 'robokassa_widget' ? 'robokassa' : '';
101 }
102
103 function robokassaGetCheckoutDispatcher() {
104 if (!window.wp || !window.wp.data || typeof window.wp.data.dispatch !== 'function') {
105 return null;
106 }
107
108 return window.wp.data.dispatch('wc/store/checkout') || null;
109 }
110
111 function robokassaInvokeCheckoutSelection(dispatcher, gatewayId) {
112 const actions = [
113 'setSelectedPaymentMethod',
114 '__experimentalSetSelectedPaymentMethod',
115 'selectPaymentMethod',
116 'setPaymentMethod',
117 ];
118
119 for (let index = 0; index < actions.length; index += 1) {
120 const action = actions[index];
121
122 if (typeof dispatcher[action] === 'function') {
123 dispatcher[action](gatewayId);
124 return true;
125 }
126 }
127
128 return false;
129 }
130
131 function robokassaIsSelectionConfirmed(gatewayId) {
132 if (!window.wp || !window.wp.data || typeof window.wp.data.select !== 'function') {
133 return true;
134 }
135
136 const selector = window.wp.data.select('wc/store/checkout');
137
138 if (!selector || typeof selector.getSelectedPaymentMethod !== 'function') {
139 return true;
140 }
141
142 const selected = selector.getSelectedPaymentMethod();
143
144 if (!selected) {
145 return false;
146 }
147
148 if (typeof selected === 'string') {
149 return selected === gatewayId;
150 }
151
152 return Boolean(selected.name === gatewayId);
153 }
154
155 function robokassaSelectGateway(gatewayId) {
156 const dispatcher = robokassaGetCheckoutDispatcher();
157
158 if (!dispatcher) {
159 return false;
160 }
161
162 if (!robokassaInvokeCheckoutSelection(dispatcher, gatewayId)) {
163 return false;
164 }
165
166 return robokassaIsSelectionConfirmed(gatewayId);
167 }
168
169 function robokassaEnsureGatewaySelection(gatewayId, attempt) {
170 if (!gatewayId) {
171 return;
172 }
173
174 if (robokassaSelectGateway(gatewayId)) {
175 return;
176 }
177
178 if (attempt >= 10) {
179 return;
180 }
181
182 window.setTimeout(function retrySelection() {
183 robokassaEnsureGatewaySelection(gatewayId, attempt + 1);
184 }, 200);
185 }
186
187 function robokassaInitGatewaySelection() {
188 const params = new window.URLSearchParams(window.location.search);
189 const gatewayId = robokassaGetGatewayFromSearch(params);
190
191 robokassaEnsureGatewaySelection(gatewayId, 0);
192 }
193
194 // === Robokassa Debug & Fallback Auto-Selection ===
195 window.addEventListener('DOMContentLoaded', () => {
196 const urlParams = new URLSearchParams(window.location.search);
197 const methodParam = urlParams.get('payment_method');
198
199 if (!methodParam) return;
200
201 const mapping = {
202 podeli: 'robokassa_podeli',
203 otp: 'robokassa_credit',
204 mokka: 'robokassa_mokka',
205 yandexpaysplit: 'robokassa_split',
206 sbp: 'robokassa_sbp',
207 };
208
209 const methodKey = mapping[methodParam.toLowerCase()];
210
211 if (!methodKey) {
212 return;
213 }
214
215 const interval = setInterval(() => {
216 const paymentInputs = document.querySelectorAll('input[name="radio-control-wc-payment-method-options"]');
217
218 for (const input of paymentInputs) {
219 if (input.value === methodKey) {
220 input.click();
221 clearInterval(interval);
222 return;
223 }
224 }
225 }, 300);
226
227 setTimeout(() => {
228 clearInterval(interval);
229 }, 5000);
230 });
231
232 if (window.wp && typeof window.wp.domReady === 'function') {
233 window.wp.domReady(robokassaInitGatewaySelection);
234 } else {
235 document.addEventListener('DOMContentLoaded', robokassaInitGatewaySelection);
236 }
237