forked from wavesnode/gateway-for-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-gateway-for-woocommerce.php
195 lines (167 loc) · 7.42 KB
/
local-gateway-for-woocommerce.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Local Gateway for Woocommerce
*
* Plugin Name: Local Gateway for Woocommerce (also for other Local assets)
* Plugin URI: https://github.com/wavesnode/gateway-for-woocommerce/
* Description: Show prices in Local (or asset) and accept Local payments in your woocommerce webshop
* Version: 0.4.4
* Author: localwaves.xyz
* Author URI: https://wavesnode.net/blog/waves-woocommerce-gateway/
* License: GPLv2 or later
* License URI: http://www.opensource.org/licenses/gpl-license.php
* Text Domain: local-gateway-for-woocommerce
* Domain Path: /languages/
*
* Copyright 2018 localwaves.xyz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('WcLocal')) {
class WcLocal
{
private static $instance;
public static $version = '0.4.1';
public static $plugin_basename;
public static $plugin_path;
public static $plugin_url;
protected function __construct()
{
self::$plugin_basename = plugin_basename(__FILE__);
self::$plugin_path = trailingslashit(dirname(__FILE__));
self::$plugin_url = plugin_dir_url(self::$plugin_basename);
add_action('plugins_loaded', array($this, 'init'));
}
public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init()
{
$this->initGateway();
}
public function initGateway()
{
if (!class_exists('WC_Payment_Gateway')) {
return;
}
if (class_exists('WC_Local_Gateway')) {
return;
}
/*
* Include gateway classes
* */
include_once plugin_basename('includes/base58/src/Base58.php');
include_once plugin_basename('includes/base58/src/ServiceInterface.php');
include_once plugin_basename('includes/base58/src/GMPService.php');
include_once plugin_basename('includes/base58/src/BCMathService.php');
include_once plugin_basename('includes/class-local-gateway.php');
include_once plugin_basename('includes/class-local-api.php');
include_once plugin_basename('includes/class-local-exchange.php');
include_once plugin_basename('includes/class-local-settings.php');
include_once plugin_basename('includes/class-local-ajax.php');
add_filter('woocommerce_payment_gateways', array($this, 'addToGateways'));
add_filter('woocommerce_currencies', array($this, 'LocalCurrencies'));
add_filter('woocommerce_currency_symbol', array($this, 'LocalCurrencySymbols'), 10, 2);
add_filter('woocommerce_get_price_html', array($this, 'LocalFilterPriceHtml'), 10, 2);
add_filter('woocommerce_cart_item_price', array($this, 'LocalFilterCartItemPrice'), 10, 3);
add_filter('woocommerce_cart_item_subtotal', array($this, 'LocalFilterCartItemSubtotal'), 10, 3);
add_filter('woocommerce_cart_subtotal', array($this, 'LocalFilterCartSubtotal'), 10, 3);
add_filter('woocommerce_cart_totals_order_total_html', array($this, 'LocalFilterCartTotal'), 10, 1);
}
public static function addToGateways($gateways)
{
$gateways['local'] = 'WcLocalGateway';
return $gateways;
}
public function LocalCurrencies( $currencies )
{
$currencies['LOCAL'] = __( 'Local', 'local' );
$currencies['AMUR'] = __( 'Amur', 'amur' );
$currencies['ARTcoin'] = __( 'ARTcoin', 'ARTcoin' );
$currencies['POL'] = __( 'POLTOKEN.PL', 'POL' );
$currencies['Wykop Coin'] = __( 'WYKOP.PL', 'Wykop Coin' );
$currencies['Surfcash'] = __( 'Surfcash', 'surfcash' );
$currencies['TN'] = __( 'TurtleNode', 'tn' );
$currencies['Ecop'] = __( 'Ecop', 'Ecop' );
return $currencies;
}
public function LocalCurrencySymbols( $currency_symbol, $currency ) {
switch( $currency ) {
case 'LOCAL': $currency_symbol = 'LOCAL'; break;
case 'AMUR': $currency_symbol = 'AMUR'; break;
case 'ARTcoin': $currency_symbol = 'ARTcoin'; break;
case 'POL': $currency_symbol = 'POL'; break;
case 'Wykop Coin': $currency_symbol = 'Wykop Coin'; break;
case 'Surfcash': $currency_symbol = 'surfcash'; break;
case 'TN': $currency_symbol = 'TN'; break;
case 'Ecop': $currency_symbol = 'Ecop'; break;
}
return $currency_symbol;
}
public function LocalFilterCartTotal($value)
{
return $this->convertToLocalPrice($value, WC()->cart->total);
}
public function LocalFilterCartItemSubtotal($cart_subtotal, $compound, $that)
{
return $this->convertToLocalPrice($cart_subtotal, $that->subtotal);
}
public function LocalFilterPriceHtml($price, $that)
{
return $this->convertToLocalPrice($price, $that->price);
}
public function LocalFilterCartItemPrice($price, $cart_item, $cart_item_key)
{
$item_price = ($cart_item['line_subtotal'] + $cart_item['line_subtotal_tax']) / $cart_item['quantity'];
return $this->convertToLocalPrice($price,$item_price);
}
public function LocalFilterCartSubtotal($price, $cart_item, $cart_item_key)
{
$subtotal = $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
return $this->convertToLocalPrice($price, $subtotal);
}
private function convertToLocalPrice($price_string, $price)
{
$options = get_option('woocommerce_local_settings');
if(!in_array(get_woocommerce_currency(), array("LOCAL","AMUR","ARTcoin","POL","Wykop Coin","Surfcash","TN","Ecop")) && $options['show_prices'] == 'yes') {
$local_currency = $options['asset_code'];
if(empty($local_currency)) {
$local_currency = 'Local';
}
$local_assetId = $options['asset_id'];
if(empty($local_assetId)) {
$local_assetId = null;
}
$local_price = LocalExchange::convertToAsset(get_woocommerce_currency(), $price,$local_assetId);
if ($local_price) {
$price_string .= ' (<span class="woocommerce-price-amount amount">' . $local_price . ' </span><span class="woocommerce-price-currencySymbol">'.$local_currency.')</span>';
}
}
return $price_string;
}
}
}
WcLocal::getInstance();
function localGateway_textdomain() {
load_plugin_textdomain( 'local-gateway-for-woocommerce', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'localGateway_textdomain' );