Skip to content

Commit 3416861

Browse files
authored
Merge pull request #81 from KyberNetwork/develop
V0.3.0
2 parents c7005c4 + eea2102 commit 3416861

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+6781
-17
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
],
2121
"minimum-stability": "dev",
2222
"require": {
23-
"kyber/monitor-kyber-tx": "v1.0.11"
23+
"kyber/monitor-kyber-tx": "v1.0.11",
24+
"chillerlan/php-qrcode": "dev-master"
2425
}
2526
}

composer.lock

Lines changed: 115 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-woo-kyber-payment-gateway.php

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

67
if ( ! defined( 'ABSPATH' ) ) {
78
exit;
@@ -52,7 +53,8 @@ public function __construct() {
5253
add_action( 'woocommerce_api_kyber_callback', array( $this, 'handle_kyber_callback' ) );
5354
add_action( 'woocommerce_order_details_after_order_table_items', array( $this, 'add_tx_hash_to_order' ) );
5455
add_action( 'woocommerce_thankyou', array( $this, 'embed_kyber_widget_button' ) );
55-
add_action( 'woocommerce_admin_order_totals_after_total', array( $this, 'kyber_price_filter' ) );
56+
// add_action( 'woocommerce_admin_order_totals_after_total', array( $this, 'kyber_price_filter' ) );
57+
add_action( 'woocommerce_email_order_meta', array( $this, 'add_tx_hash_to_email' ), 10, 3 );
5658
}
5759

5860
/**
@@ -225,12 +227,12 @@ public function get_list_token_supported() {
225227
return $result;
226228
}
227229

228-
public function get_token_rate( $token ) {
230+
public function get_token_rate( $token, $base ) {
229231
$network = $this->get_option('network');
230232
if ( $network == "ropsten" ) {
231-
$tracker_url = 'https://ropsten-api.kyber.network/token_price?currency=USD';
233+
$tracker_url = sprintf('https://ropsten-api.kyber.network/token_price?currency=%s', $base);
232234
} else {
233-
$tracker_url = 'https://api.kyber.network/token_price?currency=USD';
235+
$tracker_url = sprintf('https://api.kyber.network/token_price?currency=%s', $base);
234236
}
235237
$response = wp_remote_get( $tracker_url );
236238

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

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

466470
// Reduce stock levels
467-
$order->reduce_order_stock();
471+
wc_reduce_stock_levels($order_id);
468472

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

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

562-
printf("<a href='%s'
563-
class='theme-emerald kyber-widget-button' name='KyberWidget - Powered by KyberNetwork' title='Pay by tokens'
564-
target='_blank'>%s</a>", $endpoint, $widget_text);
566+
$qr = (new QRCode)->render($endpoint);
567+
568+
printf("
569+
<div class='kyber-payment-button-wrap'>
570+
<div class='kyber-payment-link-wrap'>
571+
<a href='%s' class='theme-emerald kyber-widget-button' name='KyberWidget - Powered by KyberNetwork' title='Pay by tokens' target='_blank'>%s</a>
572+
</div>
573+
<div class='kyber-payment-qr-wrap'>
574+
<img src='%s' class='kyber-payment-qr' />
575+
</div>
576+
</div>", $endpoint, $widget_text, $qr);
565577
}
566578
}
567579

@@ -584,17 +596,32 @@ public function payment_fields() {
584596
$total = $order->get_total();
585597
}
586598
$receiveToken = $this->get_option( "receive_token_symbol" );
587-
$rate = $this->get_token_rate( $receiveToken );
599+
$rate = $this->get_token_rate( $receiveToken, "USD" );
588600

589601
$token_price = 0;
590602
if ( $rate != 0 ) {
591603
$token_price = $total / $rate;
592604
}
593605

606+
if ( $receiveToken != "ETH" ) {
607+
$rate_eth = $this->get_token_rate( $receiveToken, "ETH" );
608+
} else {
609+
$rate_eth = 1;
610+
}
611+
612+
if ( $token_price*$rate_eth < 0.001 ) {
613+
$token_price_html = sprintf(
614+
"<br><br>
615+
<p>Total value of token is <strong>%f%s</strong> which is smaller than <strong>0.001ETH</strong>.
616+
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);
617+
echo $token_price_html;
618+
return;
619+
};
620+
594621
$token_price_html = sprintf('</br><p></p>
595622
<div class="kyber-cart-token-price">
596623
<img style="float:left; margin-right: 5px;" src="%s" height="24px" width="24px">
597-
<strong>%.3f</strong>
624+
<strong>%.5f</strong>
598625
<span class="receive-token"><strong>%s</strong></span>
599626
</div>',
600627
esc_html(sprintf("https://files.kyber.network/DesignAssets/tokens/%s.svg", strtolower($receiveToken))),
@@ -613,21 +640,45 @@ public function payment_fields() {
613640
*/
614641
public function get_token_price( $order ) {
615642
$receiveToken = $this->get_option( "receive_token_symbol" );
616-
$rate = $this->get_token_rate( $receiveToken );
643+
$rate = $this->get_token_rate( $receiveToken, "USD" );
617644

618645
$token_price = 0;
619646
if ( $rate != 0 ) {
620647
$token_price = $order->get_total() / $rate;
621648
}
622649
$order->add_meta_data( "token_price", $token_price, true );
650+
$order->save();
623651

624652
return $token_price;
625653
}
626654

627-
public function kyber_price_filter( $order_id ) {
628-
error_log( print_r( sprintf("kyber price filter: %s", $order_id), 1) );
629-
$total_order_token_price_html = sprintf('<p>%s</p>', esc_html($order_id));
630-
return $total_order_token_price_html;
655+
/**
656+
* Add tx hash to email
657+
*
658+
* @return string order meta data
659+
*
660+
* @since 0.3
661+
*/
662+
public function add_tx_hash_to_email( $order, $sent_to_admin, $plain_text ) {
663+
$tx_hash = $order->get_meta( "tx" );
664+
if ( empty($tx_hash) ) {
665+
return;
666+
}
667+
$network = $order->get_meta( "network" );
668+
$etherscan_url = "https://etherscan.io/tx/" + $tx_hash;
669+
if ( $network == "ropsten" ) {
670+
$etherscan_url = sprintf("https://%s.etherscan.io/tx/%s", $network, $tx_hash);
671+
}
672+
$metadata = "";
673+
if ($plain_text) {
674+
$metadata = sprintf("Transaction hash: %s", $tx_hash);
675+
} else {
676+
$metadata = "<h3>Transaction hash: </h3>";
677+
$metadata .= sprintf("<a href='%s'>
678+
%s
679+
</a>", $etherscan_url, $tx_hash);
680+
}
681+
echo $metadata;
631682
}
632683

633684
}

public/css/woo-kyber-payment-public.css

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,42 @@
4141
.kyber-cart-token-price {
4242
margin-top: 20px;
4343
margin-left: 10px;
44+
}
45+
46+
.kyber_widget .kyber_widget-common__input {
47+
font: 400 11px system-ui;
48+
box-shadow: none;
49+
color: #606d7b;
50+
position: relative;
51+
}
52+
53+
.hentry .entry-content .kyber-widget-button {
54+
text-decoration: none !important;
55+
}
56+
57+
.kyber_widget-broadcast__copy img {
58+
max-width: max-content;
59+
}
60+
61+
.kyber_widget .kyber_widget-broadcast__text-bold.kyber_widget-link,
62+
a.kyber-widget-button {
63+
outline: 0 !important;
64+
}
65+
66+
.kyber-payment-button-wrap {
67+
display: inline-flex;
68+
/* justify-content: space-around; */
69+
align-items: center;
70+
width: 100%;
71+
flex-wrap: wrap;
72+
}
73+
74+
.kyber-payment-button-wrap .kyber-payment-qr-wrap,
75+
.kyber-payment-button-wrap .kyber-payment-link-wrap {
76+
flex-basis: 0;
77+
flex-grow: 1;
78+
}
79+
80+
.kyber-payment-button-wrap .kyber-payment-qr {
81+
width: 250px;
4482
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
docs
2+
vendor
3+
.idea
4+
tests/Output/output_test.*
5+
composer.lock
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filter:
2+
excluded_paths:
3+
- examples/*
4+
- tests/*
5+
- vendor/*
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
addons:
2+
apt:
3+
packages:
4+
- imagemagick
5+
6+
language: php
7+
8+
matrix:
9+
include:
10+
- php: 7.2
11+
- php: 7.3
12+
- php: nightly
13+
allow_failures:
14+
- php: nightly
15+
16+
before_install: printf "\n" | pecl install imagick
17+
install: travis_retry composer install --no-interaction --prefer-source
18+
script: vendor/bin/phpunit --configuration phpunit.xml --coverage-clover clover.xml
19+
after_script: bash <(curl -s https://codecov.io/bash)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Smiley <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

0 commit comments

Comments
 (0)