2017-06-18 20:53:44 +02:00
< ? php
2017-09-22 00:11:47 +02:00
2017-10-17 14:47:28 +02:00
/*
* Main Gateway of Monero using a daemon online
2017-11-24 05:38:42 +01:00
* Authors : Serhack and cryptochangements
2017-10-17 14:47:28 +02:00
*/
2018-03-30 02:40:24 +02:00
require_once ( " cryptonote.php " );
2017-09-22 00:11:47 +02:00
2017-07-13 15:04:55 +02:00
class Monero_Gateway extends WC_Payment_Gateway
{
2018-02-12 00:07:58 +01:00
private $reloadTime = 17000 ;
2017-09-22 00:11:47 +02:00
private $discount ;
private $confirmed = false ;
private $monero_daemon ;
2018-01-23 23:49:41 +01:00
private $non_rpc = false ;
2018-01-24 23:37:33 +01:00
private $zero_cofirm = false ;
2018-03-30 02:40:24 +02:00
private $cryptonote ;
2018-04-14 20:55:53 +02:00
private $testnet = false ;
2017-09-22 00:11:47 +02:00
function __construct ()
{
$this -> id = " monero_gateway " ;
$this -> method_title = __ ( " Monero GateWay " , 'monero_gateway' );
$this -> method_description = __ ( " Monero Payment Gateway Plug-in for WooCommerce. You can find more information about this payment gateway on our website. You'll need a daemon online for your address. " , 'monero_gateway' );
$this -> title = __ ( " Monero Gateway " , 'monero_gateway' );
2018-01-25 02:00:49 +01:00
$this -> version = " 2.0 " ;
2017-09-22 00:11:47 +02:00
//
$this -> icon = apply_filters ( 'woocommerce_offline_icon' , '' );
$this -> has_fields = false ;
$this -> log = new WC_Logger ();
$this -> init_form_fields ();
$this -> host = $this -> get_option ( 'daemon_host' );
$this -> port = $this -> get_option ( 'daemon_port' );
$this -> address = $this -> get_option ( 'monero_address' );
2018-01-23 23:49:41 +01:00
$this -> viewKey = $this -> get_option ( 'viewKey' );
2017-09-22 00:11:47 +02:00
$this -> discount = $this -> get_option ( 'discount' );
2018-01-24 23:37:33 +01:00
$this -> accept_zero_conf = $this -> get_option ( 'zero_conf' );
2018-01-23 23:49:41 +01:00
$this -> use_viewKey = $this -> get_option ( 'use_viewKey' );
$this -> use_rpc = $this -> get_option ( 'use_rpc' );
2018-04-14 20:55:53 +02:00
$env = $this -> get_option ( 'environment' );
2018-01-23 23:49:41 +01:00
if ( $this -> use_viewKey == 'yes' )
{
$this -> non_rpc = true ;
}
if ( $this -> use_rpc == 'yes' )
{
$this -> non_rpc = false ;
}
2018-01-24 23:37:33 +01:00
if ( $this -> accept_zero_conf == 'yes' )
{
$this -> zero_confirm = true ;
}
2018-04-14 20:55:53 +02:00
if ( $env == 'yes' )
{
$this -> testnet = true ;
}
2017-09-22 00:11:47 +02:00
// After init_settings() is called, you can get the settings and load them into variables, e.g:
// $this->title = $this->get_option('title' );
$this -> init_settings ();
// Turn these settings into variables we can use
foreach ( $this -> settings as $setting_key => $value ) {
$this -> $setting_key = $value ;
}
add_action ( 'admin_notices' , array ( $this , 'do_ssl_check' ));
add_action ( 'admin_notices' , array ( $this , 'validate_fields' ));
add_action ( 'woocommerce_thankyou_' . $this -> id , array ( $this , 'instruction' ));
if ( is_admin ()) {
/* Save Settings */
add_action ( 'woocommerce_update_options_payment_gateways_' . $this -> id , array ( $this , 'process_admin_options' ));
add_filter ( 'woocommerce_currencies' , 'add_my_currency' );
add_filter ( 'woocommerce_currency_symbol' , 'add_my_currency_symbol' , 10 , 2 );
add_action ( 'woocommerce_email_before_order_table' , array ( $this , 'email_instructions' ), 10 , 2 );
}
2017-12-28 01:44:13 +01:00
$this -> monero_daemon = new Monero_Library ( $this -> host , $this -> port );
2018-03-30 02:40:24 +02:00
$this -> cryptonote = new Cryptonote ();
2017-09-22 00:11:47 +02:00
}
2018-03-21 10:37:24 +01:00
public function get_icon ()
{
return apply_filters ( 'woocommerce_gateway_icon' , " <img src='http://cdn.monerointegrations.com/logomonero.png' /> " );
}
2017-09-22 00:11:47 +02:00
public function init_form_fields ()
{
$this -> form_fields = array (
'enabled' => array (
'title' => __ ( 'Enable / Disable' , 'monero_gateway' ),
'label' => __ ( 'Enable this payment gateway' , 'monero_gateway' ),
'type' => 'checkbox' ,
'default' => 'no'
),
'title' => array (
'title' => __ ( 'Title' , 'monero_gateway' ),
'type' => 'text' ,
'desc_tip' => __ ( 'Payment title the customer will see during the checkout process.' , 'monero_gateway' ),
'default' => __ ( 'Monero XMR Payment' , 'monero_gateway' )
),
'description' => array (
'title' => __ ( 'Description' , 'monero_gateway' ),
'type' => 'textarea' ,
'desc_tip' => __ ( 'Payment description the customer will see during the checkout process.' , 'monero_gateway' ),
'default' => __ ( 'Pay securely using XMR.' , 'monero_gateway' )
),
2018-01-23 23:49:41 +01:00
'use_viewKey' => array (
'title' => __ ( 'Use ViewKey' , 'monero_gateway' ),
'label' => __ ( ' Verify Transaction with ViewKey ' , 'monero_gateway' ),
'type' => 'checkbox' ,
'description' => __ ( 'Fill in the Address and ViewKey fields to verify transactions with your ViewKey' , 'monero_gateway' ),
'default' => 'no'
),
2017-09-22 00:11:47 +02:00
'monero_address' => array (
'title' => __ ( 'Monero Address' , 'monero_gateway' ),
'label' => __ ( 'Useful for people that have not a daemon online' ),
'type' => 'text' ,
'desc_tip' => __ ( 'Monero Wallet Address' , 'monero_gateway' )
),
2018-01-23 23:49:41 +01:00
'viewKey' => array (
'title' => __ ( 'Secret ViewKey' , 'monero_gateway' ),
'label' => __ ( 'Secret ViewKey' ),
'type' => 'text' ,
'desc_tip' => __ ( 'Your secret ViewKey' , 'monero_gateway' )
),
'use_rpc' => array (
'title' => __ ( 'Use monero-wallet-rpc' , 'monero_gateway' ),
'label' => __ ( ' Verify transactions with the monero-wallet-rpc ' , 'monero_gateway' ),
'type' => 'checkbox' ,
2018-05-20 21:17:38 +02:00
'description' => __ ( 'This must be setup seperately' , 'monero_gateway' ),
2018-01-23 23:49:41 +01:00
'default' => 'no'
),
2017-09-22 00:11:47 +02:00
'daemon_host' => array (
2018-05-20 21:17:38 +02:00
'title' => __ ( 'Monero wallet RPC Host/ IP' , 'monero_gateway' ),
2017-09-22 00:11:47 +02:00
'type' => 'text' ,
'desc_tip' => __ ( 'This is the Daemon Host/IP to authorize the payment with port' , 'monero_gateway' ),
'default' => 'localhost' ,
),
'daemon_port' => array (
2018-05-20 21:17:38 +02:00
'title' => __ ( 'Monero wallet RPC port' , 'monero_gateway' ),
2017-09-22 00:11:47 +02:00
'type' => 'text' ,
'desc_tip' => __ ( 'This is the Daemon Host/IP to authorize the payment with port' , 'monero_gateway' ),
'default' => '18080' ,
),
'discount' => array (
'title' => __ ( '% discount for using XMR' , 'monero_gateway' ),
'desc_tip' => __ ( 'Provide a discount to your customers for making a private payment with XMR!' , 'monero_gateway' ),
'description' => __ ( 'Do you want to spread the word about Monero? Offer a small discount! Leave this empty if you do not wish to provide a discount' , 'monero_gateway' ),
2018-01-23 23:49:41 +01:00
'type' => __ ( 'number' ),
'default' => '5'
2017-09-22 00:11:47 +02:00
),
'environment' => array (
2017-12-28 01:44:13 +01:00
'title' => __ ( ' Testnet' , 'monero_gateway' ),
'label' => __ ( ' Check this if you are using testnet ' , 'monero_gateway' ),
2017-09-22 00:11:47 +02:00
'type' => 'checkbox' ,
'description' => __ ( 'Check this box if you are using testnet' , 'monero_gateway' ),
'default' => 'no'
),
2018-01-24 23:37:33 +01:00
'zero_conf' => array (
'title' => __ ( ' Accept 0 conf txs' , 'monero_gateway' ),
'label' => __ ( ' Accept 0-confirmation transactions ' , 'monero_gateway' ),
'type' => 'checkbox' ,
'description' => __ ( 'This is faster but less secure' , 'monero_gateway' ),
'default' => 'no'
),
2017-09-22 00:11:47 +02:00
'onion_service' => array (
2017-12-28 01:44:13 +01:00
'title' => __ ( ' SSL warnings ' , 'monero_gateway' ),
'label' => __ ( ' Check to Silence SSL warnings' , 'monero_gateway' ),
2017-09-22 00:11:47 +02:00
'type' => 'checkbox' ,
'description' => __ ( 'Check this box if you are running on an Onion Service (Suppress SSL errors)' , 'monero_gateway' ),
'default' => 'no'
),
);
}
public function add_my_currency ( $currencies )
{
$currencies [ 'XMR' ] = __ ( 'Monero' , 'woocommerce' );
return $currencies ;
}
function add_my_currency_symbol ( $currency_symbol , $currency )
{
switch ( $currency ) {
case 'XMR' :
$currency_symbol = 'XMR' ;
break ;
}
return $currency_symbol ;
}
public function admin_options ()
{
$this -> log -> add ( 'Monero_gateway' , '[SUCCESS] Monero Settings OK' );
echo " <h1>Monero Payment Gateway</h1> " ;
echo " <p>Welcome to Monero Extension for WooCommerce. Getting started: Make a connection with daemon <a href='https://reddit.com/u/serhack'>Contact Me</a> " ;
echo " <div style='border:1px solid #DDD;padding:5px 10px;font-weight:bold;color:#223079;background-color:#9ddff3;'> " ;
2018-01-23 23:49:41 +01:00
if ( ! $this -> non_rpc ) // only try to get balance data if using wallet-rpc
$this -> getamountinfo ();
2017-09-22 00:11:47 +02:00
echo " </div> " ;
echo " <table class='form-table'> " ;
$this -> generate_settings_html ();
echo " </table> " ;
2018-01-23 23:49:41 +01:00
echo " <h4>Learn more about using monero-wallet-rpc <a href= \" https://github.com/monero-integrations/monerowp/blob/master/README.md \" >here</a> and viewkeys <a href= \" https://getmonero.org/resources/moneropedia/viewkey.html \" >here</a> </h4> " ;
2017-09-22 00:11:47 +02:00
}
public function getamountinfo ()
{
$wallet_amount = $this -> monero_daemon -> getbalance ();
if ( ! isset ( $wallet_amount )) {
2018-05-20 21:17:38 +02:00
$this -> log -> add ( 'Monero_gateway' , '[ERROR] Cannot connect to monero-wallet-rpc' );
echo " </br>Your balance is: Not Available </br> " ;
echo " Unlocked balance: Not Available " ;
2017-12-28 01:44:13 +01:00
}
else
{
$real_wallet_amount = $wallet_amount [ 'balance' ] / 1000000000000 ;
$real_amount_rounded = round ( $real_wallet_amount , 6 );
$unlocked_wallet_amount = $wallet_amount [ 'unlocked_balance' ] / 1000000000000 ;
$unlocked_amount_rounded = round ( $unlocked_wallet_amount , 6 );
echo " Your balance is: " . $real_amount_rounded . " XMR </br> " ;
echo " Unlocked balance: " . $unlocked_amount_rounded . " XMR </br> " ;
2017-09-22 00:11:47 +02:00
}
}
public function process_payment ( $order_id )
{
$order = wc_get_order ( $order_id );
$order -> update_status ( 'on-hold' , __ ( 'Awaiting offline payment' , 'monero_gateway' ));
// Reduce stock levels
$order -> reduce_order_stock ();
// Remove cart
WC () -> cart -> empty_cart ();
// Return thank you redirect
return array (
'result' => 'success' ,
'redirect' => $this -> get_return_url ( $order )
);
}
// Submit payment and handle response
public function validate_fields ()
{
if ( $this -> check_monero () != TRUE ) {
2018-01-23 23:49:41 +01:00
echo " <div class= \" error \" ><p>Your Monero Address doesn't look valid. Have you checked it?</p></div> " ;
}
if ( ! $this -> check_viewKey ())
{
echo " <div class= \" error \" ><p>Your ViewKey doesn't look valid. Have you checked it?</p></div> " ;
}
if ( $this -> check_checkedBoxes ())
{
echo " <div class= \" error \" ><p>You must choose to either use monero-wallet-rpc or a ViewKey, not both</p></div> " ;
2017-09-22 00:11:47 +02:00
}
}
// Validate fields
public function check_monero ()
{
$monero_address = $this -> settings [ 'monero_address' ];
2018-03-30 02:40:24 +02:00
if ( strlen ( $monero_address ) == 95 && substr ( $monero_address , 1 ))
{
if ( $this -> cryptonote -> verify_checksum ( $monero_address ))
{
return true ;
}
2017-09-22 00:11:47 +02:00
}
return false ;
}
2018-01-23 23:49:41 +01:00
public function check_viewKey ()
{
if ( $this -> use_viewKey == 'yes' )
{
if ( strlen ( $this -> viewKey ) == 64 ) {
return true ;
}
return false ;
}
return true ;
}
public function check_checkedBoxes ()
{
if ( $this -> use_viewKey == 'yes' )
{
if ( $this -> use_rpc == 'yes' )
{
return true ;
}
}
else
return false ;
}
2017-12-28 16:22:55 +01:00
public function is_virtual_in_cart ( $order_id )
{
$order = wc_get_order ( $order_id );
$items = $order -> get_items ();
2018-04-20 23:52:03 +02:00
$cart_size = count ( $items );
$virtual_items = 0 ;
2017-12-28 16:22:55 +01:00
foreach ( $items as $item ) {
$product = new WC_Product ( $item [ 'product_id' ] );
if ( $product -> is_virtual () ) {
2018-04-20 23:52:03 +02:00
$virtual_items += 1 ;
2017-12-28 16:22:55 +01:00
}
}
2018-04-20 23:52:03 +02:00
if ( $virtual_items == $cart_size )
{
return true ;
}
else {
return false ;
}
2017-12-28 16:22:55 +01:00
}
2017-09-22 00:11:47 +02:00
public function instruction ( $order_id )
{
2018-01-23 23:49:41 +01:00
if ( $this -> non_rpc )
{
2018-02-18 20:38:46 +01:00
echo " <noscript><h1>You must enable javascript in order to confirm your order</h1></noscript> " ;
2018-01-23 23:49:41 +01:00
$order = wc_get_order ( $order_id );
$amount = floatval ( preg_replace ( '#[^\d.]#' , '' , $order -> get_total ()));
$payment_id = $this -> set_paymentid_cookie ( 32 );
$currency = $order -> get_currency ();
$amount_xmr2 = $this -> changeto ( $amount , $currency , $payment_id );
$address = $this -> address ;
2018-02-27 02:02:32 +01:00
$order -> update_meta_data ( " Payment ID " , $payment_id );
$order -> update_meta_data ( " Amount requested (XMR) " , $amount_xmr2 );
$order -> save ();
2018-01-23 23:49:41 +01:00
if ( ! isset ( $address )) {
// If there isn't address (merchant missed that field!), $address will be the Monero address for donating :)
$address = " 44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A " ;
}
2018-01-31 02:38:40 +01:00
$uri = " monero: $address ?tx_payment_id= $payment_id " ;
2018-01-23 23:49:41 +01:00
2018-01-24 23:37:33 +01:00
if ( $this -> zero_confirm ){
2018-02-12 00:07:58 +01:00
$this -> verify_zero_conf ( $payment_id , $amount_xmr2 , $order_id );
2018-01-24 23:37:33 +01:00
}
else {
$this -> verify_non_rpc ( $payment_id , $amount_xmr2 , $order_id );
}
2018-01-24 04:47:49 +01:00
if ( $this -> confirmed == false )
2018-01-23 23:49:41 +01:00
{
2018-01-24 23:44:08 +01:00
echo " <h4><font color=DC143C> We are waiting for your transaction to be confirmed </font></h4> " ;
2018-01-24 04:47:49 +01:00
}
if ( $this -> confirmed )
{
2018-01-24 23:44:08 +01:00
echo " <h4><font color=006400> Your transaction has been successfully confirmed! </font></h4> " ;
2018-01-24 04:47:49 +01:00
}
2018-01-23 23:49:41 +01:00
echo "
< head >
2018-02-18 20:38:46 +01:00
< p >* don ' t forget to include the payment ID in your transaction </ p >
2018-01-23 23:49:41 +01:00
<!-- Import Google Icon Font -->
< link href = 'https://fonts.googleapis.com/icon?family=Material+Icons' rel = 'stylesheet' >
< link href = 'https://fonts.googleapis.com/css?family=Montserrat:400,800' rel = 'stylesheet' >
< link href = 'http://cdn.monerointegrations.com/style.css' rel = 'stylesheet' >
<!-- Let browser know website is optimized for mobile -->
< meta name = 'viewport' content = 'width=device-width, initial-scale=1.0' />
</ head >
< body >
<!-- page container -->
< div class = 'page-container' >
2018-05-20 21:17:38 +02:00
<!-- Monero container payment box -->
2018-01-23 23:49:41 +01:00
< div class = 'container-xmr-payment' >
<!-- header -->
< div class = 'header-xmr-payment' >
< span class = 'logo-xmr' >< img src = 'http://cdn.monerointegrations.com/logomonero.png' /></ span >
< span class = 'xmr-payment-text-header' >< h2 > MONERO PAYMENT </ h2 ></ span >
</ div >
<!-- end header -->
<!-- xmr content box -->
< div class = 'content-xmr-payment' >
< div class = 'xmr-amount-send' >
< span class = 'xmr-label' > Send :</ span >
< div class = 'xmr-amount-box' > " . $amount_xmr2 . " </ div >
< span class = 'xmr-label' > Payment ID :</ span >
< div class = 'xmr-integrated-address-box' > " . $payment_id . " </ div >
</ div >
< div class = 'xmr-address' >
< span class = 'xmr-label' > To this address :</ span >
< div class = 'xmr-address-box' > " . $address . " </ div >
</ div >
< div class = 'xmr-qr-code' >
< span class = 'xmr-label' > Or scan QR :</ span >
< div class = 'xmr-qr-code-box' >< img src = 'https://api.qrserver.com/v1/create-qr-code/? size=200x200&data=".$uri."' /></ div >
</ div >
< div class = 'clear' ></ div >
</ div >
<!-- end content box -->
<!-- footer xmr payment -->
< div class = 'footer-xmr-payment' >
< a href = 'https://getmonero.org' target = '_blank' > Help </ a > | < a href = 'https://getmonero.org' target = '_blank' > About Monero </ a >
</ div >
<!-- end footer xmr payment -->
</ div >
2018-05-20 21:17:38 +02:00
<!-- end Monero container payment box -->
2018-01-23 23:49:41 +01:00
</ div >
<!-- end page container -->
</ body >
" ;
echo "
< script type = 'text/javascript' > setTimeout ( function () { location . reload ( true ); }, $this -> reloadTime ); </ script > " ;
2017-09-22 00:11:47 +02:00
}
2018-01-23 23:49:41 +01:00
else
{
$order = wc_get_order ( $order_id );
$amount = floatval ( preg_replace ( '#[^\d.]#' , '' , $order -> get_total ()));
$payment_id = $this -> set_paymentid_cookie ( 8 );
$currency = $order -> get_currency ();
$amount_xmr2 = $this -> changeto ( $amount , $currency , $payment_id );
2018-02-27 02:02:32 +01:00
$order -> update_meta_data ( " Payment ID " , $payment_id );
$order -> update_meta_data ( " Amount requested (XMR) " , $amount_xmr2 );
$order -> save ();
2018-01-31 02:35:14 +01:00
2018-01-31 02:38:40 +01:00
$uri = " monero: $address ?tx_payment_id= $payment_id " ;
2018-01-23 23:49:41 +01:00
$array_integrated_address = $this -> monero_daemon -> make_integrated_address ( $payment_id );
if ( ! isset ( $array_integrated_address )) {
$this -> log -> add ( 'Monero_Gateway' , '[ERROR] Unable get integrated address' );
// Seems that we can't connect with daemon, then set array_integrated_address, little hack
$array_integrated_address [ " integrated_address " ] = $address ;
}
$message = $this -> verify_payment ( $payment_id , $amount_xmr2 , $order );
if ( $this -> confirmed ) {
$color = " 006400 " ;
} else {
$color = " DC143C " ;
}
echo " <h4><font color= $color > " . $message . " </font></h4> " ;
2017-12-28 16:22:55 +01:00
2018-01-23 23:49:41 +01:00
echo "
< head >
<!-- Import Google Icon Font -->
< link href = 'https://fonts.googleapis.com/icon?family=Material+Icons' rel = 'stylesheet' >
< link href = 'https://fonts.googleapis.com/css?family=Montserrat:400,800' rel = 'stylesheet' >
2018-03-30 17:26:47 +02:00
< link href = 'http://cdn.monerointegrations.com/style.css' rel = 'stylesheet' >
2018-01-23 23:49:41 +01:00
<!-- Let browser know website is optimized for mobile -->
< meta name = 'viewport' content = 'width=device-width, initial-scale=1.0' />
</ head >
< body >
<!-- page container -->
< div class = 'page-container' >
2018-05-20 21:17:38 +02:00
<!-- Monero container payment box -->
2018-01-23 23:49:41 +01:00
< div class = 'container-xmr-payment' >
<!-- header -->
< div class = 'header-xmr-payment' >
< span class = 'logo-xmr' >< img src = 'http://cdn.monerointegrations.com/logomonero.png' /></ span >
< span class = 'xmr-payment-text-header' >< h2 > MONERO PAYMENT </ h2 ></ span >
</ div >
<!-- end header -->
<!-- xmr content box -->
< div class = 'content-xmr-payment' >
< div class = 'xmr-amount-send' >
< span class = 'xmr-label' > Send :</ span >
< div class = 'xmr-amount-box' > " . $amount_xmr2 . " </ div >
</ div >
< div class = 'xmr-address' >
< span class = 'xmr-label' > To this address :</ span >
< div class = 'xmr-address-box' > " . $array_integrated_address['integrated_address'] . " </ div >
</ div >
< div class = 'xmr-qr-code' >
< span class = 'xmr-label' > Or scan QR :</ span >
< div class = 'xmr-qr-code-box' >< img src = 'https://api.qrserver.com/v1/create-qr-code/? size=200x200&data=".$uri."' /></ div >
</ div >
< div class = 'clear' ></ div >
</ div >
<!-- end content box -->
<!-- footer xmr payment -->
< div class = 'footer-xmr-payment' >
< a href = 'https://getmonero.org' target = '_blank' > Help </ a > | < a href = 'https://getmonero.org' target = '_blank' > About Monero </ a >
</ div >
<!-- end footer xmr payment -->
</ div >
2018-05-20 21:17:38 +02:00
<!-- end Monero container payment box -->
2018-01-23 23:49:41 +01:00
</ div >
<!-- end page container -->
</ body >
" ;
echo "
< script type = 'text/javascript' > setTimeout ( function () { location . reload ( true ); }, $this -> reloadTime ); </ script > " ;
}
2017-09-22 00:11:47 +02:00
}
2018-01-23 23:49:41 +01:00
private function set_paymentid_cookie ( $size )
2017-09-22 00:11:47 +02:00
{
if ( ! isset ( $_COOKIE [ 'payment_id' ])) {
2018-01-23 23:49:41 +01:00
$payment_id = bin2hex ( openssl_random_pseudo_bytes ( $size ));
2017-09-22 00:11:47 +02:00
setcookie ( 'payment_id' , $payment_id , time () + 2700 );
2017-11-24 05:38:42 +01:00
}
else {
$payment_id = $this -> sanatize_id ( $_COOKIE [ 'payment_id' ]);
}
2017-09-22 00:11:47 +02:00
return $payment_id ;
}
2017-11-23 14:21:33 +01:00
2017-11-24 05:38:42 +01:00
public function sanatize_id ( $payment_id )
{
// Limit payment id to alphanumeric characters
$sanatized_id = preg_replace ( " /[^a-zA-Z0-9]+/ " , " " , $payment_id );
return $sanatized_id ;
}
2017-09-22 00:11:47 +02:00
public function changeto ( $amount , $currency , $payment_id )
{
global $wpdb ;
// This will create a table named whatever the payment id is inside the database "WordPress"
$create_table = " CREATE TABLE IF NOT EXISTS $payment_id (
2017-08-10 18:46:34 +02:00
rate INT
2017-09-22 00:11:47 +02:00
) " ;
2017-11-22 20:10:01 +01:00
$wpdb -> query ( $create_table );
2017-09-22 00:11:47 +02:00
$rows_num = $wpdb -> get_results ( " SELECT count(*) as count FROM $payment_id " );
if ( $rows_num [ 0 ] -> count > 0 ) // Checks if the row has already been created or not
{
$stored_rate = $wpdb -> get_results ( " SELECT rate FROM $payment_id " );
$stored_rate_transformed = $stored_rate [ 0 ] -> rate / 100 ; //this will turn the stored rate back into a decimaled number
if ( isset ( $this -> discount )) {
2017-12-28 01:44:13 +01:00
$sanatized_discount = preg_replace ( '/[^0-9]/' , '' , $this -> discount );
$discount_decimal = $sanatized_discount / 100 ;
2017-09-22 00:11:47 +02:00
$new_amount = $amount / $stored_rate_transformed ;
$discount = $new_amount * $discount_decimal ;
$final_amount = $new_amount - $discount ;
$rounded_amount = round ( $final_amount , 12 );
} else {
$new_amount = $amount / $stored_rate_transformed ;
2018-05-20 21:17:38 +02:00
$rounded_amount = round ( $new_amount , 12 ); //the Monero wallet can't handle decimals smaller than 0.000000000001
2017-09-22 00:11:47 +02:00
}
} else // If the row has not been created then the live exchange rate will be grabbed and stored
{
$xmr_live_price = $this -> retriveprice ( $currency );
$live_for_storing = $xmr_live_price * 100 ; //This will remove the decimal so that it can easily be stored as an integer
2018-01-31 02:32:19 +01:00
$wpdb -> query ( " INSERT INTO $payment_id (rate) VALUES ( $live_for_storing ) " );
if ( isset ( $this -> discount ))
{
$new_amount = $amount / $xmr_live_price ;
$discount = $new_amount * $this -> discount / 100 ;
$discounted_price = $new_amount - $discount ;
$rounded_amount = round ( $discounted_price , 12 );
}
else
{
$new_amount = $amount / $xmr_live_price ;
$rounded_amount = round ( $new_amount , 12 );
}
2017-09-22 00:11:47 +02:00
}
return $rounded_amount ;
}
// Check if we are forcing SSL on checkout pages
// Custom function not required by the Gateway
public function retriveprice ( $currency )
{
2018-05-18 04:57:33 +02:00
$api_link = 'https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=BTC,USD,EUR,CAD,INR,GBP,COP,SGD' . ',' . $currency . '&extraParams=monero_woocommerce' ;
$xmr_price = file_get_contents ( $api_link );
2017-09-22 00:11:47 +02:00
$price = json_decode ( $xmr_price , TRUE );
if ( ! isset ( $price )) {
$this -> log -> add ( 'Monero_Gateway' , '[ERROR] Unable to get the price of Monero' );
}
switch ( $currency ) {
case 'USD' :
return $price [ 'USD' ];
case 'EUR' :
return $price [ 'EUR' ];
case 'CAD' :
return $price [ 'CAD' ];
case 'GBP' :
return $price [ 'GBP' ];
case 'INR' :
return $price [ 'INR' ];
2018-02-03 00:11:23 +01:00
case 'COP' :
return $price [ 'COP' ];
case 'SGD' :
return $price [ 'SGD' ];
2018-05-18 04:57:33 +02:00
case $currency :
return $price [ $currency ];
2017-09-22 00:11:47 +02:00
case 'XMR' :
$price = '1' ;
return $price ;
}
}
2017-12-06 02:46:45 +01:00
private function on_verified ( $payment_id , $amount_atomic_units , $order_id )
{
$message = " Payment has been received and confirmed. Thanks! " ;
$this -> log -> add ( 'Monero_gateway' , '[SUCCESS] Payment has been recorded. Congratulations!' );
$this -> confirmed = true ;
$order = wc_get_order ( $order_id );
2017-12-28 16:22:55 +01:00
if ( $this -> is_virtual_in_cart ( $order_id ) == true ){
2018-02-27 02:03:57 +01:00
$order -> update_status ( 'completed' , __ ( 'Payment has been received.' , 'monero_gateway' ));
2017-12-28 16:22:55 +01:00
}
else {
2018-02-27 02:03:57 +01:00
$order -> update_status ( 'processing' , __ ( 'Payment has been received.' , 'monero_gateway' )); // Show payment id used for order
2017-12-28 16:22:55 +01:00
}
2017-12-06 02:46:45 +01:00
global $wpdb ;
$wpdb -> query ( " DROP TABLE $payment_id " ); // Drop the table from database after payment has been confirmed as it is no longer needed
$this -> reloadTime = 3000000000000 ; // Greatly increase the reload time as it is no longer needed
return $message ;
}
2017-09-22 00:11:47 +02:00
public function verify_payment ( $payment_id , $amount , $order_id )
{
/*
* function for verifying payments
* Check if a payment has been made with this payment id then notify the merchant
*/
$message = " We are waiting for your payment to be confirmed " ;
$amount_atomic_units = $amount * 1000000000000 ;
$get_payments_method = $this -> monero_daemon -> get_payments ( $payment_id );
if ( isset ( $get_payments_method [ " payments " ][ 0 ][ " amount " ])) {
2017-12-06 02:46:45 +01:00
if ( $get_payments_method [ " payments " ][ 0 ][ " amount " ] >= $amount_atomic_units )
{
$message = $this -> on_verified ( $payment_id , $amount_atomic_units , $order_id );
}
if ( $get_payments_method [ " payments " ][ 0 ][ " amount " ] < $amount_atomic_units )
{
$totalPayed = $get_payments_method [ " payments " ][ 0 ][ " amount " ];
$outputs_count = count ( $get_payments_method [ " payments " ]); // number of outputs recieved with this payment id
$output_counter = 1 ;
while ( $output_counter < $outputs_count )
{
$totalPayed += $get_payments_method [ " payments " ][ $output_counter ][ " amount " ];
$output_counter ++ ;
}
if ( $totalPayed >= $amount_atomic_units )
{
$message = $this -> on_verified ( $payment_id , $amount_atomic_units , $order_id );
}
2017-09-22 00:11:47 +02:00
}
}
return $message ;
}
2018-05-20 21:17:38 +02:00
public function last_block_seen ( $height ) // sometimes 2 blocks are mined within a few seconds of each other. Make sure we don't miss one
2018-02-05 22:46:28 +01:00
{
if ( ! isset ( $_COOKIE [ 'last_seen_block' ]))
{
setcookie ( 'last_seen_block' , $height , time () + 2700 );
return 0 ;
}
else {
$cookie_block = $_COOKIE [ 'last_seen_block' ];
$difference = $height - $cookie_block ;
setcookie ( 'last_seen_block' , $height , time () + 2700 );
return $difference ;
}
}
2018-01-23 23:49:41 +01:00
public function verify_non_rpc ( $payment_id , $amount , $order_id )
{
2018-04-14 20:55:53 +02:00
$tools = new NodeTools ( $this -> testnet );
2018-01-23 23:49:41 +01:00
$bc_height = $tools -> get_last_block_height ();
2018-02-05 22:46:28 +01:00
$block_difference = $this -> last_block_seen ( $bc_height );
2018-01-23 23:49:41 +01:00
$txs_from_block = $tools -> get_txs_from_block ( $bc_height );
$tx_count = count ( $txs_from_block ) - 1 ; // The tx at index 0 is a coinbase tx so it can be ignored
$output_found ;
$block_index ;
2018-02-05 22:46:28 +01:00
2018-02-05 22:52:20 +01:00
if ( $block_difference != 0 )
2018-02-05 22:46:28 +01:00
{
2018-02-18 20:38:46 +01:00
if ( $block_difference >= 2 ){
$this -> log -> add ( '[WARNING] Block difference is greater or equal to 2' );
2018-02-12 00:13:00 +01:00
}
2018-02-05 22:46:28 +01:00
$txs_from_block_2 = $tools -> get_txs_from_block ( $bc_height - 1 );
$tx_count_2 = count ( $txs_from_block_2 ) - 1 ;
$i = 1 ;
while ( $i <= $tx_count_2 )
{
$tx_hash = $txs_from_block_2 [ $i ][ 'tx_hash' ];
if ( strlen ( $txs_from_block_2 [ $i ][ 'payment_id' ]) != 0 )
{
$result = $tools -> check_tx ( $tx_hash , $this -> address , $this -> viewKey );
if ( $result )
{
$output_found = $result ;
$block_index = $i ;
$i = $tx_count_2 ; // finish loop
}
}
$i ++ ;
}
}
$i = 1 ;
2018-01-23 23:49:41 +01:00
while ( $i <= $tx_count )
{
$tx_hash = $txs_from_block [ $i ][ 'tx_hash' ];
2018-01-24 04:47:49 +01:00
if ( strlen ( $txs_from_block [ $i ][ 'payment_id' ]) != 0 )
2018-01-23 23:49:41 +01:00
{
2018-01-24 04:47:49 +01:00
$result = $tools -> check_tx ( $tx_hash , $this -> address , $this -> viewKey );
if ( $result )
{
$output_found = $result ;
$block_index = $i ;
$i = $tx_count ; // finish loop
}
2018-01-23 23:49:41 +01:00
}
$i ++ ;
}
2018-02-05 22:46:28 +01:00
2018-01-23 23:49:41 +01:00
if ( isset ( $output_found ))
{
$amount_atomic_units = $amount * 1000000000000 ;
2018-02-12 00:07:58 +01:00
2018-01-24 23:37:33 +01:00
if ( $txs_from_block [ $block_index ][ 'payment_id' ] == $payment_id && $output_found [ 'amount' ] >= $amount_atomic_units )
2018-01-23 23:49:41 +01:00
{
$this -> on_verified ( $payment_id , $amount_atomic_units , $order_id );
}
2018-02-12 00:07:58 +01:00
if ( $txs_from_block_2 [ $block_index ][ 'payment_id' ] == $payment_id && $output_found [ 'amount' ] >= $amount_atomic_units )
{
$this -> on_verified ( $payment_id , $amount_atomic_units , $order_id );
}
2018-01-23 23:49:41 +01:00
return true ;
}
return false ;
}
2018-02-05 22:46:28 +01:00
2018-01-24 23:37:33 +01:00
public function verify_zero_conf ( $payment_id , $amount , $order_id )
{
2018-04-14 20:55:53 +02:00
$tools = new NodeTools ( $this -> testnet );
2018-01-24 23:37:33 +01:00
$txs_from_mempool = $tools -> get_mempool_txs ();;
$tx_count = count ( $txs_from_mempool [ 'data' ][ 'txs' ]);
$i = 0 ;
$output_found ;
while ( $i <= $tx_count )
{
$tx_hash = $txs_from_mempool [ 'data' ][ 'txs' ][ $i ][ 'tx_hash' ];
if ( strlen ( $txs_from_mempool [ 'data' ][ 'txs' ][ $i ][ 'payment_id' ]) != 0 )
{
$result = $tools -> check_tx ( $tx_hash , $this -> address , $this -> viewKey );
if ( $result )
{
$output_found = $result ;
$tx_i = $i ;
$i = $tx_count ; // finish loop
}
}
$i ++ ;
}
if ( isset ( $output_found ))
{
$amount_atomic_units = $amount * 1000000000000 ;
if ( $txs_from_mempool [ 'data' ][ 'txs' ][ $tx_i ][ 'payment_id' ] == $payment_id && $output_found [ 'amount' ] >= $amount_atomic_units )
{
$this -> on_verified ( $payment_id , $amount_atomic_units , $order_id );
}
return true ;
}
else
return false ;
}
2017-09-22 00:11:47 +02:00
public function do_ssl_check ()
{
2017-11-05 12:07:30 +01:00
if ( $this -> enabled == " yes " && ! $this -> get_option ( 'onion_service' )) {
2017-09-22 00:11:47 +02:00
if ( get_option ( 'woocommerce_force_ssl_checkout' ) == " no " ) {
echo " <div class= \" error \" ><p> " . sprintf ( __ ( " <strong>%s</strong> is enabled and WooCommerce is not forcing the SSL certificate on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href= \" %s \" >forcing the checkout pages to be secured.</a> " ), $this -> method_title , admin_url ( 'admin.php?page=wc-settings&tab=checkout' )) . " </p></div> " ;
}
}
}
public function connect_daemon ()
{
$host = $this -> settings [ 'daemon_host' ];
$port = $this -> settings [ 'daemon_port' ];
$monero_library = new Monero ( $host , $port );
if ( $monero_library -> works () == true ) {
echo " <div class= \" notice notice-success is-dismissible \" ><p>Everything works! Congratulations and welcome to Monero. <button type= \" button \" class= \" notice-dismiss \" >
2017-07-15 10:09:48 +02:00
< span class = \ " screen-reader-text \" >Dismiss this notice.</span>
</ button ></ p ></ div > " ;
2017-09-22 00:11:47 +02:00
} else {
2018-05-20 21:17:38 +02:00
$this -> log -> add ( 'Monero_gateway' , '[ERROR] Plugin cannot reach wallet RPC.' );
2017-09-22 00:11:47 +02:00
echo " <div class= \" notice notice-error \" ><p>Error with connection of daemon, see documentation!</p></div> " ;
}
}
2017-08-10 18:46:34 +02:00
}