-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtpay-functions.php
251 lines (211 loc) · 7.38 KB
/
tpay-functions.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
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
<?php
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Tpay\TpayBlik;
use Tpay\TpayGateways;
defined('ABSPATH') || exit();
/**
* @return mixed|null
*/
function tpayOption(?string $optionName = null, string $optionScope = 'tpay_settings_option_name')
{
$options = get_option($optionScope);
if (empty($options)) {
return null;
}
if ($optionName === null) {
return $options;
}
return $options[$optionName] ?? null;
}
function tpay_add_checkout_fee_for_gateway()
{
$chosen_gateway = WC()->session->get('chosen_payment_method');
$type = tpayOption('global_enable_fee');
if ($type == 'amount') {
$fee = tpayOption('global_amount_fee');
} else {
$percentage = tpayOption('global_percentage_fee');
$amount = WC()->cart->cart_contents_total + WC()->cart->shipping_total;
$fee = $percentage / 100 * $amount;
}
$fee = round($fee, 2);
if (array_key_exists($chosen_gateway, TpayGateways::gateways_list())) {
WC()->cart->add_fee(__('Transaction fee', 'tpay'), $fee);
}
}
function displayChildPluginNotice()
{
echo sprintf(
'<div class="error"><p>%s <a target="_blank" href="https://wordpress.org/plugins/woocommerce/">%s</a></p></div>',
__('Tpay requires a WooCommerce plugin,', 'tpay'),
__('download it', 'tpay')
);
}
function displayConfigPluginNotice()
{
echo sprintf('<div class="error"><p>%s</p></div>', 'Cannot modify wp-config.php, you have to do it manually and add WP_TPAY_HASH and WP_TPAY_BLIK_PREFIX');
}
function childPluginHasParentPlugin()
{
if (is_admin() && current_user_can('activate_plugins')) {
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
add_action('admin_notices', 'displayChildPluginNotice');
deactivate_plugins(plugin_basename(__FILE__));
if (filter_input(INPUT_GET, 'activate')) {
unset($_GET['activate']);
}
}
}
function enqueue_tpay_admin_assets()
{
wp_enqueue_script('tpay_admin_js', plugin_dir_url(__FILE__) . 'views/assets/admin.min.js', [], time());
wp_enqueue_style('tpay_admin_css', plugin_dir_url(__FILE__) . 'views/assets/admin.css', [], TPAY_PLUGIN_VERSION);
}
function buildInfo(): string
{
return sprintf(
'woocommerce:%s|wordpress:%s|tpay-woocommerce:%s|tpay-openapi-php:%s|PHP:%s',
WC()->version,
get_bloginfo('version'),
TPAY_PLUGIN_VERSION,
get_package_version(),
phpversion()
);
}
function enqueue_tpay_gateway_assets(): void
{
$asset = require plugin_dir_path(__FILE__) . 'views/assets/main.min.asset.php';
wp_register_script(
'tpay_gateway_js',
plugin_dir_url(__FILE__) . 'views/assets/main.min.js',
$asset['dependencies'],
$asset['version'],
true
);
wp_localize_script('tpay_gateway_js', 'tpay', [
'merchantId' => tpayOption('global_api_key'),
]);
wp_enqueue_script('tpay_gateway_js');
wp_enqueue_style('tpay_gateway_css', plugin_dir_url(__FILE__) . 'views/assets/main.css', [], time());
if (CartCheckoutUtils::is_checkout_block_default() || CartCheckoutUtils::is_cart_block_default()) {
$asset = require plugin_dir_path(__FILE__) . 'views/assets/installments-blocks.min.asset.php';
wp_register_script(
'tpay-installments-blocks',
plugin_dir_url(__FILE__) . 'views/assets/installments-blocks.min.js',
$asset['dependencies'],
$asset['version'],
true
);
wp_localize_script(
'tpay-installments-blocks',
'tpayInstallmentsBlocks',
[
'merchantId' => tpayOption(
'tpay_settings_installments_merchant_id',
'woocommerce_pekaoinstallments_settings'
),
'installments' => [
'cart' => tpayOption('tpay_settings_installments_cart', 'woocommerce_pekaoinstallments_settings'),
'checkout' => tpayOption(
'tpay_settings_installments_checkout',
'woocommerce_pekaoinstallments_settings'
),
],
'translations' => [
'button' => __('Calculate the installment!', 'tpay'),
],
]
);
wp_enqueue_script('tpay-installments-blocks');
}
}
function add_tpay_gateways($gateways)
{
return array_merge($gateways, array_values(TPAY_CLASSMAP));
}
function generate_random_string($length = 10): string
{
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function wp_config_put($slash = '')
{
$hash = generate_random_string(64);
$config = file_get_contents(ABSPATH . "wp-config.php");
if (strpos($config, 'WP_TPAY_HASH') === false) {
$config .= PHP_EOL . "define('WP_TPAY_HASH', '" . $hash . "');";
file_put_contents(ABSPATH . $slash . "wp-config.php", $config);
}
$hash = generate_random_string();
if (strpos($config, 'WP_TPAY_BLIK_PREFIX') === false) {
$config .= PHP_EOL . "define('WP_TPAY_BLIK_PREFIX', '" . $hash . "');";
file_put_contents(ABSPATH . $slash . "wp-config.php", $config);
}
}
function tpay_on_activate()
{
global $wpdb;
$sql = 'CREATE TABLE if not exists `' . $wpdb->prefix . 'tpay_cards` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`vendor` varchar(20) DEFAULT NULL,
`hash` text DEFAULT NULL,
`short_code` int(11) DEFAULT NULL,
`crc` varchar(64) DEFAULT NULL,
`token` varchar(64) DEFAULT NULL,
`source_order` int(11) DEFAULT NULL,
`valid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;';
$wpdb->get_results($sql);
}
function tpay_config_init()
{
if (file_exists(ABSPATH . "wp-config.php") && is_writable(ABSPATH . "wp-config.php")) {
wp_config_put();
} else {
if (file_exists(dirname(ABSPATH) . "/wp-config.php") && is_writable(dirname(ABSPATH) . "/wp-config.php")) {
wp_config_put('/');
} else {
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
add_action('admin_notices', 'displayConfigPluginNotice');
deactivate_plugins(plugin_basename(__DIR__ . '/tpay.php'));
if (filter_input(INPUT_GET, 'activate')) {
unset($_GET['activate']);
}
}
}
}
function tpay_refresh_checkout_on_payment_methods_change()
{
wc_enqueue_js(
"
$( 'form.checkout' ).on( 'change', 'input[name^=\'payment_method\']', function() {
$('body').trigger('update_checkout');
});
"
);
}
function get_package_version(): string
{
return \Tpay\Vendor\Composer\InstalledVersions::getPrettyVersion('tpay-com/tpay-openapi-php');
}
function tpay_blik0_transaction_status()
{
check_ajax_referer('tpay-thank-you', 'nonce');
$result = (new TpayBlik())->checkTransactionStatus(htmlspecialchars($_POST['transactionId']));
wp_send_json($result);
}
function tpay_lang(): string
{
$locale = explode('_', get_locale());
if (false === in_array($locale[0], ['pl', 'en', 'uk'])) {
return 'en';
}
return $locale[0];
}