-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from clipto/feature/15-Made-plugin-compatible-…
…with-new-Waves-wallet Feature/15 made plugin compatible with new waves wallet
- Loading branch information
Showing
4 changed files
with
137 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters