2017-06-18 18:53:44 +00:00
< ? php
2017-06-27 12:55:02 +00:00
/* Main Gateway of Monero using a daemon online */
2017-07-13 13:04:55 +00:00
class Monero_Gateway extends WC_Payment_Gateway
{
2017-08-10 16:46:34 +00:00
private $reloadTime = 30000 ;
2017-08-11 16:06:06 +00:00
private $discount ;
2017-08-19 15:45:57 +00:00
private $confirmed = false ;
2017-07-17 21:28:09 +00:00
private $monero_daemon ;
2017-07-13 13:04:55 +00:00
function __construct ()
{
$this -> id = " monero_gateway " ;
$this -> method_title = __ ( " Monero GateWay " , 'monero_gateway' );
2017-09-06 11:48:00 +00:00
$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' );
2017-07-13 13:04:55 +00:00
$this -> title = __ ( " Monero Gateway " , 'monero_gateway' );
2017-07-27 11:59:38 +00:00
$this -> version = " 0.2 " ;
2017-07-13 13:04:55 +00:00
//
$this -> icon = apply_filters ( 'woocommerce_offline_icon' , '' );
$this -> has_fields = false ;
2017-07-26 19:52:48 +00:00
$this -> log = new WC_Logger ();
2017-07-13 13:04:55 +00:00
$this -> init_form_fields ();
2017-07-15 08:09:48 +00:00
$this -> host = $this -> get_option ( 'daemon_host' );
$this -> port = $this -> get_option ( 'daemon_port' );
2017-07-13 13:04:55 +00:00
$this -> address = $this -> get_option ( 'monero_address' );
2017-07-24 02:14:59 +00:00
$this -> username = $this -> get_option ( 'username' );
$this -> password = $this -> get_option ( 'password' );
2017-08-11 16:06:06 +00:00
$this -> discount = $this -> get_option ( 'discount' );
2017-07-13 13:04:55 +00: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 ;
}
2017-08-02 19:14:20 +00:00
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' ));
2017-07-13 13:04:55 +00:00
if ( is_admin ()) {
/* Save Settings */
2017-08-02 19:14:20 +00:00
add_action ( 'woocommerce_update_options_payment_gateways_' . $this -> id , array ( $this , 'process_admin_options'
2017-07-13 13:04:55 +00:00
));
2017-08-13 13:25:39 +00:00
add_filter ( 'woocommerce_currencies' , array ( $this , 'add_my_currency' ) );
add_filter ( 'woocommerce_currency_symbol' , 'add_my_currency_symbol' , 10 , 2 );
2017-08-13 12:37:46 +00:00
add_action ( 'woocommerce_email_before_order_table' , array ( $this , 'email_instructions' ), 10 , 2 );
2017-07-13 13:04:55 +00:00
}
2017-07-24 02:14:59 +00:00
$this -> monero_daemon = new Monero_Library ( $this -> host . ':' . $this -> port . '/json_rpc' , $this -> username , $this -> password );
2017-07-13 13:04:55 +00:00
}
2017-08-13 13:25:39 +00:00
public function add_my_currency ( $currencies ) {
$currencies [ 'XMR' ] = __ ( 'Monero' , 'woocommerce' );
2017-08-13 12:37:46 +00:00
return $currencies ;
}
2017-08-13 13:25:39 +00:00
public function add_my_currency_symbol ( $currency_symbol , $currency ) {
2017-08-13 12:37:46 +00:00
switch ( $currency ) {
2017-08-13 13:25:39 +00:00
case 'XMR' : $currency_symbol = 'XMR' ; break ;
2017-08-13 12:37:46 +00:00
}
return $currency_symbol ;
}
2017-07-13 13:04:55 +00:00
public function admin_options ()
{
2017-07-27 12:17:23 +00:00
$this -> log -> add ( 'Monero_gateway' , '[SUCCESS] Monero Settings OK' );
2017-08-13 12:37:46 +00:00
2017-07-13 13:04:55 +00:00
echo " <h1>Monero Payment Gateway</h1> " ;
2017-08-13 13:25:39 +00:00
2017-07-15 08:09:48 +00:00
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> " ;
2017-08-13 13:25:39 +00:00
echo " <div style='border:1px solid #DDD;padding:5px 10px;font-weight:bold;color:#223079;background-color:#9ddff3;'> " ;
$this -> getamountinfo ();
echo " </div> " ;
2017-07-13 13:04:55 +00:00
echo " <table class='form-table'> " ;
$this -> generate_settings_html ();
echo " </table> " ;
2017-07-24 02:14:59 +00:00
echo " <h4>Learn more about using a password with the monero wallet-rpc <a href= \" https://github.com/cryptochangements34/monerowp/blob/master/README.md \" >here</a></h4> " ;
2017-07-13 13:04:55 +00: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' )
),
'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' )
),
2017-07-24 02:14:59 +00:00
'daemon_host' => array (
2017-07-13 13:04:55 +00:00
'title' => __ ( 'Daemon Host/ IP' , 'monero_gateway' ),
'type' => 'text' ,
'desc_tip' => __ ( 'This is the Daemon Host/IP to authorize the payment with port' , 'monero_gateway' ),
'default' => 'localhost' ,
),
'daemon_port' => array (
'title' => __ ( 'Daemon PORT' , 'monero_gateway' ),
'type' => 'text' ,
'desc_tip' => __ ( 'This is the Daemon Host/IP to authorize the payment with port' , 'monero_gateway' ),
2017-07-15 08:09:48 +00:00
'default' => '18080' ,
),
2017-07-24 02:14:59 +00:00
'username' => array (
2017-08-10 16:46:34 +00:00
'title' => __ ( 'Daemon username' , 'monero_gateway' ),
2017-07-24 02:14:59 +00:00
'desc_tip' => __ ( 'This is the username that you used with your monero wallet-rpc' , 'monero_gateway' ),
'type' => __ ( 'text' ),
2017-07-24 15:20:46 +00:00
'default' => __ ( 'username' , 'monero_gateway' ),
2017-07-24 02:14:59 +00:00
),
'password' => array (
2017-08-10 16:46:34 +00:00
'title' => __ ( 'Daemon password' , 'monero_gateway' ),
2017-07-24 02:14:59 +00:00
'desc_tip' => __ ( 'This is the password that you used with your monero wallet-rpc' , 'monero_gateway' ),
'description' => __ ( 'you can leave these fields empty if you did not set' , 'monero_gateway' ),
'type' => __ ( 'text' ),
2017-07-24 15:20:46 +00:00
'default' => ''
2017-07-24 02:14:59 +00:00
2017-08-11 16:06:06 +00:00
),
'discount' => array (
'title' => __ ( '% discount for using XMR' , 'monero_gateway' ),
2017-08-13 18:26:51 +00:00
2017-09-06 11:48:00 +00:00
'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' ),
2017-08-13 18:26:51 +00:00
'type' => __ ( 'text' ),
'default' => '5%'
2017-08-11 16:06:06 +00:00
2017-07-24 02:14:59 +00:00
),
2017-07-13 13:04:55 +00:00
'environment' => array (
'title' => __ ( ' Test Mode' , 'monero_gateway' ),
'label' => __ ( 'Enable Test Mode' , 'monero_gateway' ),
'type' => 'checkbox' ,
2017-07-24 02:14:59 +00:00
'description' => __ ( 'Check this box if you are using testnet' , 'monero_gateway' ),
2017-07-13 13:04:55 +00:00
'default' => 'no'
2017-07-24 02:14:59 +00:00
),
2017-07-13 13:04:55 +00:00
);
}
public function retriveprice ( $currency )
{
2017-07-27 11:59:38 +00:00
$xmr_price = file_get_contents ( 'https://min-api.cryptocompare.com/data/price?fsym=XMR&tsyms=BTC,USD,EUR,CAD,INR,GBP&extraParams=monero_woocommerce' );
$price = json_decode ( $xmr_price , TRUE );
2017-08-10 16:46:34 +00:00
if ( ! isset ( $price )){
2017-07-27 12:17:23 +00:00
$this -> log -> add ( 'Monero_Gateway' , '[ERROR] Unable to get the price of Monero' );
2017-07-26 19:52:48 +00:00
}
2017-07-13 13:04:55 +00:00
if ( $currency == 'USD' ) {
2017-07-27 11:59:38 +00:00
return $price [ 'USD' ];
2017-07-13 13:04:55 +00:00
}
if ( $currency == 'EUR' ) {
2017-07-27 11:59:38 +00:00
return $price [ 'EUR' ];
}
if ( $currency == 'CAD' ){
return $price [ 'CAD' ];
}
if ( $currency == 'GBP' ){
return $price [ 'GBP' ];
}
if ( $currency == 'INR' ){
return $price [ 'INR' ];
2017-07-13 13:04:55 +00:00
}
2017-08-13 12:37:46 +00:00
if ( $currency == 'XMR' ){
$price = '1' ;
return $price ;
}
2017-07-13 13:04:55 +00:00
}
2017-08-10 16:46:34 +00:00
public function changeto ( $amount , $currency , $payment_id )
2017-07-13 13:04:55 +00:00
{
2017-08-10 16:46:34 +00:00
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 (
rate INT
) " ;
$wpdb -> query ( $create_table );
$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
2017-08-05 16:29:50 +00:00
{
2017-08-10 16:46:34 +00:00
$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
2017-08-11 16:06:06 +00:00
if ( isset ( $this -> discount ))
{
$discount_decimal = $this -> discount / 100 ;
$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 ;
$rounded_amount = round ( $new_amount , 12 ); //the moneo wallet can't handle decimals smaller than 0.000000000001
}
2017-08-05 16:29:50 +00:00
}
2017-08-10 16:46:34 +00:00
else // If the row has not been created then the live exchange rate will be grabbed and stored
2017-08-05 16:29:50 +00:00
{
$xmr_live_price = $this -> retriveprice ( $currency );
2017-08-10 16:46:34 +00:00
$live_for_storing = $xmr_live_price * 100 ; //This will remove the decimal so that it can easily be stored as an integer
$new_amount = $amount / $xmr_live_price ;
$rounded_amount = round ( $new_amount , 12 );
$wpdb -> query ( " INSERT INTO $payment_id (rate)
VALUES ( $live_for_storing ) " );
}
return $rounded_amount ;
2017-07-13 13:04:55 +00:00
}
// Submit payment and handle response
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 thankyou redirect
return array (
'result' => 'success' ,
'redirect' => $this -> get_return_url ( $order )
);
}
// Validate fields
public function validate_fields ()
{
if ( $this -> check_monero () != TRUE ) {
2017-09-06 11:48:00 +00:00
echo " <div class= \" error \" ><p>Your Monero Address doesn't seem valid. Have you checked it?</p></div> " ;
2017-07-13 13:04:55 +00:00
}
}
public function check_monero ()
{
$monero_address = $this -> settings [ 'monero_address' ];
if ( strlen ( $monero_address ) == 95 && substr ( $monero_address , 1 )) {
return true ;
}
return false ;
2017-07-17 21:28:09 +00:00
}
2017-07-13 13:04:55 +00:00
2017-08-02 22:42:01 +00:00
private function set_paymentid_cookie ()
{
if ( ! isset ( $_COOKIE [ 'payment_id' ]))
{
$payment_id = bin2hex ( openssl_random_pseudo_bytes ( 8 ));
setcookie ( 'payment_id' , $payment_id , time () + 2700 );
}
else
$payment_id = $_COOKIE [ 'payment_id' ];
return $payment_id ;
}
2017-07-13 13:04:55 +00:00
public function instruction ( $order_id )
{
$order = wc_get_order ( $order_id );
2017-08-13 12:37:46 +00:00
$amount = floatval ( preg_replace ( '#[^\d.]#' , '' , $order -> get_total ()));
2017-08-10 16:46:34 +00:00
$payment_id = $this -> set_paymentid_cookie ();
2017-08-13 12:37:46 +00:00
$currency = $order -> get_currency ();
2017-08-10 16:46:34 +00:00
$amount_xmr2 = $this -> changeto ( $amount , $currency , $payment_id );
2017-07-13 13:04:55 +00:00
$address = $this -> address ;
2017-08-14 16:25:39 +00:00
if ( ! isset ( $address )){
2017-08-13 13:25:39 +00:00
// If there isn't address (merchant missed that field!), $address will be the Monero address for donating :)
$address = " 44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A " ;
2017-08-13 12:37:46 +00:00
}
2017-07-15 11:32:34 +00:00
$uri = " monero: $address ?amount= $amount ?payment_id= $payment_id " ;
2017-07-26 16:47:25 +00:00
$array_integrated_address = $this -> monero_daemon -> make_integrated_address ( $payment_id );
2017-08-07 17:17:51 +00:00
if ( ! isset ( $array_integrated_address )){
2017-08-13 13:25:39 +00:00
$this -> log -> add ( 'Monero_Gateway' , '[ERROR] Unable to getting integrated address' );
// Seems that we can't connect with daemon, then set array_integrated_address, little hack
$array_integrated_address [ " integrated_address " ] = $address ;
2017-07-26 19:52:48 +00:00
}
2017-08-05 03:23:27 +00:00
$message = $this -> verify_payment ( $payment_id , $amount_xmr2 , $order );
2017-08-19 16:13:13 +00:00
if ( $this -> confirmed ){
2017-08-19 15:57:08 +00:00
$color = " 006400 " ;
2017-08-19 15:45:57 +00:00
}
else {
2017-08-19 15:57:08 +00:00
$color = " DC143C " ;
2017-08-19 15:45:57 +00:00
}
echo " <h4><font color= $color > " . $message . " </font></h4> " ;
2017-08-03 13:47:06 +00:00
echo " <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'> " ;
2017-07-13 13:04:55 +00:00
echo " <div class='row'>
< div class = 'col-sm-12 col-md-12 col-lg-12' >
2017-08-02 19:14:20 +00:00
< div class = 'panel panel-default' id = 'PaymentBox_de3a227fb470475' >
< div class = 'panel-body' >
< div class = 'row' >
< div class = 'col-sm-12 col-md-12 col-lg-12' >
2017-08-13 13:25:39 +00:00
< h3 > Monero Payment Box </ h3 >
2017-08-02 19:14:20 +00:00
</ div >
< div class = 'col-sm-3 col-md-3 col-lg-3' >
2017-08-13 12:37:46 +00:00
< img src = 'https://chart.googleapis.com/chart?cht=qr&chs=250x250&chl=" . $uri . "' class = 'img-responsive' >
2017-08-02 19:14:20 +00:00
</ div >
< div class = 'col-sm-9 col-md-9 col-lg-9' style = 'padding:10px;' >
2017-08-14 16:25:39 +00:00
Send < b > " . $amount_xmr2 . " XMR </ b > to < br />< input type = 'text' class = 'form-control' value = '" . $array_integrated_address["integrated_address"]."' >
2017-08-02 19:14:20 +00:00
or scan QR Code with your mobile device < br />< br />
2017-09-06 11:48:00 +00:00
< small > If you need help with how to pay with Monero or want to learn more about it , please go to the Monero < a href = '#' > site </ a >. </ small >
2017-08-02 19:14:20 +00:00
</ div >
< div class = 'col-sm-12 col-md-12 col-lg-12' >
2017-06-29 14:40:47 +00:00
2017-08-13 13:25:39 +00:00
2017-08-02 19:14:20 +00:00
</ div >
</ div >
</ div >
2017-08-13 13:25:39 +00:00
2017-08-02 19:14:20 +00:00
</ div >
2017-06-29 14:40:47 +00:00
</ div >
2017-08-02 19:14:20 +00:00
</ div >
2017-06-29 14:40:47 +00:00
2017-08-04 04:50:59 +00:00
< script type = 'text/javascript' >
2017-08-10 16:46:34 +00:00
setTimeout ( function () { location . reload ( true ); }, $this -> reloadTime );
2017-08-03 13:47:06 +00:00
</ script > " ;
2017-07-13 13:04:55 +00:00
}
// Check if we are forcing SSL on checkout pages
// Custom function not required by the Gateway
public function do_ssl_check ()
{
if ( $this -> enabled == " yes " ) {
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> " ;
}
}
}
2017-07-15 08:09:48 +00:00
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 ){
2017-09-06 11:48:00 +00:00
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 08:09:48 +00:00
< span class = \ " screen-reader-text \" >Dismiss this notice.</span>
</ button ></ p ></ div > " ;
}
else {
2017-07-27 12:17:23 +00:00
$this -> log -> add ( 'Monero_gateway' , '[ERROR] Plugin can not reach wallet rpc.' );
2017-07-15 08:09:48 +00:00
echo " <div class= \" notice notice-error \" ><p>Error with connection of daemon, see documentation!</p></div> " ;
2017-07-16 09:15:43 +00:00
} }
2017-07-15 20:02:33 +00:00
2017-08-05 03:23:27 +00:00
public function verify_payment ( $payment_id , $amount , $order_id ){
2017-07-15 20:02:33 +00:00
/*
2017-08-03 13:47:06 +00:00
* function for verifying payments
2017-08-04 04:50:59 +00:00
* Check if a payment has been made with this payment id then notify the merchant
2017-08-03 13:47:06 +00:00
*/
2017-07-15 08:09:48 +00:00
2017-08-04 04:50:59 +00:00
$amount_atomic_units = $amount * 1000000000000 ;
$get_payments_method = $this -> monero_daemon -> get_payments ( $payment_id );
if ( isset ( $get_payments_method [ " payments " ][ 0 ][ " amount " ]))
{
if ( $get_payments_method [ " payments " ][ 0 ][ " amount " ] >= $amount_atomic_units )
{
$message = " Payment has been received and confirmed. Thanks! " ;
2017-09-06 11:48:00 +00:00
$this -> log -> add ( 'Monero_gateway' , '[SUCCESS] Payment has been recorded. Congratulations!' );
2017-08-19 15:45:57 +00:00
$this -> confirmed = true ;
2017-08-05 03:23:27 +00:00
$order = wc_get_order ( $order_id );
2017-08-10 16:46:34 +00:00
$order -> update_status ( 'completed' , __ ( 'Payment has been received' , 'monero_gateway' ));
2017-08-14 16:25:39 +00:00
global $wpdb ;
2017-08-10 16:46:34 +00:00
$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
2017-08-04 04:50:59 +00:00
}
2017-08-05 03:23:27 +00:00
}
else
{
$message = " We are waiting for your payment to be confirmed " ;
}
return $message ;
2017-08-04 04:50:59 +00:00
}
2017-08-13 21:53:11 +00:00
public function getamountinfo (){
$wallet_amount = $this -> monero_daemon -> getbalance ();
2017-08-14 16:25:39 +00:00
if ( ! isset ( $wallet_amount )){
2017-09-06 11:48:00 +00:00
$this -> log -> add ( 'Monero_gateway' , '[ERROR] No connection with daemon' );
2017-08-14 16:25:39 +00:00
$wallet_amount [ 'balance' ] = " 0 " ;
$wallet_amount [ 'unlocked_balance' ] = " 0 " ;
}
2017-08-13 21:53:11 +00:00
$real_wallet_amount = $wallet_amount [ 'balance' ] / 1000000000000 ;
$real_amount_rounded = round ( $real_wallet_amount , 6 );
2017-08-13 13:25:39 +00:00
2017-08-13 21:53:11 +00:00
$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-08-10 16:46:34 +00:00
}