Skip to content

Commit

Permalink
Merge pull request #25 from clipto/feature/15-Made-plugin-compatible-…
Browse files Browse the repository at this point in the history
…with-new-Waves-wallet

Feature/15 made plugin compatible with new waves wallet
  • Loading branch information
clipto authored Feb 28, 2019
2 parents e09bfcf + 529940d commit df1438b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 118 deletions.
114 changes: 60 additions & 54 deletions includes/class-waves-ajax.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

/**
* Ajax class
*/
class WavesAjax
{

private static $instance;

public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}

public function __construct()
{
$this->init();
}

public function init()
{
add_action('wp_ajax_check_waves_payment', array(__CLASS__, 'checkWavesPayment'));
}

public function checkWavesPayment()
{
global $woocommerce;
$woocommerce->cart->get_cart();

$options = get_option('woocommerce_waves_settings');

$payment_total = WC()->session->get('waves_payment_total');
$destination_tag = WC()->session->get('waves_destination_tag');

$ra = new WavesApi($options['address']);
$result = $ra->findByDestinationTag($destination_tag);

$result['match'] = ($result['amount'] == $payment_total ) ? true : false;

echo json_encode($result);
exit();
}

}

WavesAjax::getInstance();
<?php

if (!defined('ABSPATH')) {
exit;
}

/**
* Ajax class
*/
class WavesAjax
{

private static $instance;

public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}

public function __construct()
{
$this->init();
}

public function init()
{
add_action('wp_ajax_check_waves_payment', array(__CLASS__, 'checkWavesPayment'));
}

public function checkWavesPayment()
{
global $woocommerce;
$woocommerce->cart->get_cart();

$options = get_option('woocommerce_waves_settings');

$payment_total = WC()->session->get('waves_payment_total');
$destination_tag = WC()->session->get('waves_destination_tag');

$ra = new WavesApi($options['address']);
$result = $ra->findByDestinationTag($destination_tag);

$result['match'] = ($result['amount'] == $payment_total ) ? true : false;

$result['wantedtag'] = $destination_tag;
$result['wantedtotal'] = $payment_total;
$result['wantedaddr'] = $options['address'];
$result['validaccount'] = $ra->validAccount($options['address']);
$result['validaccountwnet'] = $ra->validAccount("3PLFq1p7T77rmcXBQ1Wv9aBSb4cQ6yCnEXY");

echo json_encode($result);
exit();
}

}

WavesAjax::getInstance();
94 changes: 47 additions & 47 deletions includes/class-waves-exchange.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

/**
* Exchange class
*/
class WavesExchange
{
private static function getBodyAsJson($url,$retries=1) {
$response = wp_remote_get( $url );
$result = json_decode(wp_remote_retrieve_body($response));
if(!$result && $retries>0) {
return WavesExchange::getBodyAsJson($url,--$retries);
}
return $result?$result:null;
}

private static function getExchangePrice($currency1,$currency2) {
$pair = $currency1."/".$currency2;
$result = wp_cache_get($pair,'exchangePrices');
if (false === $result ) {
$result = WavesExchange::getBodyAsJson("http://marketdata.wavesplatform.com/api/ticker/".$pair);
$result = isset($result->{'24h_vwap'})?$result->{'24h_vwap'}:false;
wp_cache_set( $pair, $result, 'exchangePrices', 3600);
}
return $result;
}

private static function exchange($currency,$price,$currencyTo) {
$exchange_price = WavesExchange::getExchangePrice($currencyTo,$currency);
if(!$exchange_price || $exchange_price==0 || $price==null) {
return null;
}
return round($price / $exchange_price, $currencyTo=='waves'?2:0, PHP_ROUND_HALF_UP);
}

public static function convertToAsset($currency, $price,$assetId) {
$price_in_waves = WavesExchange::exchange(strtolower($currency),$price,'waves');
if($assetId==null) {
return $price_in_waves;
}
return WavesExchange::exchange('waves',$price_in_waves,$assetId);
}
}
<?php

if (!defined('ABSPATH')) {
exit;
}

/**
* Exchange class
*/
class WavesExchange
{
private static function getBodyAsJson($url,$retries=1) {
$response = wp_remote_get( $url );
$result = json_decode(wp_remote_retrieve_body($response));
if(!$result && $retries>0) {
return WavesExchange::getBodyAsJson($url,--$retries);
}
return $result?$result:null;
}

private static function getExchangePrice($currency1,$currency2) {
$pair = $currency1."/".$currency2;
$result = wp_cache_get($pair,'exchangePrices');
if (false === $result ) {
$result = WavesExchange::getBodyAsJson("http://marketdata.wavesplatform.com/api/ticker/".$pair);
$result = isset($result->{'24h_vwap'})?$result->{'24h_vwap'}:false;
wp_cache_set( $pair, $result, 'exchangePrices', 3600);
}
return $result;
}

private static function exchange($currency,$price,$currencyTo) {
$exchange_price = WavesExchange::getExchangePrice($currencyTo,$currency);
if(!$exchange_price || $exchange_price==0 || $price==null) {
return null;
}
return round($price / $exchange_price, $currencyTo=='waves'?2:0, PHP_ROUND_HALF_UP);
}

public static function convertToAsset($currency, $price,$assetId) {
$price_in_waves = WavesExchange::exchange(strtolower($currency),$price,'waves');
if($assetId==null) {
return $price_in_waves;
}
return WavesExchange::exchange('waves',$price_in_waves,$assetId);
}
}
41 changes: 25 additions & 16 deletions includes/class-waves-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct()
$this->has_fields = true;

// assetCode+id if woocommerce_currency is set to Waves-like currency
$this->currencyIsWaves = in_array(get_woocommerce_currency(), array("WAVES","WNET","ARTcoin","POL","Wykop Coin","Surfcash","TN","Ecop"));
$this->currencyIsWaves = in_array(get_woocommerce_currency(), array("WAVES","WNET","ARTcoin","POL","Wykop Coin","TN","Ecop"));
if($this->currencyIsWaves) {
if (get_woocommerce_currency() == "Waves") {
$this->assetCode = 'Waves';
Expand All @@ -46,9 +46,6 @@ public function __construct()
} else if (get_woocommerce_currency() == "Wykop Coin") {
$this->assetCode = 'Wykop Coin';
$this->assetId = 'AHcY2BMoxDZ57mLCWWQYBcWvKzf7rdFMgozJn6n4xgLt';
} else if (get_woocommerce_currency() == "Surfcash") {
$this->assetCode = 'Surfcash';
$this->assetId = 'GcQ7JVnwDizXW8KkKLKd8VDnygGgN7ZnpwnP3bA3VLsE';
} else if (get_woocommerce_currency() == "TN") {
$this->assetCode = 'TN';
$this->assetId = 'HxQSdHu1X4ZVXmJs232M6KfZi78FseeWaEXJczY6UxJ3';
Expand Down Expand Up @@ -108,29 +105,29 @@ public function payment_fields()
if (get_woocommerce_currency() == "Ecop") {
$total_waves = $total_converted * 100000;
}
else if (get_woocommerce_currency() == "Surfcash") {
$total_waves = $total_converted * 100;
}
else if (get_woocommerce_currency() == "TN") {
$total_waves = $total_converted * 100;
}
else {
$total_waves = $total_converted * 100000000;
$total_waves = $total_converted * 100000000;
//$total_waves_qr = $total_converted * 1;
}


$destination_tag = hexdec( substr(sha1(current_time(timestamp,1) . key ($woocommerce->cart->cart_contents ) ), 0, 7) );
$destination_tag = 'P' . hexdec( substr(sha1(current_time(timestamp,1) . key ($woocommerce->cart->cart_contents ) ), 0, 7) );
$base58 = new StephenHill\Base58();
$destination_tag_encoded = $base58->encode(strval($destination_tag));
// set session data
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 ));
//QR uri
$url = "waves://". $this->address ."?amount=". $total_waves."&attachment=".$destination_tag;
if($this->assetId) {
$url .= "&asset=".$this->assetId;
}?>
$url = "https://client.wavesplatform.com/#send/".$this->assetId."?amount=". $total_converted."&recipient=".$this->address."&attachment=".$destination_tag;
}
else {
$url = "https://client.wavesplatform.com/#send/WAVES"."?amount=". $total_converted."&recipient=".$this->address."&attachment=".$destination_tag;
}?>
<div id="waves-form">
<div class="waves-container">
<div>
Expand Down Expand Up @@ -171,16 +168,28 @@ public function payment_fields()
data-clipboard-text="<?=esc_attr($destination_tag)?>"><?=esc_attr($destination_tag)?>
</span>
</p>
<br>
<strong>DO NOT FORGET TO ADD THE ATTACHMENT NUMBER IN THE DESCRIPTION FIELD OF YOUR PAYMENT! </strong>
</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).' '.$this->assetCode.'</strong>' )?>

<div class="separator"></div>

<a href="<?=$url?>" target="_blank">
<button style="color:#FFF;background-color:#0056FF;padding:7px 8px;min-width:64px; font-size: 0.8125rem;min-height:32px;" tabindex="0"" type="button"><span class="jss283">Pay with<span><svg xmlns="http://www.w3.org/2000/svg" width="90" height="16" viewBox="0 0 130 28" style="display: block;margin-top: -5px;"><path fill="#fff" d="M30.25 6.76l.26.36-6.1 18.93-.43.37h-3.89l-.43-.37-4.39-13.55h-.08l-4.37 13.55-.43.37H6.54l-.43-.37L0 7.12l.26-.36h3.88l.4.36 3.93 13.3h.08l4.38-13.3.4-.35h4l.4.35 4.4 13.46 3.94-13.44.4-.37h3.79zm39.37 0l-.43.35-5.38 14-5.33-14-.48-.33h-3.89l-.23.35 7.65 19 .45.36h3.53l.45-.36 7.73-19-.23-.35h-3.84zm21.44 2.42a10.89 10.89 0 0 1 2.59 7.56v.57l-.36.36h-14.5a5.6 5.6 0 0 0 1.78 3.81 5.54 5.54 0 0 0 3.89 1.56A4.5 4.5 0 0 0 89 20.39l.5-.36h3.58l.28.37a8.42 8.42 0 0 1-3.48 4.89 9.15 9.15 0 0 1-5.34 1.51 9.39 9.39 0 0 1-7.12-2.9 10.19 10.19 0 0 1-2.74-7.29 10.53 10.53 0 0 1 2.65-7.18 8.75 8.75 0 0 1 6.83-3 8.9 8.9 0 0 1 6.9 2.75zm-1.72 5a5.08 5.08 0 0 0-5.17-4 5.31 5.31 0 0 0-5.13 4zM50.92 6.76l.36.36v18.95l-.36.36h-3.28l-.36-.36V24h-.12a6.82 6.82 0 0 1-1.91 1.63c-.19.12-.4.22-.61.32a8.24 8.24 0 0 1-3.61.78 8.48 8.48 0 0 1-6.44-2.93 10.34 10.34 0 0 1-2.7-7.27 10.34 10.34 0 0 1 2.7-7.27A8.48 8.48 0 0 1 41 6.37a8.13 8.13 0 0 1 3.62.79 4.85 4.85 0 0 1 .57.31 6.91 6.91 0 0 1 2 1.69l.06-.08v-2l.36-.36h3.28zm-3.84 7.93a5.89 5.89 0 0 0-1.46-2.83 5.32 5.32 0 0 0-4-1.75 5 5 0 0 0-3.81 1.76 6.59 6.59 0 0 0-1.68 4.71 6.54 6.54 0 0 0 1.68 4.71 4.93 4.93 0 0 0 3.8 1.71 5.26 5.26 0 0 0 4-1.75 5.83 5.83 0 0 0 1.46-2.83zm60 .37s-2.15-.45-3.92-.85-2.43-.84-2.43-2 1.18-2.24 3.7-2.24 3.83 1.11 3.83 2.07l.43.37h3.58l.28-.36c0-2.56-2.22-5.65-8-5.65-6.06 0-8 3.56-8 5.86 0 1.93.7 4.2 5.34 5.28l4 .89c2 .47 2.87 1.14 2.87 2.33s-1.07 2.37-3.87 2.37c-2.6 0-4.18-1.25-4.24-2.73l-.45-.36h-3.63l-.28.37c.34 3.3 2.78 6.39 8.62 6.39 6.61 0 8-4 8-6.15-.06-2.83-1.68-4.65-5.79-5.59z"></path><path fill="#fff" d="M116.45 6.77l6.774-6.774 6.774 6.774-6.774 6.774z"></path></svg></span></span><span class="jss71"></span></button>
</a>
<div class="separator"></div>


<strong>DO NOT FORGET TO ADD THE ATTACHMENT NUMBER IN THE DESCRIPTION FIELD OF YOUR PAYMENT! </strong> <br>
<p>
<?=sprintf(__('Send a payment of exactly %s to the address above (click the links to copy or scan the QR code). <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).' '.$this->assetCode.'</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>
Expand Down Expand Up @@ -257,4 +266,4 @@ public function paymentScripts()

}

}
}
6 changes: 5 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.9.8
Stable tag: 0.4.4
Stable tag: 0.5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

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

== Changelog ==

- 0.5.0
* Made plugin compatible with new Waves wallet
* Added a button with link to Lite Client
* Changed attachment names, added a P in front of the number due to a bug in the new client
- 0.4.4
* Added Surfcash, TurtleNode and Ecop currency as static currency (conversion is skipped)
* Added statements for static currencies with decimals value other than default (default is 8)
Expand Down

0 comments on commit df1438b

Please sign in to comment.