|
3 | 3 | Plugin Name: CryptAPI Payment Gateway for WooCommerce |
4 | 4 | Plugin URI: https://github.com/cryptapi/woocommerce-cryptapi |
5 | 5 | Description: Accept cryptocurrency payments on your WooCommerce website |
6 | | -Version: 4.8.8 |
| 6 | +Version: 5.0.0 |
7 | 7 | Requires at least: 5.8 |
8 | 8 | Tested up to: 6.7 |
9 | 9 | WC requires at least: 5.8 |
10 | | -WC tested up to: 9.4.1 |
| 10 | +WC tested up to: 9.5.2 |
11 | 11 | Requires PHP: 7.2 |
12 | 12 | Author: cryptapi |
13 | 13 | Author URI: https://cryptapi.io/ |
14 | 14 | License: MIT |
15 | 15 | */ |
| 16 | +if (!defined('ABSPATH')) { |
| 17 | + exit; // Exit if accessed directly. |
| 18 | +} |
16 | 19 |
|
17 | | -require_once 'define.php'; |
| 20 | +define('CRYPTAPI_PLUGIN_VERSION', '5.0.0'); |
| 21 | +define('CRYPTAPI_PLUGIN_PATH', plugin_dir_path(__FILE__)); |
| 22 | +define('CRYPTAPI_PLUGIN_URL', plugin_dir_url(__FILE__)); |
18 | 23 |
|
19 | | -function cryptapi_missing_wc_notice() |
20 | | -{ |
21 | | - echo '<div class="error"><p><strong>' . sprintf(esc_html__('CryptAPI requires WooCommerce to be installed and active. You can download %s here.', 'cryptapi'), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>') . '</strong></p></div>'; |
22 | | -} |
| 24 | +spl_autoload_register(function ($class) { |
| 25 | + $prefix = 'CryptAPI\\'; |
| 26 | + $base_dir = __DIR__ . '/'; |
23 | 27 |
|
24 | | -function cryptapi_missing_bcmath() |
25 | | -{ |
26 | | - echo '<div class="error"><p><strong>' . sprintf(esc_html__('CryptAPI requires PHP\'s BCMath extension. You can know more about it %s.', 'cryptapi'), '<a href="https://www.php.net/manual/en/book.bc.php" target="_blank">here</a>') . '</strong></p></div>'; |
27 | | -} |
| 28 | + $len = strlen($prefix); |
| 29 | + if (strncmp($prefix, $class, $len) !== 0) { |
| 30 | + return; |
| 31 | + } |
28 | 32 |
|
| 33 | + $relative_class = substr($class, $len); |
29 | 34 |
|
30 | | -function cryptapi_include_gateway($methods) |
31 | | -{ |
32 | | - $methods[] = 'WC_CryptAPI_Gateway'; |
33 | | - return $methods; |
34 | | -} |
| 35 | + $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; |
| 36 | + |
| 37 | + if (file_exists($file)) { |
| 38 | + require $file; |
| 39 | + } |
| 40 | +}); |
35 | 41 |
|
36 | | -function cryptapi_loader() |
37 | | -{ |
| 42 | +add_action('plugins_loaded', function () { |
38 | 43 | if (!class_exists('WooCommerce')) { |
39 | | - add_action('admin_notices', 'cryptapi_missing_wc_notice'); |
| 44 | + add_action('admin_notices', function () { |
| 45 | + echo '<div class="error"><p><strong>' . sprintf(esc_html__('CryptAPI requires WooCommerce to be installed and active. You can download %s here.', 'cryptapi'), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>') . '</strong></p></div>'; |
| 46 | + }); |
40 | 47 | return; |
41 | 48 | } |
42 | 49 |
|
43 | 50 | if (!extension_loaded('bcmath')) { |
44 | | - add_action('admin_notices', 'cryptapi_missing_bcmath'); |
| 51 | + add_action('admin_notices', function () { |
| 52 | + echo '<div class="error"><p><strong>' . sprintf(esc_html__('CryptAPI requires PHP\'s BCMath extension. You can know more about it %s.', 'cryptapi'), '<a href="https://www.php.net/manual/en/book.bc.php" target="_blank">here</a>') . '</strong></p></div>'; |
| 53 | + }); |
45 | 54 | return; |
46 | 55 | } |
47 | 56 |
|
48 | | - $dirs = [ |
49 | | - CRYPTAPI_PLUGIN_PATH . 'controllers/', |
50 | | - CRYPTAPI_PLUGIN_PATH . 'utils/', |
51 | | - CRYPTAPI_PLUGIN_PATH . 'languages/', |
52 | | - ]; |
| 57 | + $register = new \CryptAPI\Register(); |
| 58 | + $register->register(); |
53 | 59 |
|
54 | | - cryptapi_include_dirs($dirs); |
| 60 | + $initialize = new \CryptAPI\Initialize(); |
| 61 | + $initialize->initialize(); |
55 | 62 |
|
56 | | - $language_dir = CRYPTAPI_PLUGIN_PATH . 'languages/'; |
57 | | - $mo_file_path = $language_dir . 'cryptapi-payment-gateway-for-woocommerce-' . get_locale() . '.mo'; |
| 63 | + $cryptapi = new \CryptAPI\Controllers\WC_CryptAPI_Gateway(); |
| 64 | +}); |
58 | 65 |
|
59 | | - if (file_exists($mo_file_path)) { |
60 | | - load_textdomain('cryptapi', $mo_file_path, get_locale()); |
61 | | - } else { |
62 | | - error_log('Translation file not found: ' . $mo_file_path); |
63 | | - } |
64 | 66 |
|
65 | | - $cryptapi = new WC_CryptAPI_Gateway(); |
66 | | -} |
| 67 | +add_filter('cron_schedules', function ($cryptapi_interval) { |
| 68 | + $cryptapi_interval['cryptapi_interval'] = array( |
| 69 | + 'interval' => 60, |
| 70 | + 'display' => esc_html__('CryptAPI Interval'), |
| 71 | + ); |
67 | 72 |
|
68 | | -add_action('plugins_loaded', 'cryptapi_loader'); |
69 | | -add_filter('woocommerce_payment_gateways', 'cryptapi_include_gateway'); |
| 73 | + return $cryptapi_interval; |
| 74 | +}); |
70 | 75 |
|
71 | | -function cryptapi_include_dirs($dirs) |
72 | | -{ |
| 76 | +register_activation_hook(__FILE__, function () { |
| 77 | + if (!wp_next_scheduled('cryptapi_cronjob')) { |
| 78 | + wp_schedule_event(time(), 'cryptapi_interval', 'cryptapi_cronjob'); |
| 79 | + } |
| 80 | +}); |
73 | 81 |
|
74 | | - foreach ($dirs as $dir) { |
75 | | - $files = cryptapi_scan_dir($dir); |
76 | | - if ($files === false) continue; |
| 82 | +register_deactivation_hook(__FILE__, function () { |
| 83 | + wp_clear_scheduled_hook('cryptapi_cronjob'); |
| 84 | +}); |
77 | 85 |
|
78 | | - foreach ($files as $f) { |
79 | | - cryptapi_include_file($dir . $f); |
80 | | - } |
| 86 | +use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 87 | +// Declare compatibility with WooCommerce features |
| 88 | +add_action('before_woocommerce_init', function () { |
| 89 | + if (class_exists(FeaturesUtil::class)) { |
| 90 | + FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true); |
| 91 | + FeaturesUtil::declare_compatibility('cart_checkout_blocks', __FILE__, true); |
81 | 92 | } |
82 | | -} |
| 93 | +}); |
| 94 | + |
| 95 | +// Register minimum endpoint to be used in the blocks |
| 96 | + |
| 97 | +add_action('rest_api_init', function () { |
| 98 | + register_rest_route('cryptapi/v1', '/get-minimum', array( |
| 99 | + 'methods' => 'POST', |
| 100 | + 'callback' => 'cryptapi_get_minimum', |
| 101 | + 'permission_callback' => 'cryptapi_verify_nonce', |
| 102 | + )); |
| 103 | + register_rest_route('cryptapi/v1', '/update-coin', array( |
| 104 | + 'methods' => 'POST', |
| 105 | + 'callback' => 'cryptapi_update_coin', |
| 106 | + 'permission_callback' => 'cryptapi_verify_nonce', |
| 107 | + )); |
| 108 | +}); |
83 | 109 |
|
84 | | -function cryptapi_include_file($file) |
85 | | -{ |
86 | | - if (cryptapi_is_includable($file)) { |
87 | | - require_once $file; |
88 | | - return true; |
| 110 | +function cryptapi_verify_nonce(WP_REST_Request $request) { |
| 111 | + $origin = $_SERVER['HTTP_ORIGIN'] ?? ''; |
| 112 | + if ($origin !== home_url()) { |
| 113 | + return false; |
89 | 114 | } |
90 | 115 |
|
91 | | - return false; |
| 116 | + $nonce = $request->get_header('X-WP-Nonce'); |
| 117 | + return wp_verify_nonce($nonce, 'wp_rest'); |
92 | 118 | } |
93 | 119 |
|
94 | | -function cryptapi_scan_dir($dir) |
95 | | -{ |
96 | | - if (!is_dir($dir)) return false; |
97 | | - $file = scandir($dir); |
98 | | - unset($file[0], $file[1]); |
| 120 | +function cryptapi_get_minimum(WP_REST_Request $request) { |
| 121 | + $coin = sanitize_text_field($request->get_param('coin')); |
| 122 | + $fiat = sanitize_text_field($request->get_param('fiat')); |
| 123 | + $value = sanitize_text_field($request->get_param('value')); |
99 | 124 |
|
100 | | - return $file; |
101 | | -} |
| 125 | + if (!$coin) { |
| 126 | + return new WP_REST_Response(['status' => 'error'], 400); |
| 127 | + } |
102 | 128 |
|
103 | | -function cryptapi_is_includable($file) |
104 | | -{ |
105 | | - if (!is_file($file)) return false; |
106 | | - if (!file_exists($file)) return false; |
107 | | - if (strtolower(substr($file, -3, 3)) != 'php') return false; |
| 129 | + try { |
| 130 | + $convert = (float) \CryptAPI\Utils\Api::get_conversion($fiat, $coin, (string) $value, false); |
| 131 | + $minimum = (float) \CryptAPI\Utils\Api::get_info($coin)->minimum_transaction_coin; |
108 | 132 |
|
109 | | - return true; |
| 133 | + if ($convert > $minimum) { |
| 134 | + return new WP_REST_Response(['status' => 'success'], 200); |
| 135 | + } else { |
| 136 | + return new WP_REST_Response(['status' => 'error'], 200); |
| 137 | + } |
| 138 | + } catch (Exception $e) { |
| 139 | + return new WP_REST_Response(['status' => 'error'], 500); |
| 140 | + } |
110 | 141 | } |
111 | 142 |
|
112 | | -add_filter('cron_schedules', function ($cryptapi_interval) { |
113 | | - $cryptapi_interval['cryptapi_interval'] = array( |
114 | | - 'interval' => 60, |
115 | | - 'display' => esc_html__('CryptAPI Interval'), |
116 | | - ); |
| 143 | +function cryptapi_update_coin(WP_REST_Request $request) { |
| 144 | + $coin = sanitize_text_field($request->get_param('coin')); |
| 145 | + $selected = $request->get_param('selected', false); |
117 | 146 |
|
118 | | - return $cryptapi_interval; |
119 | | -}); |
| 147 | + // Ensure WooCommerce session is available |
| 148 | + if (!WC()->session) { |
| 149 | + $session_handler = new \WC_Session_Handler(); |
| 150 | + $session_handler->init(); |
| 151 | + WC()->session = $session_handler; |
| 152 | + } |
120 | 153 |
|
121 | | -register_activation_hook(__FILE__, 'cryptapi_activation'); |
| 154 | + if (!$selected) { |
| 155 | + WC()->session->set('cryptapi_coin', 'none'); |
| 156 | + WC()->session->set('chosen_payment_method', ''); |
| 157 | + return new WP_REST_Response(['success' => true, 'coin' => $coin], 200); |
| 158 | + } |
122 | 159 |
|
123 | | -function cryptapi_activation() |
124 | | -{ |
125 | | - if (!wp_next_scheduled('cryptapi_cronjob')) { |
126 | | - wp_schedule_event(time(), 'cryptapi_interval', 'cryptapi_cronjob'); |
| 160 | + if (!$coin) { |
| 161 | + return new WP_REST_Response(['error' => 'Coin not specified'], 400); |
127 | 162 | } |
128 | | -} |
129 | 163 |
|
130 | | -register_deactivation_hook(__FILE__, 'cryptapi_deactivation'); |
| 164 | + // Set the session value |
| 165 | + WC()->session->set('cryptapi_coin', $coin); |
| 166 | + WC()->session->set('chosen_payment_method', 'cryptapi'); |
131 | 167 |
|
132 | | -function cryptapi_deactivation() |
133 | | -{ |
134 | | - wp_clear_scheduled_hook('cryptapi_cronjob'); |
| 168 | + return new WP_REST_Response(['success' => true, 'coin' => $coin], 200); |
135 | 169 | } |
136 | | - |
137 | | -if (!wp_next_scheduled('cryptapi_cronjob')) { |
138 | | - wp_schedule_event(time(), 'cryptapi_interval', 'cryptapi_cronjob'); |
139 | | -} |
140 | | - |
141 | | -add_action('before_woocommerce_init', function () { |
142 | | - if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) { |
143 | | - \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true); |
144 | | - } |
145 | | -}); |
|
0 commit comments