-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockBee.php
180 lines (150 loc) · 5.87 KB
/
BlockBee.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
<?php
/*
Plugin Name: BlockBee Cryptocurrency Payment Gateway
Plugin URI: https://blockbee.io/resources/woocommerce/
Description: Accept cryptocurrency payments on your WooCommerce website
Version: 1.4.1
Requires at least: 5.8
Tested up to: 6.7.1
WC requires at least: 5.8
WC tested up to: 9.5.1
Requires PHP: 7.2
Author: BlockBee
Author URI: https://blockbee.io/
License: MIT
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
define('BLOCKBEE_PLUGIN_VERSION', '1.4.1');
define('BLOCKBEE_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('BLOCKBEE_PLUGIN_URL', plugin_dir_url(__FILE__));
// Custom Autoloader
spl_autoload_register(function ($class) {
$prefix = 'BlockBee\\';
$base_dir = __DIR__ . '/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
// Check WooCommerce and PHP requirements
add_action('plugins_loaded', function () {
if (!class_exists('WC_Payment_Gateway')) {
return;
}
if (!class_exists('WooCommerce')) {
add_action('admin_notices', function () {
echo '<div class="error"><p><strong>' . sprintf(
esc_html__('BlockBee requires WooCommerce to be installed and active. You can download %s here.', 'blockbee-cryptocurrency-payment-gateway'),
'<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>'
) . '</strong></p></div>';
});
return;
}
if (!extension_loaded('bcmath')) {
add_action('admin_notices', function () {
echo '<div class="error"><p><strong>' . sprintf(
esc_html__('BlockBee requires PHP\'s BCMath extension. Learn more about it %s.', 'blockbee-cryptocurrency-payment-gateway'),
'<a href="https://www.php.net/manual/en/book.bc.php" target="_blank">here</a>'
) . '</strong></p></div>';
});
return;
}
$register = new \BlockBee\Register();
$register->register();
$initialize = new \BlockBee\Initialize();
$initialize->initialize();
$blockbee = new \BlockBee\Controllers\WC_BlockBee_Gateway();
});
register_activation_hook(__FILE__, function () {
if (!wp_next_scheduled('blockbee_cronjob')) {
wp_schedule_event(time(), 'hourly', 'blockbee_cronjob');
}
});
register_deactivation_hook(__FILE__, function () {
wp_clear_scheduled_hook('blockbee_cronjob');
});
use Automattic\WooCommerce\Utilities\FeaturesUtil;
// Declare compatibility with WooCommerce features
add_action('before_woocommerce_init', function () {
if (class_exists(FeaturesUtil::class)) {
FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
FeaturesUtil::declare_compatibility('cart_checkout_blocks', __FILE__, true);
}
});
// Pass BlockBee coin into legacy "process_payment"
add_filter('woocommerce_rest_checkout_process_payment_method_data', function($payment_method_data, $request) {
if (isset($payment_method_data['blockbee_coin'])) {
WC()->session->set('blockbee_coin', sanitize_text_field($payment_method_data['blockbee_coin']));
$_POST['blockbee_coin'] = sanitize_text_field($payment_method_data['blockbee_coin']);
}
return $payment_method_data;
}, 10, 2);
// Register minimum endpoint to be used in the blocks
add_action('rest_api_init', function () {
register_rest_route('blockbee/v1', '/get-minimum', array(
'methods' => 'POST',
'callback' => 'blockbee_get_minimum',
'permission_callback' => 'blockbee_verify_nonce',
));
register_rest_route('blockbee/v1', '/update-coin', array(
'methods' => 'POST',
'callback' => 'blockbee_update_coin',
'permission_callback' => 'blockbee_verify_nonce',
));
});
function blockbee_verify_nonce(WP_REST_Request $request) {
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if ($origin !== home_url()) {
return false;
}
$nonce = $request->get_header('X-WP-Nonce');
return wp_verify_nonce($nonce, 'wp_rest');
}
function blockbee_get_minimum(WP_REST_Request $request) {
$coin = sanitize_text_field($request->get_param('coin'));
$fiat = sanitize_text_field($request->get_param('fiat'));
$value = sanitize_text_field($request->get_param('value'));
if (!$coin) {
return new WP_REST_Response(['status' => 'error'], 400);
}
try {
$convert = (float) \BlockBee\Utils\Api::get_conversion($fiat, $coin, (string) $value, false);
$minimum = (float) \BlockBee\Utils\Api::get_info($coin)->minimum_transaction_coin;
if ($convert > $minimum) {
return new WP_REST_Response(['status' => 'success'], 200);
} else {
return new WP_REST_Response(['status' => 'error'], 200);
}
} catch (Exception $e) {
return new WP_REST_Response(['status' => 'error'], 500);
}
}
function blockbee_update_coin(WP_REST_Request $request) {
$coin = sanitize_text_field($request->get_param('coin'));
$selected = $request->get_param('selected', false);
// Ensure WooCommerce session is available
if (!WC()->session) {
$session_handler = new \WC_Session_Handler();
$session_handler->init();
WC()->session = $session_handler;
}
if (!$selected) {
WC()->session->set('blockbee_coin', 'none');
WC()->session->set('chosen_payment_method', '');
return new WP_REST_Response(['success' => true, 'coin' => $coin], 200);
}
if (!$coin) {
return new WP_REST_Response(['error' => 'Coin not specified'], 400);
}
// Set the session value
WC()->session->set('blockbee_coin', $coin);
WC()->session->set('chosen_payment_method', 'blockbee');
return new WP_REST_Response(['success' => true, 'coin' => $coin], 200);
}