Skip to content

Commit

Permalink
Merge pull request #81 from KyberNetwork/develop
Browse files Browse the repository at this point in the history
V0.3.0
  • Loading branch information
halink0803 authored Apr 12, 2019
2 parents c7005c4 + eea2102 commit 3416861
Show file tree
Hide file tree
Showing 81 changed files with 6,781 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"minimum-stability": "dev",
"require": {
"kyber/monitor-kyber-tx": "v1.0.11"
"kyber/monitor-kyber-tx": "v1.0.11",
"chillerlan/php-qrcode": "dev-master"
}
}
116 changes: 115 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 66 additions & 15 deletions includes/class-woo-kyber-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once dirname(__DIR__, 1) . '/vendor/autoload.php';
use Web3\Utils;
use chillerlan\QRCode\QRCode;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down Expand Up @@ -52,7 +53,8 @@ public function __construct() {
add_action( 'woocommerce_api_kyber_callback', array( $this, 'handle_kyber_callback' ) );
add_action( 'woocommerce_order_details_after_order_table_items', array( $this, 'add_tx_hash_to_order' ) );
add_action( 'woocommerce_thankyou', array( $this, 'embed_kyber_widget_button' ) );
add_action( 'woocommerce_admin_order_totals_after_total', array( $this, 'kyber_price_filter' ) );
// add_action( 'woocommerce_admin_order_totals_after_total', array( $this, 'kyber_price_filter' ) );
add_action( 'woocommerce_email_order_meta', array( $this, 'add_tx_hash_to_email' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -225,12 +227,12 @@ public function get_list_token_supported() {
return $result;
}

public function get_token_rate( $token ) {
public function get_token_rate( $token, $base ) {
$network = $this->get_option('network');
if ( $network == "ropsten" ) {
$tracker_url = 'https://ropsten-api.kyber.network/token_price?currency=USD';
$tracker_url = sprintf('https://ropsten-api.kyber.network/token_price?currency=%s', $base);
} else {
$tracker_url = 'https://api.kyber.network/token_price?currency=USD';
$tracker_url = sprintf('https://api.kyber.network/token_price?currency=%s', $base);
}
$response = wp_remote_get( $tracker_url );

Expand Down Expand Up @@ -401,6 +403,8 @@ public function get_checkout_url( $order ) {
if ( !$receiveAmount ) {
$receiveAmount = $this->get_token_price( $order );
error_log( print_r( sprintf("cannot get token price from order meta data, try to get from blockchain: %s", $receiveAmount), 1 ) );
$order->add_meta_data( "token_price", $receiveAmount, true );
$order->save();
}

$endpoint .= 'mode='. $mode .'&receiveAddr=' . $receiveAddr . '&receiveToken=' . $receiveToken . '&callback=' . $callback_url . '&receiveAmount=' . $receiveAmount;
Expand Down Expand Up @@ -464,7 +468,7 @@ public function handle_kyber_callback() {
}

// Reduce stock levels
$order->reduce_order_stock();
wc_reduce_stock_levels($order_id);

// Save transaction hash to order
$order->update_meta_data("tx", $tx);
Expand Down Expand Up @@ -559,9 +563,17 @@ public function embed_kyber_widget_button( $order_id ) {

$widget_text = apply_filters( 'kyber_widget_text', __('Pay by tokens', 'woocommerce-gateway-kyber') );

printf("<a href='%s'
class='theme-emerald kyber-widget-button' name='KyberWidget - Powered by KyberNetwork' title='Pay by tokens'
target='_blank'>%s</a>", $endpoint, $widget_text);
$qr = (new QRCode)->render($endpoint);

printf("
<div class='kyber-payment-button-wrap'>
<div class='kyber-payment-link-wrap'>
<a href='%s' class='theme-emerald kyber-widget-button' name='KyberWidget - Powered by KyberNetwork' title='Pay by tokens' target='_blank'>%s</a>
</div>
<div class='kyber-payment-qr-wrap'>
<img src='%s' class='kyber-payment-qr' />
</div>
</div>", $endpoint, $widget_text, $qr);
}
}

Expand All @@ -584,17 +596,32 @@ public function payment_fields() {
$total = $order->get_total();
}
$receiveToken = $this->get_option( "receive_token_symbol" );
$rate = $this->get_token_rate( $receiveToken );
$rate = $this->get_token_rate( $receiveToken, "USD" );

$token_price = 0;
if ( $rate != 0 ) {
$token_price = $total / $rate;
}

if ( $receiveToken != "ETH" ) {
$rate_eth = $this->get_token_rate( $receiveToken, "ETH" );
} else {
$rate_eth = 1;
}

if ( $token_price*$rate_eth < 0.001 ) {
$token_price_html = sprintf(
"<br><br>
<p>Total value of token is <strong>%f%s</strong> which is smaller than <strong>0.001ETH</strong>.
Kyber Widget only can handle amount which is equal or bigger than <strong>0.001ETH</strong> equivalent. Please add more items or choose another payment method.</p>", $token_price, $receiveToken);
echo $token_price_html;
return;
};

$token_price_html = sprintf('</br><p></p>
<div class="kyber-cart-token-price">
<img style="float:left; margin-right: 5px;" src="%s" height="24px" width="24px">
<strong>%.3f</strong>
<strong>%.5f</strong>
<span class="receive-token"><strong>%s</strong></span>
</div>',
esc_html(sprintf("https://files.kyber.network/DesignAssets/tokens/%s.svg", strtolower($receiveToken))),
Expand All @@ -613,21 +640,45 @@ public function payment_fields() {
*/
public function get_token_price( $order ) {
$receiveToken = $this->get_option( "receive_token_symbol" );
$rate = $this->get_token_rate( $receiveToken );
$rate = $this->get_token_rate( $receiveToken, "USD" );

$token_price = 0;
if ( $rate != 0 ) {
$token_price = $order->get_total() / $rate;
}
$order->add_meta_data( "token_price", $token_price, true );
$order->save();

return $token_price;
}

public function kyber_price_filter( $order_id ) {
error_log( print_r( sprintf("kyber price filter: %s", $order_id), 1) );
$total_order_token_price_html = sprintf('<p>%s</p>', esc_html($order_id));
return $total_order_token_price_html;
/**
* Add tx hash to email
*
* @return string order meta data
*
* @since 0.3
*/
public function add_tx_hash_to_email( $order, $sent_to_admin, $plain_text ) {
$tx_hash = $order->get_meta( "tx" );
if ( empty($tx_hash) ) {
return;
}
$network = $order->get_meta( "network" );
$etherscan_url = "https://etherscan.io/tx/" + $tx_hash;
if ( $network == "ropsten" ) {
$etherscan_url = sprintf("https://%s.etherscan.io/tx/%s", $network, $tx_hash);
}
$metadata = "";
if ($plain_text) {
$metadata = sprintf("Transaction hash: %s", $tx_hash);
} else {
$metadata = "<h3>Transaction hash: </h3>";
$metadata .= sprintf("<a href='%s'>
%s
</a>", $etherscan_url, $tx_hash);
}
echo $metadata;
}

}
38 changes: 38 additions & 0 deletions public/css/woo-kyber-payment-public.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,42 @@
.kyber-cart-token-price {
margin-top: 20px;
margin-left: 10px;
}

.kyber_widget .kyber_widget-common__input {
font: 400 11px system-ui;
box-shadow: none;
color: #606d7b;
position: relative;
}

.hentry .entry-content .kyber-widget-button {
text-decoration: none !important;
}

.kyber_widget-broadcast__copy img {
max-width: max-content;
}

.kyber_widget .kyber_widget-broadcast__text-bold.kyber_widget-link,
a.kyber-widget-button {
outline: 0 !important;
}

.kyber-payment-button-wrap {
display: inline-flex;
/* justify-content: space-around; */
align-items: center;
width: 100%;
flex-wrap: wrap;
}

.kyber-payment-button-wrap .kyber-payment-qr-wrap,
.kyber-payment-button-wrap .kyber-payment-link-wrap {
flex-basis: 0;
flex-grow: 1;
}

.kyber-payment-button-wrap .kyber-payment-qr {
width: 250px;
}
5 changes: 5 additions & 0 deletions vendor/chillerlan/php-qrcode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
docs
vendor
.idea
tests/Output/output_test.*
composer.lock
5 changes: 5 additions & 0 deletions vendor/chillerlan/php-qrcode/.scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filter:
excluded_paths:
- examples/*
- tests/*
- vendor/*
19 changes: 19 additions & 0 deletions vendor/chillerlan/php-qrcode/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
addons:
apt:
packages:
- imagemagick

language: php

matrix:
include:
- php: 7.2
- php: 7.3
- php: nightly
allow_failures:
- php: nightly

before_install: printf "\n" | pecl install imagick
install: travis_retry composer install --no-interaction --prefer-source
script: vendor/bin/phpunit --configuration phpunit.xml --coverage-clover clover.xml
after_script: bash <(curl -s https://codecov.io/bash)
21 changes: 21 additions & 0 deletions vendor/chillerlan/php-qrcode/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Smiley <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit 3416861

Please sign in to comment.