Skip to content

Commit f7c6313

Browse files
committed
v5.0.0
* Now supports WordPress Blocks. * Bug fixes.
1 parent f6c826d commit f7c6313

File tree

46 files changed

+2399
-1839
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2399
-1839
lines changed

CryptAPI.php

Lines changed: 116 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,143 +3,167 @@
33
Plugin Name: CryptAPI Payment Gateway for WooCommerce
44
Plugin URI: https://github.com/cryptapi/woocommerce-cryptapi
55
Description: Accept cryptocurrency payments on your WooCommerce website
6-
Version: 4.8.8
6+
Version: 5.0.0
77
Requires at least: 5.8
88
Tested up to: 6.7
99
WC requires at least: 5.8
10-
WC tested up to: 9.4.1
10+
WC tested up to: 9.5.2
1111
Requires PHP: 7.2
1212
Author: cryptapi
1313
Author URI: https://cryptapi.io/
1414
License: MIT
1515
*/
16+
if (!defined('ABSPATH')) {
17+
exit; // Exit if accessed directly.
18+
}
1619

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__));
1823

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__ . '/';
2327

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+
}
2832

33+
$relative_class = substr($class, $len);
2934

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+
});
3541

36-
function cryptapi_loader()
37-
{
42+
add_action('plugins_loaded', function () {
3843
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+
});
4047
return;
4148
}
4249

4350
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+
});
4554
return;
4655
}
4756

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();
5359

54-
cryptapi_include_dirs($dirs);
60+
$initialize = new \CryptAPI\Initialize();
61+
$initialize->initialize();
5562

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+
});
5865

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-
}
6466

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+
);
6772

68-
add_action('plugins_loaded', 'cryptapi_loader');
69-
add_filter('woocommerce_payment_gateways', 'cryptapi_include_gateway');
73+
return $cryptapi_interval;
74+
});
7075

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+
});
7381

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+
});
7785

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);
8192
}
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+
});
83109

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;
89114
}
90115

91-
return false;
116+
$nonce = $request->get_header('X-WP-Nonce');
117+
return wp_verify_nonce($nonce, 'wp_rest');
92118
}
93119

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'));
99124

100-
return $file;
101-
}
125+
if (!$coin) {
126+
return new WP_REST_Response(['status' => 'error'], 400);
127+
}
102128

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;
108132

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+
}
110141
}
111142

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);
117146

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+
}
120153

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+
}
122159

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);
127162
}
128-
}
129163

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');
131167

132-
function cryptapi_deactivation()
133-
{
134-
wp_clear_scheduled_hook('cryptapi_cronjob');
168+
return new WP_REST_Response(['success' => true, 'coin' => $coin], 200);
135169
}
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-
});

Initialize.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace CryptAPI;
4+
5+
require_once 'controllers/CryptAPI.php';
6+
7+
class Initialize {
8+
public function initialize()
9+
{
10+
add_action('init', [$this, 'load_textdomain']);
11+
add_action('init', [$this, 'schedule_cron_job']);
12+
}
13+
14+
public function load_textdomain()
15+
{
16+
$plugin_dir = plugin_dir_path(__FILE__);
17+
$mo_file_path = $plugin_dir . '../languages/cryptapi-payment-gateway-for-woocommerce-' . get_locale() . '.mo';
18+
19+
if (file_exists($mo_file_path)) {
20+
load_textdomain('cryptapi', $mo_file_path);
21+
}
22+
}
23+
24+
public function schedule_cron_job()
25+
{
26+
if (!wp_next_scheduled('cryptapi_cronjob')) {
27+
wp_schedule_event(time(), 'hourly', 'cryptapi_cronjob');
28+
}
29+
}
30+
}

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ The CryptAPI plugin extends WooCommerce, allowing you to get paid in crypto dire
3131
* (USDT) USDT
3232
* (SHIB) Shiba Inu
3333
* (DOGE) Dogecoin
34-
* (MATIC) Matic
34+
* (POL) Polygon
35+
* (SOL) Solana
3536

3637
among many others, for a full list of the supported cryptocurrencies and tokens, check [this page](https://cryptapi.io/fees/).
3738

@@ -393,6 +394,10 @@ The easiest and fastest way is via our live chat on our [website](https://crypta
393394
* Minor fixes
394395
* Minor improvements
395396

397+
#### 5.0.0
398+
* Now supports WordPress Blocks.
399+
* Bug fixes.
400+
396401
### Upgrade Notice
397402
#### 4.3
398403
* Please be sure to enable the PHP extension BCMath before upgrading to this version.

Register.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace CryptAPI;
4+
5+
require_once 'blocks/CryptAPI.php';
6+
7+
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
8+
9+
use CryptAPI\Blocks\WC_CryptAPI_Gateway;
10+
11+
class Register {
12+
public function register() {
13+
// Register Payment Gateway for legacy WooCommerce
14+
add_filter('woocommerce_payment_gateways', [$this, 'register_payment_gateway']);
15+
16+
// Register Payment Gateway for WooCommerce Blocks
17+
if (class_exists('\Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
18+
add_action('woocommerce_blocks_payment_method_type_registration', [$this, 'register_blocks_payment_gateway']);
19+
}
20+
}
21+
22+
public function register_payment_gateway($methods) {
23+
$methods[] = \CryptAPI\Controllers\WC_CryptAPI_Gateway::class;
24+
return $methods;
25+
}
26+
27+
public function register_blocks_payment_gateway(PaymentMethodRegistry $registry) {
28+
$registry->register(new \CryptAPI\Blocks\WC_CryptAPI_Payments());
29+
}
30+
}

0 commit comments

Comments
 (0)