-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathethereum-for-woocommerce.php
316 lines (254 loc) · 10.4 KB
/
ethereum-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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Plugin Name: Ethereum Payments for WooCommerce
* Plugin URI: https://www.ethereumpos.com
* Description: Accept Ethereum Payments in your WooCommerce shopping cart
* Author: Ethereum POS
* Author URI: https://ethereumpos.com
* Version: 0.0.1
* Text Domain: ethereum-for-woocommerce
* Domain Path: /i18n/languages/
*
* Copyright: (c) 2017 Ethereum POS
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package ethereum-for-woocommerce
* @author Ethereum POS
* @category Admin
* @copyright Copyright (c) 2017, Ethereum POS
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*
* This will automatically convert USD to ETH price and allow a customer to pay the ETH value of Dollar amount
*/
defined('ABSPATH') or exit;
// Make sure WooCommerce is active
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
return;
}
/**
* Add the gateway to WC Available Gateways
*
* @since 1.0.0
* @param array $gateways all available WC gateways
* @return array $gateways all WC gateways + offline gateway
*/
function wc_ethereum_add_to_gateways($gateways)
{
$gateways[] = 'WC_Gateway_Ethereum';
return $gateways;
}
add_filter('woocommerce_payment_gateways', 'wc_ethereum_add_to_gateways');
/**
* Adds plugin page links
*
* @since 1.0.0
* @param array $links all plugin links
* @return array $links all plugin links + our custom links (i.e., "Settings")
*/
function wc_ethereum_gateway_plugin_links($links)
{
$plugin_links = array(
'<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout§ion=ethereum_gateway') . '">' . __('Configure', 'ethereum-for-woocommerce') . '</a>'
);
return array_merge($plugin_links, $links);
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'wc_ethereum_gateway_plugin_links');
add_action('init', 'ethereum_callback_page');
function ethereum_callback_page()
{
add_feed('ethereum_callback', 'ethereum_callback');
}
function ethereum_callback()
{
$data = json_decode(file_get_contents('php://input'), true);
$secret = $data['secret'];
$refid = $data['ref_id'];
$txid = $data['transaction_id'];
$status = $data['status'];
$order = wc_get_order($refid);
if ($status == "paid") {
$order->reduce_order_stock();
$order->add_order_note(__('Ethereum Payment Paid.<br>Transaction ID: ' . $txid, 'ethereum-for-woocommerce'), 1);
$order->update_status('processing', 'ethereum-for-woocommerce');
} else if ($status == "complete") {
$order->add_order_note(__('Ethereum Payment Complete.', 'ethereum-for-woocommerce'), 1);
}
echo "<p>It works! $refid and $txid </p>";
}
/**
* Ethereum Payment Gateway
*
* Provides an Ethereum Payment Gateway; mainly for testing purposes.
* We load it later to ensure WC is loaded first since we're extending it.
*
* @class WC_Gateway_Ethereum
* @extends WC_Payment_Gateway
* @version 1.0.0
* @package WooCommerce/Classes/Payment
* @author Ethereum POS
*/
add_action('plugins_loaded', 'wc_ethereum_gateway_init', 11);
function wc_ethereum_gateway_init()
{
class WC_Gateway_Ethereum extends WC_Payment_Gateway
{
/**
* Constructor for the gateway.
*/
public function __construct()
{
$this->id = 'ethereum_gateway';
$this->icon = apply_filters('woocommerce_offline_icon', '');
$this->has_fields = false;
$this->method_title = __('Ethereum POS', 'ethereum-for-woocommerce');
$this->method_description = __('Allow Ethereum Payments to be accepted on your wordpress woocommerce site', 'ethereum-for-woocommerce');
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = "Pay with Ethereum";
$this->description = "On the next page you'll be able to pay your order with Ethereum (ETH). <p><small><a target=\"_blank\" href=\"https://ethereumpos.com\" style=\"text-align: center;float: left;width: 100%;margin-top: 5px;\">Powered by EthereumPOS.com</a></small>";
$this->testmode = $this->get_option('testmode');
$this->public_key = $this->get_option('public_key');
$this->private_key = $this->get_option('private_key');
$this->address = $this->get_option('address');
// Actions
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(
$this,
'process_admin_options'
));
add_action('woocommerce_thankyou_order_received_text', array(
$this,
'thankyou_page'
));
// Customer Emails
add_action('woocommerce_email_before_order_table', array(
$this,
'email_instructions'
), 10, 3);
}
/**
* Initialize Gateway Settings Form Fields
*/
public function init_form_fields()
{
$this->form_fields = apply_filters('wc_offline_form_fields', array(
'enabled' => array(
'title' => __('Enable/Disable', 'ethereum-for-woocommerce'),
'type' => 'checkbox',
'label' => __('Enable Ethereum Payment', 'ethereum-for-woocommerce'),
'default' => 'yes'
),
'testmode' => array(
'title' => __('Use Testnet', 'ethereum-for-woocommerce'),
'type' => 'checkbox',
'label' => __('Enable Testmode using Ropsten Ethereum Testnet', 'ethereum-for-woocommerce'),
'default' => 'yes'
),
'public_key' => array(
'title' => __('Public API Key', 'ethereum-for-woocommerce'),
'type' => 'text',
'description' => __('This controls the title for the payment method the customer sees during checkout.', 'ethereum-for-woocommerce'),
'default' => __('', 'ethereum-for-woocommerce'),
'desc_tip' => true
),
'private_key' => array(
'title' => __('Secret API Key', 'ethereum-for-woocommerce'),
'type' => 'text',
'description' => __('Payment method description that the customer will see on your checkout.', 'ethereum-for-woocommerce'),
'default' => __('', 'ethereum-for-woocommerce'),
'desc_tip' => true
),
'address' => array(
'title' => __('Ethereum Address', 'ethereum-for-woocommerce'),
'type' => 'text',
'description' => __('Instructions that will be added to the thank you page and emails.', 'ethereum-for-woocommerce'),
'default' => '',
'desc_tip' => true
)
));
}
/**
* Output for the order received page.
*/
public function thankyou_page()
{
$eth_id = $_GET['eth_id'];
$added_text = "<iframe src=\"https://ethereumpos.com/payment/$eth_id\" style=\"width: 100%;height: 310px;border: 0;\"></iframe>";
return $added_text;
}
/**
* Add content to the WC emails.
*
* @access public
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
*/
public function email_instructions($order, $sent_to_admin, $plain_text = false)
{
if ($this->instructions && !$sent_to_admin && $this->id === $order->payment_method && $order->has_status('on-hold')) {
echo wpautop(wptexturize($this->instructions)) . PHP_EOL;
}
}
/**
* Process the payment and return the result
*
* @param int $order_id
* @return array
*/
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
$testmode = $this->testmode;
if ($testmode == true) {
$post_url = 'https://testapi.ethereumpos.com/order';
} else {
$post_url = 'https://api.ethereumpos.com/order';
}
$oid = $order->get_order_number();
$amount = $order->get_total();
$callback = get_site_url() . '/ethereum_callback';
$myaddress = $this->address;
$arg_data = array(
'ref_id' => $oid,
'amount' => $amount,
'address' => $myaddress,
'callback' => $callback
);
$data = json_encode($arg_data);
$auth = $this->private_key;
$args = array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => $auth
),
'body' => $data
);
$response = wp_remote_post(esc_url_raw($post_url), $args);
$response_body = wp_remote_retrieve_body($response);
$result = json_decode($response_body);
// Mark as on-hold (we're awaiting the payment)
$order->update_status('pending', __('Awaiting Ethereum Payment', 'ethereum-for-woocommerce'));
// Remove cart
WC()->cart->empty_cart();
$ethoid = $result->id;
$ethaddr = $result->address;
$ethamount = $result->expected_amount;
$payload = array(
"eth_id" => $ethoid,
"eth_amount" => $ethamount,
"eth_address" => $ethaddr
);
$order->add_order_note(__('EthereumPOS.com Order ID: ' . $ethoid . '<br>Pay Exactly ' . $ethamount . ' ETH<br>Send to Address: ' . $ethaddr . '<br>', 'ethereum-for-woocommerce'), 1);
$querystring = http_build_query($payload);
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order) . '&' . $querystring
);
}
} // end \WC_Gateway_Ethereum class
}