Skip to content

Commit

Permalink
Merge pull request #2 from wavesnode/feature/1-asset-settings
Browse files Browse the repository at this point in the history
Feature/1 asset settings
  • Loading branch information
tubbynl authored Dec 3, 2017
2 parents 28350ac + cd94f00 commit 7ffe696
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 111 deletions.
11 changes: 7 additions & 4 deletions includes/class-waves-exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static function getBodyAsJson($url,$retries=1) {
}

private static function getExchangePrice($currency1,$currency2) {
$pair = strtolower($currency1."/".$currency2);
$pair = $currency1."/".$currency2;
$result = wp_cache_get($pair,'exchangePrices');
if (false === $result ) {
$result = WavesExchange::getBodyAsJson("http://marketdata.wavesplatform.com/api/ticker/".$pair);
Expand All @@ -30,12 +30,15 @@ private static function getExchangePrice($currency1,$currency2) {
}

private static function exchange($currency,$price,$currencyTo) {
if($currency==$currencyTo) {
return price;
}
$exchange_price = WavesExchange::getExchangePrice($currencyTo,$currency);
return round($price / $exchange_price, 0, PHP_ROUND_HALF_UP);
}

public static function convertToWnet($currency, $price) {
$price_in_waves = WavesExchange::exchange($currency,$price,'waves');
return WavesExchange::exchange('waves',$price_in_waves,'wnet');
public static function convertToAsset($currency, $price,$assetCurrency) {
$price_in_waves = WavesExchange::exchange(strtolower($currency),$price,'waves');
return WavesExchange::exchange('waves',$price_in_waves,strtolower($assetCurrency));
}
}
132 changes: 71 additions & 61 deletions includes/class-waves-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class WcWavesGateway extends WC_Payment_Gateway
public $title;
public $form_fields;
public $addresses;
private $assetId = 'AxAmJaro7BJ4KasYiZhw7HkjwgYtt2nekPuF2CN9LMym';//WNET
private $assetId;
private $assetCode;

public function __construct()
{
Expand All @@ -25,7 +26,11 @@ public function __construct()
$this->secret = $this->get_option('secret');
$this->order_button_text = __('Awaiting transfer..','waves-gateway-for-woocommerce');
$this->has_fields = true;

$this->assetId = $this->get_option('asset_id');
$this->assetCode = $this->get_option('asset_code');
if(empty($this->assetCode)) {
$this->assetCode = 'Waves';
}
$this->initFormFields();

$this->initSettings();
Expand Down Expand Up @@ -56,11 +61,10 @@ public function payment_fields()
{
global $woocommerce;
$woocommerce->cart->get_cart();
$currency = get_woocommerce_currency();
$total_converted = $this->get_order_total();
if($currency!='WNET') {
$total_converted = WavesExchange::convertToWnet(get_woocommerce_currency(), $this->get_order_total());
}
$fiat_currency = get_woocommerce_currency();
$fiat_total = $this->get_order_total();
$total_converted = WavesExchange::convertToAsset($fiat_currency, $fiat_total,$this->assetCode);
$rate = $total_converted / $fiat_total;
$total_waves = $total_converted * 100000000;

$destination_tag = hexdec( substr(sha1(current_time(timestamp,1) . key ($woocommerce->cart->cart_contents ) ), 0, 7) );
Expand All @@ -70,61 +74,67 @@ public function payment_fields()
WC()->session->set('waves_payment_total', $total_waves);
WC()->session->set('waves_destination_tag', $destination_tag_encoded);
WC()->session->set('waves_data_hash', sha1( $this->secret . $total_converted ));

echo '<div id="waves-form">';
//QR uri

$url = "waves://". $this->address ."?amount=". $total_waves."&asset=".$this->assetId."&attachment=".$destination_tag;

echo '<div class="waves-container">';
echo '<div>';


if ($this->description) {
echo '<div class="separator"></div>';
echo '<div id="waves-description">';
echo apply_filters( 'wc_waves_description', wpautop( $this->description ) );
echo '</div>';
}


echo '<div class="separator"></div>';
echo '<div class="waves-container">';

// echo $destination_tag_encoded;

$fiat_total = $this->get_order_total();
$rate = $total_converted / $fiat_total;
echo '<label class="waves-label">(1'. get_woocommerce_currency() .' = '.round($rate,6).' WNET)</label>';
echo '<p class="waves-amount"><span class="copy" data-success-label="'. __('copied','waves-gateway-for-woocommerce') .'" data-clipboard-text="' . esc_attr($total_converted) . '">' . esc_attr($total_converted) . '</span></p>';
echo '</div>';
echo '</div>';
echo '<div class="separator"></div>';
echo '<div class="waves-container">';
echo '<label class="waves-label">' . __('destination address', 'waves-gateway-for-woocommerce') . '</label>';
echo '<p class="waves-address"><span class="copy" data-success-label="'. __('copied','waves-gateway-for-woocommerce') .'" data-clipboard-text="' . esc_attr($this->address) . '">' . esc_attr($this->address) . '</span></p>';
echo '</div>';
echo '<div class="separator"></div>';
echo '<div class="waves-container">';
echo '<label class="waves-label">' . __('attachment', 'waves-gateway-for-woocommerce') . '</label>';
echo '<p class="waves-address"><span class="copy" data-success-label="'. __('copied','waves-gateway-for-woocommerce') .'" data-clipboard-text="' . esc_attr($destination_tag) . '">' . esc_attr($destination_tag) . '</span></p>';
echo '</div>';
echo '<div class="separator"></div>';

echo '</div>';
echo '<div id="waves-qr-code" data-contents="'. $url .'"></div>';
echo '<div class="separator"></div>';
echo '<div class="waves-container">';
echo '<p>'. sprintf(__('Send a payment of exactly %s to the address above (click the links to copy or scan the QR code). <br><br>
<strong>DO NOT FORGET THE ATTACHMENT IF YOU USE MANUAL PAYMENT! </strong><br><br>
We will check in the background and notify you when the payment has been validated.', 'waves-gateway-for-woocommerce'), '<strong>'. esc_attr($total_converted) .'</strong>' ) .'</p>';
echo '<p>'. sprintf(__('Please send your payment within %s.', 'waves-gateway-for-woocommerce'), '<strong><span class="waves-countdown" data-minutes="10">10:00</span></strong>' ) .'</p>';
echo '<p class="small">'. __('When the timer reaches 0 this form will refresh and update the attachment as well as the total amount using the latest conversion rate.', 'waves-gateway-for-woocommerce') .'</p>';
echo '</div>';

echo '<input type="hidden" name="tx_hash" id="tx_hash" value="0"/>';
echo '</div>';

$url = "waves://". $this->address ."?amount=". $total_waves."&attachment=".$destination_tag;
if($this->assetId) {
$url .= "&asset=".$this->assetId;
}?>
<div id="waves-form">
<div class="waves-container">
<div>
<?if ($this->description) { ?>
<div class="separator"></div>
<div id="waves-description">
<?=apply_filters( 'wc_waves_description', wpautop( $this->description ) )?>
</div>
<?}?>
<div class="separator"></div>
<div class="waves-container">
<label class="waves-label">(1<?=get_woocommerce_currency()?> = <?=round($rate,6)?> <?=$this->assetCode?>)</label>
<p class="waves-amount">
<span class="copy" data-success-label="<?=__('copied','waves-gateway-for-woocommerce')?>"
data-clipboard-text="<?=esc_attr($total_converted)?>"><?=esc_attr($total_converted)?>
</span>
</p>
</div>
</div>
<div class="separator"></div>
<div class="waves-container">
<label class="waves-label"><?=__('destination address', 'waves-gateway-for-woocommerce')?></label>
<p class="waves-address">
<span class="copy" data-success-label="<?=__('copied','waves-gateway-for-woocommerce')?>"
data-clipboard-text="<?=esc_attr($this->address)?>"><?=esc_attr($this->address)?>
</span>
</p>
</div>
<div class="separator"></div>
<div class="waves-container">
<label class="waves-label"><?=__('attachment', 'waves-gateway-for-woocommerce')?></label>';
<p class="waves-address">
<span class="copy" data-success-label="<?=__('copied','waves-gateway-for-woocommerce')?>"
data-clipboard-text="<?=esc_attr($destination_tag)?>"><?=esc_attr($destination_tag)?>
</span>
</p>
</div>
<div class="separator"></div>
</div>
<div id="waves-qr-code" data-contents="<?=$url?>"></div>
<div class="separator"></div>
<div class="waves-container">
<p>
<?=sprintf(__('Send a payment of exactly %s to the address above (click the links to copy or scan the QR code). We will check in the background and notify you when the payment has been validated.', 'waves-gateway-for-woocommerce'), '<strong>'. esc_attr($total_converted) .'</strong>' )?>
</p>
<strong>DO NOT FORGET THE ATTACHMENT IF YOU USE MANUAL PAYMENT! </strong>
<p>
<?=sprintf(__('Please send your payment within %s.', 'waves-gateway-for-woocommerce'), '<strong><span class="waves-countdown" data-minutes="10">10:00</span></strong>' )?>
</p>
<p class="small">
<?=__('When the timer reaches 0 this form will refresh and update the attachment as well as the total amount using the latest conversion rate.', 'waves-gateway-for-woocommerce')?>
</p>
</div>
<input type="hidden" name="tx_hash" id="tx_hash" value="0"/>
</div>
<?
}

public function process_payment( $order_id )
Expand Down
24 changes: 21 additions & 3 deletions includes/class-waves-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public static function fields()
'title' => __('Title', 'waves-gateway-for-woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'waves-gateway-for-woocommerce'),
'default' => __('Pay with WAVES', 'waves-gateway-for-woocommerce'),
'default' => __('Pay with Waves', 'waves-gateway-for-woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Customer Message', 'waves-gateway-for-woocommerce'),
'type' => 'textarea',
'default' => __('Ultra-fast and secure checkout with WAVES'),
'default' => __('Ultra-fast and secure checkout with Waves'),
),
'address' => array(
'title' => __('Destination address', 'waves-gateway-for-woocommerce'),
Expand All @@ -45,7 +45,7 @@ public static function fields()
'show_prices' => array(
'title' => __('Convert prices', 'waves-gateway-for-woocommerce'),
'type' => 'checkbox',
'label' => __('Add prices in WAVES', 'waves-gateway-for-woocommerce'),
'label' => __('Add prices in Waves (or asset)', 'waves-gateway-for-woocommerce'),
'default' => 'no',

),
Expand All @@ -54,6 +54,24 @@ public static function fields()
'default' => sha1(get_bloginfo() . Date('U')),

),
'asset_id' => array(
'title' => __('Asset ID', 'waves-gateway-for-woocommerce'),
'type' => 'text',
'default' => null,
'description' => __('This is the asset Id used for transactions.', 'waves-gateway-for-woocommerce'),
),
'asset_code' => array(
'title' => __('Asset code (short name = currency code = currency symbol)', 'waves-gateway-for-woocommerce'),
'type' => 'text',
'default' => null,
'description' => __('This is the Asset Currency code for exchange rates. If omitted Waves will be used', 'waves-gateway-for-woocommerce'),
),
'asset_description' => array(
'title' => __('Asset description', 'waves-gateway-for-woocommerce'),
'type' => 'text',
'default' => null,
'description' => __('Asset full name', 'waves-gateway-for-woocommerce'),
),
)
);
}
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: Waves: 3P4gvv7rZC1kFDobs4oQHN3H6NQckWiu9wz (tubbynl),3PFn9SGPJ8yVjc
Tags: billing, invoicing, woocommerce, payment
Requires at least: 3.0.1
Tested up to: 4.8
Stable tag: 0.2.2
Stable tag: 0.3.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -39,6 +39,9 @@ Install the plugin by uploading the zipfile in your WP admin interface or via FT

== Changelog ==

- 0.3.0
* Added asset settings (if omitted Waves will be used)
* Removed WNET currency (can be set using the asset settings)
- 0.2.2
* added WNET Currency and skipping conversion if selected
* extra explicit message for the Attachment when paying manually
Expand Down
65 changes: 23 additions & 42 deletions waves-gateway-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/**
* Waves Gateway for Woocommerce
*
* Plugin Name: WNET Gateway for Woocommerce
* Plugin Name: WNET Gateway for Woocommerce (also for other Waves assets)
* Plugin URI: https://uwtoken.com
* Description: Show prices in WNET and accept WNET payments in your woocommerce webshop
* Version: 0.1.3
* Author: John Doe / Useless Waves Token
* Description: Show prices in Waves (or asset) and accept Waves payments in your woocommerce webshop
* Version: 0.3.0
* Author: John Doe / Useless Waves Token / Tubby
* License: GPLv2 or later
* License URI: http://www.opensource.org/licenses/gpl-license.php
* Text Domain: waves-gateway-for-woocommerce
Expand Down Expand Up @@ -89,11 +89,8 @@ public function initGateway()
include_once plugin_basename('includes/class-waves-exchange.php');
include_once plugin_basename('includes/class-waves-settings.php');
include_once plugin_basename('includes/class-waves-ajax.php');

add_filter('woocommerce_currencies', array($this, 'WnetCurrency'));
add_filter('woocommerce_currency_symbol', array($this, 'WnetCurrencySymbol'), 10, 2);
add_filter('woocommerce_payment_gateways', array($this, 'addToGateways'));

add_filter('woocommerce_payment_gateways', array($this, 'addToGateways'));
add_filter('woocommerce_get_price_html', array($this, 'WavesFilterPriceHtml'), 10, 2);
add_filter('woocommerce_cart_item_price', array($this, 'WavesFilterCartItemPrice'), 10, 3);
add_filter('woocommerce_cart_item_subtotal', array($this, 'WavesFilterCartItemSubtotal'), 10, 3);
Expand All @@ -110,58 +107,42 @@ public static function addToGateways($gateways)

public function WavesFilterCartTotal($value)
{
$total = WC()->cart->total;
$value = $this->convertToWave($value, $total);
return $value;
return $this->convertToWavesPrice($value, WC()->cart->total);
}

public function WavesFilterCartItemSubtotal($cart_subtotal, $compound, $that)
{
$cart_subtotal = $this->convertToWave($cart_subtotal, $that->subtotal);
return $cart_subtotal;
return $this->convertToWavesPrice($cart_subtotal, $that->subtotal);
}

public function WavesFilterPriceHtml($price, $that)
{
$price = $this->convertToWave($price, $that->price);
return $price;
return $this->convertToWavesPrice($price, $that->price);
}

public function WavesFilterCartItemPrice($price, $cart_item, $cart_item_key)
{
$price = $this->convertToWave($price, ($cart_item['line_subtotal'] + $cart_item['line_subtotal_tax']) / $cart_item['quantity']);
return $price;
$item_price = ($cart_item['line_subtotal'] + $cart_item['line_subtotal_tax']) / $cart_item['quantity'];
return $this->convertToWavesPrice($price,$item_price);
}

public function WavesFilterCartSubtotal($price, $cart_item, $cart_item_key)
{
$price = $this->convertToWave($price, $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax']);
return $price;
$subtotal = $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
return $this->convertToWavesPrice($price, $subtotal);
}

public function WnetCurrency( $currencies )
{
$currencies['WNET'] = __( 'Wavesnode.NET', 'wnet' );
return $currencies;
}

public function WnetCurrencySymbol( $currency_symbol, $currency ) {
switch( $currency ) {
case 'WNET': $currency_symbol = 'WNET'; break;
}
return $currency_symbol;
}

public function convertToWave($price_string, $price)
private function convertToWavesPrice($price_string, $price)
{
$currency = get_woocommerce_currency();
if($currency!='WNET') {
$options = get_option('woocommerce_waves_settings');

if ($options['show_prices'] == 'yes') {
$wnet_price = WavesExchange::convertToWnet($currency, $price);
if ($wnet_price) {
return $price_string . '&nbsp;(<span class="woocommerce-price-amount amount">' . $wnet_price . '&nbsp;</span><span class="woocommerce-price-currencySymbol">WNET)</span>';
}
$options = get_option('woocommerce_waves_settings');
if($options['show_prices'] == 'yes') {
$waves_currency = $options['asset_code'];
if(empty($waves_currency)) {
$waves_currency = 'Waves';
}
$waves_price = WavesExchange::convertToAsset(get_woocommerce_currency(), $price,$waves_currency);
if ($waves_price) {
$price_string .= '&nbsp;(<span class="woocommerce-price-amount amount">' . $waves_price . '&nbsp;</span><span class="woocommerce-price-currencySymbol">'.$waves_currency.')</span>';
}
}
return $price_string;
Expand Down

0 comments on commit 7ffe696

Please sign in to comment.