Skip to content

Commit

Permalink
Merge branch 'blockonomics:master' into fix/base-url-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
anktd authored Nov 27, 2024
2 parents 113854d + b1de9dc commit 3a3ed6a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Contributors:** juhasiivikko, darrenwestwood, blockonomics, anktd, btcdeveloper
**Tags:** bitcoin, accept bitcoin, bitcoin woocommerce, bitcoin wordpress plugin, bitcoin payments
**Requires at least:** 3.0.1
**Tested up to:** 6.6.2
**Tested up to:** 6.7
**Stable tag:** 3.7.6
**License:** MIT
**License URI:** http://opensource.org/licenses/MIT
Expand Down
28 changes: 17 additions & 11 deletions blockonomics-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ function blockonomics_woocommerce_init()
return;
}


require_once plugin_dir_path(__FILE__) . 'php' . DIRECTORY_SEPARATOR . 'WC_Gateway_Blockonomics.php';
include_once plugin_dir_path(__FILE__) . 'php' . DIRECTORY_SEPARATOR . 'Blockonomics.php';
require_once plugin_dir_path(__FILE__) . 'php' . DIRECTORY_SEPARATOR . 'admin-page.php';
require_once plugin_dir_path(__FILE__) . 'php' . DIRECTORY_SEPARATOR . 'class-blockonomics-setup.php';

add_action('admin_menu', 'add_page');
add_action('init', 'load_plugin_translations');
add_action('woocommerce_order_details_after_order_table', 'nolo_custom_field_display_cust_order_meta', 10, 1);
Expand Down Expand Up @@ -100,7 +100,7 @@ function blockonomics_add_admin_menu() {
'blockonomics_setup_page'
);
}

add_action( 'admin_enqueue_scripts', 'blockonomics_enqueue_custom_admin_style' );
add_action( 'wp_ajax_test_setup', 'blockonomics_test_setup' );

Expand All @@ -120,7 +120,7 @@ function blockonomics_enqueue_custom_admin_style() {
wp_enqueue_style( 'blockonomics-admin-style' );

wp_register_script( 'blockonomics-admin-scripts', plugins_url('js/admin.js', __FILE__), array(), get_plugin_data( __FILE__ )['Version'], array( 'strategy' => 'defer' ) );

wp_localize_script('blockonomics-admin-scripts', 'blockonomics_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'apikey' => get_option('blockonomics_api_key')
Expand Down Expand Up @@ -154,7 +154,7 @@ function add_payment_page_shortcode() {
if ($currentFilter == 'wp_head'){
return;
}

$show_order = isset($_GET["show_order"]) ? sanitize_text_field(wp_unslash($_GET['show_order'])) : "";
$crypto = isset($_GET["crypto"]) ? sanitize_key($_GET['crypto']) : "";
$select_crypto = isset($_GET["select_crypto"]) ? sanitize_text_field(wp_unslash($_GET['select_crypto'])) : "";
Expand Down Expand Up @@ -198,7 +198,7 @@ function update_payment_url_on_underpayments($pay_url, $order) {
return esc_url($blockonomics->get_order_checkout_url($order_id));

}

/**
* Add Styles to Blockonomics Admin Page
**/
Expand All @@ -210,7 +210,7 @@ function blockonomics_load_admin_scripts($hook){
/**
* Adding new filter to WooCommerce orders
**/

function filter_orders() {
$screen = get_current_screen();
if ( in_array( $screen->id, array( 'edit-shop_order', 'woocommerce_page_wc-orders' ) )) {
Expand Down Expand Up @@ -241,7 +241,7 @@ function filter_orders_by_address_or_txid( $vars ) {
}
return $vars;
}

/**
* Add this Gateway to WooCommerce
**/
Expand Down Expand Up @@ -427,9 +427,15 @@ function blockonomics_create_table() {
}

function blockonomics_activation_hook() {
if(!is_plugin_active('woocommerce/woocommerce.php'))
{
trigger_error(__( 'Wordpress Bitcoin Payments - Blockonomics requires WooCommerce plugin to be installed and active.', 'blockonomics-bitcoin-payments' ).'<br>', E_USER_ERROR);
if(!is_plugin_active('woocommerce/woocommerce.php')) {
deactivate_plugins(plugin_basename(__FILE__));
$error_message = sprintf(
__('This plugin requires WooCommerce to be installed and activated. Please install and activate WooCommerce first, then activate Blockonomics Bitcoin Payments.', 'blockonomics-bitcoin-payments')
);
wp_die($error_message, 'Plugin Activation Error', array(
'response' => 200,
'back_link' => true,
));
}
}
// Page creation function for the Blockonomics payement following woo-commerce page creation shortcode logic
Expand Down
105 changes: 77 additions & 28 deletions php/Blockonomics.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public function new_address($secret, $crypto, $reset=false)
return $responseObj;
}

public function get_price($currency, $crypto)
{
public function get_price($currency, $crypto) {
if($crypto === 'btc'){
$url = Blockonomics::PRICE_URL. "?currency=$currency";
}else{
Expand All @@ -114,16 +113,23 @@ public function get_price($currency, $crypto)
$response = $this->get($url);
if (!isset($responseObj)) $responseObj = new stdClass();
$responseObj->{'response_code'} = wp_remote_retrieve_response_code($response);
if (wp_remote_retrieve_body($response))
{
$body = json_decode(wp_remote_retrieve_body($response));
$responseObj->{'response_message'} = isset($body->message) ? $body->message : '';
$responseObj->{'price'} = isset($body->price) ? $body->price : '';
if (wp_remote_retrieve_body($response)) {
$body = json_decode(wp_remote_retrieve_body($response));
// Check if api response is {"price":null} which indicates unsupported currency
if ($body && property_exists($body, 'price') && $body->price === null) {
$responseObj->{'response_message'} = sprintf(
__('Currency %s is not supported by Blockonomics', 'blockonomics-bitcoin-payments'),
$currency
);
$responseObj->{'price'} = '';
} else {
$responseObj->{'response_message'} = isset($body->message) ? $body->message : '';
$responseObj->{'price'} = isset($body->price) ? $body->price : '';
}
}
return $responseObj;
}


public function get_callbacks($crypto)
{
if ($crypto !== 'btc') {
Expand Down Expand Up @@ -566,34 +572,73 @@ public function record_address($order_id, $crypto, $address){
$wc_order->save();
}

public function create_new_order($order_id, $crypto){
$responseObj = $this->new_address(get_option("blockonomics_callback_secret"), $crypto);
if($responseObj->response_code != 200) {
return array("error"=>$responseObj->response_message);
public function create_new_order($order_id, $crypto)
{
$wc_order = wc_get_order($order_id);
$currency = $wc_order->get_currency();

// Get price first to check if currency is supported
$price_obj = $this->get_price($currency, $crypto);
if (empty($price_obj->price)) {
return array(
'error' => $price_obj->response_message
);
}
$address = $responseObj->address;

// Continue with rest of order creation only if we have a valid price
$order = array(
'order_id' => $order_id,
'payment_status' => 0,
'crypto' => $crypto,
'address' => $address
'order_id' => $order_id,
'crypto' => $crypto,
'currency' => $currency,
'expected_fiat' => $wc_order->get_total(),
'timestamp' => time(),
'status' => -1,
'payment_status' => 0,
'paid_fiat' => 0
);
$order = $this->calculate_order_params($order);
return $order;

// Generate new address
$callback_secret = get_option("blockonomics_callback_secret");
$response = $this->new_address($callback_secret, $crypto);

if ($response->response_code != 200) {
return array(
'error' => isset($response->response_message) && $response->response_message ?
$response->response_message :
__('Could not generate new address', 'blockonomics-bitcoin-payments')
);
}

if (empty($response->address)) {
return array(
'error' => __('No address returned from API', 'blockonomics-bitcoin-payments')
);
}

$order['address'] = $response->address;
return $this->calculate_order_params($order);
}

public function get_error_context($error_type){
$context = array();

if ($error_type == 'generic') {
// Show Generic Error to Client.
if ($error_type == 'currency') {
// For unsupported currency errors
// $context['error_title'] = __('Checkout Page Error', 'blockonomics-bitcoin-payments');
$context['error_title'] = '';

$context['error_msg'] = sprintf(
__('Currency %s selected on this store is not supported by Blockonomics', 'blockonomics-bitcoin-payments'),
get_woocommerce_currency()
);
} else if ($error_type == 'generic') {
// Show Generic Error to Client
$context['error_title'] = __('Could not generate new address (This may be a temporary error. Please try again)', 'blockonomics-bitcoin-payments');
$context['error_msg'] = __('If this continues, please ask website administrator to do following:<br/><ul><li>Login to admin panel, navigate to WooCommerce > Settings > Payment. Select Manage on "Blockonomics Bitcoin" and click Test Setup to diagnose the exact issue.</li><li>Check blockonomics registered email address for error messages</li>', 'blockonomics-bitcoin-payments');
} else if($error_type == 'underpaid') {
$context['error_msg'] = __('If this continues, please ask website administrator to do following:<br/><ul><li>Login to WordPress admin panel, navigate to WooCommerce > Settings > Payment. Select Manage on "Blockonomics Bitcoin" and click Test Setup to diagnose the exact issue.</li><li>Check blockonomics registered email address for error messages</li></ul>', 'blockonomics-bitcoin-payments');
} else if ($error_type == 'underpaid') {
$context['error_title'] = '';
$context['error_msg'] = __('Paid order BTC amount is less than expected. Contact merchant', 'blockonomics-bitcoin-payments');
}

return $context;
}

Expand All @@ -617,17 +662,21 @@ public function get_crypto_payment_uri($crypto, $address, $order_amount) {
}

public function get_checkout_context($order, $crypto){

$context = array();
$error_context = NULL;

$context['order_id'] = $order['order_id'];

$context['order_id'] = isset($order['order_id']) ? $order['order_id'] : '';
$cryptos = $this->getActiveCurrencies();
$context['crypto'] = $cryptos[$crypto];

if (array_key_exists('error', $order)) {
$error_context = $this->get_error_context('generic');
// Check if this is a currency error
if (strpos($order['error'], 'Currency') === 0) {
$error_context = $this->get_error_context('currency');
} else {
// All other errors use generic error handling
$error_context = $this->get_error_context('generic');
}
} else {
$context['order'] = $order;

Expand Down
23 changes: 23 additions & 0 deletions templates/blockonomics_nojs_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@
<span class="blockonomics-icon-cart"></span>
<?php echo $order['expected_fiat'] ?> <?php echo $order['currency'] ?>
</div>
<?php
if (isset($paid_fiat)) {
?>
<tr>
<th>
<div class="bnomics-header-row">
<span class="bnomics-order-id"><?php _e('Paid Amount:', 'blockonomics-bitcoin-payments'); ?></span>
<div>
<?php echo $paid_fiat ?> <?php echo $order['currency'] ?>
</div>
</div>

<div class="bnomics-header-row">
<span class="bnomics-order-id"><?php _e('Remaining Amount:', 'blockonomics-bitcoin-payments'); ?></span>
<div>
<?php echo $order['expected_fiat'] ?> <?php echo $order['currency'] ?>
</div>
</div>
</th>
</tr>
<?php
}
?>
</th>
</tr>
</table>
Expand Down

0 comments on commit 3a3ed6a

Please sign in to comment.