Skip to content

Commit 3c8e1a2

Browse files
committed
add comment
1 parent d3e2298 commit 3c8e1a2

File tree

1 file changed

+128
-25
lines changed

1 file changed

+128
-25
lines changed

woocommerce-erc20-payment-gateway.php

Lines changed: 128 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,145 @@
1616
if (!defined('ABSPATH')) {
1717
exit;
1818
}
19-
add_filter('plugin_row_meta', 'misha_support_and_faq_links', 10, 4);
20-
21-
function misha_support_and_faq_links($links_array, $plugin_file_name, $plugin_data, $status) {
19+
/**
20+
* 添加链接到插件的 Meta 信息区域
21+
*/
22+
add_filter('plugin_row_meta', 'inkerk_add_link_to_plugin_meta', 10, 4);
2223

24+
function inkerk_add_link_to_plugin_meta($links_array, $plugin_file_name, $plugin_data, $status) {
25+
/**
26+
* 使用 if 判断当前操作的插件是否是我们自己的插件。
27+
*/
2328
if (strpos($plugin_file_name, basename(__FILE__))) {
29+
// 在数组最后加入对应的链接
30+
// 如果希望显示在前面,可以参考一下 array_unshift 函数。
2431
$links_array[] = '<a href="#">FAQ</a>';
2532
}
26-
2733
return $links_array;
2834
}
35+
/**
36+
* 添加插件名称设置
37+
*/
38+
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'inkerk_erc20_add_settings_link');
39+
function inkerk_erc20_add_settings_link($links) {
40+
$settings_link = '<a href="admin.php?page=wc-settings&tab=checkout">' . __('Settings') . '</a>';
41+
array_push($links, $settings_link);
42+
return $links;
43+
}
44+
/**
45+
* 加载 i18n 语言包
46+
*/
47+
add_action('init', 'inkerk_erc20_load_textdomain');
2948
function inkerk_erc20_load_textdomain() {
49+
/**
50+
* 这里的第一个参数为 __($str,'param') 中的 param ,即区分不同语言包域的参数
51+
*/
3052
load_plugin_textdomain('woocommerce-erc20-payment-gateway', false, basename(dirname(__FILE__)) . '/lang');
3153
}
32-
add_action('init', 'inkerk_erc20_load_textdomain');
54+
55+
/**
56+
* 添加新的 Gateway
57+
*/
3358
add_filter('woocommerce_payment_gateways', 'inkerk_erc20_add_gateway_class');
3459
function inkerk_erc20_add_gateway_class($gateways) {
3560
$gateways[] = 'WC_Inkerk_Erc20_Gateway';
3661
return $gateways;
3762
}
63+
/**
64+
* 监听插件的支付完成请求
65+
*/
3866
add_action('init', 'inkerk_thankyour_request');
3967
function inkerk_thankyour_request() {
40-
68+
/**
69+
* 判定用户请求是否是特定路径。如果此处路径修改,需要对应修改 payments.js 中的代码
70+
*/
4171
if ($_SERVER["REQUEST_URI"] == '/hook/wc_erc20') {
4272
$data = $_POST;
4373
$order_id = $data['orderid'];
4474
$tx = $data['tx'];
75+
/**
76+
* 获取到订单
77+
*/
4578
$order = wc_get_order($order_id);
79+
/**
80+
* 标记订单支付完成
81+
*/
4682
$order->payment_complete();
83+
/**
84+
* 添加订单备注,并表明 tx 的查看地址
85+
*/
4786
$order->add_order_note(__("Order payment completed", 'woocommerce-erc20-payment-gateway') . "Tx:<a target='_blank' href='http://etherscan.io/tx/" . $tx . "'>" . $tx . "</a>");
87+
/**
88+
* 需要退出,不然会显示页面内容。退出就显示空白,也开业在界面打印一段 JSON。
89+
*/
4890
exit();
4991
}
5092

5193
}
5294
/*
53-
* The class itself, please note that it is inside plugins_loaded action hook
95+
* 插件加载以及对应的 class
5496
*/
5597
add_action('plugins_loaded', 'inkerk_erc20_init_gateway_class');
5698
function inkerk_erc20_init_gateway_class() {
57-
99+
/**
100+
* 定义 class
101+
*/
58102
class WC_Inkerk_Erc20_Gateway extends WC_Payment_Gateway {
59103

60104
/**
61105
* Class constructor, more about it in Step 3
62106
*/
63107
public function __construct() {
108+
/**
109+
* 定义所需内容
110+
* @var string
111+
*/
64112
$this->id = 'inkerk_erc20';
65-
$this->has_fields = true;
113+
/**
114+
* 设置 - 付款 - 支付方式界面展示的支付方式名称
115+
* @var [type]
116+
*/
66117
$this->method_title = __('Pay with ERC20 Token', 'woocommerce-erc20-payment-gateway');
118+
/**
119+
* 用户下单时显示的按钮的文字
120+
*/
67121
$this->order_button_text = __('Use Token Payment', 'woocommerce-erc20-payment-gateway');
122+
/**
123+
* 设置 - 付款 - 支付方式界面展示的支付方式介绍
124+
*/
68125
$this->method_description = __('If you want to use this Payment Gateway, We suggest you read <a href="#">our guide </a> before.', 'woocommerce-erc20-payment-gateway');
69126

70127
$this->supports = array(
71128
'products',
72129
); // 仅支持购买
73130

131+
/**
132+
* 初始化设置及后台设置界面
133+
*/
74134
$this->init_settings();
75135
$this->init_form_fields();
76136

77-
// Turn these settings into variables we can use
137+
// 使用 foreach 将设置都赋值给对象,方便后续调用。
78138
foreach ($this->settings as $setting_key => $value) {
79139
$this->$setting_key = $value;
80140
}
81141

142+
/**
143+
* 各种 hook
144+
*/
82145
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
83146
add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
84147
add_action('woocommerce_api_compete', array($this, 'webhook'));
85148
add_action('admin_notices', array($this, 'do_ssl_check'));
86149
add_action('woocommerce_email_before_order_table', array($this, 'email_instructions'), 10, 3);
87-
add_filter('the_title', array($this, 'title_order_received'), 10, 2);
88150
add_action('woocommerce_thankyou', array($this, 'thankyou_page'));
151+
add_filter('the_title', array($this, 'title_order_received'), 10, 2);
89152
add_filter('woocommerce_currencies', array($this, 'inkerk_add_my_currency'));
90153
add_filter('woocommerce_currency_symbol', array($this, 'inkerk_add_my_currency_symbol'), 10, 2);
91-
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'inkerk_erc20_add_settings_link');
92154
}
93155

94156
/**
95-
* Plugin options, we deal with it in Step 3 too
157+
* 插件设置项目
96158
*/
97159
public function init_form_fields() {
98160

@@ -163,59 +225,98 @@ public function init_form_fields() {
163225

164226
);
165227
}
166-
228+
/**
229+
* 加载前台的支付用的 JavaScript
230+
*/
167231
public function payment_scripts() {
168232
wp_enqueue_script('inkerk_web3', plugins_url('assets/web3.min.js', __FILE__), array('jquery'), 1.1, true);
169233
wp_register_script('inkerk_payments', plugins_url('assets/payments.js', __FILE__), array('jquery', 'inkerk_web3'));
170234
wp_enqueue_script('inkerk_payments');
171235
}
172236

173-
/*
174-
* Fields validation, more in Step 5
175-
*/
237+
/**
238+
* 不做表单验证,因为结算页面没有设置表单。
239+
*/
176240
public function validate_fields() {
177241
return true;
178242
}
179243

180-
/*
181-
* We're processing the payments here, everything about it is in Step 5
182-
*/
244+
/**
245+
* 用户结算页面的下一步操作
246+
*/
183247
public function process_payment($order_id) {
184248
global $woocommerce;
185249
$order = wc_get_order($order_id);
250+
/**
251+
* 标记订单为未支付。
252+
*/
186253
$order->add_order_note(__('create order ,wait for payment', 'woocommerce-erc20-payment-gateway'));
254+
/**
255+
* 设置订单状态为 unpaid ,后续可以使用 needs_payments 监测到
256+
*/
187257
$order->update_status('unpaid', __('Wait For Payment', 'woocommerce-erc20-payment-gateway'));
258+
/**
259+
* 减少库存
260+
*/
188261
$order->reduce_order_stock();
262+
/**
263+
* 清空购物车
264+
*/
189265
WC()->cart->empty_cart();
266+
/**
267+
* 支付成功,进入 thank you 页面
268+
*/
190269
return array(
191270
'result' => 'success',
192271
'redirect' => $this->get_return_url($order),
193272
);
194273
}
274+
/**
275+
* 检查是否使用了 SSL,确保安全。
276+
*/
195277
public function do_ssl_check() {
196278
if ($this->enabled == "yes") {
197279
if (get_option('woocommerce_force_ssl_checkout') == "no") {
198280
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>";
199281
}
200282
}
201283
}
284+
/**
285+
* thank you 页面配置
286+
* 需要在此提醒用户支付。
287+
*/
202288
public function thankyou_page($order_id) {
289+
/**
290+
* 如果未传入 order_id, 就返回。
291+
*/
203292
if (!$order_id) {
204293
return;
205294
}
206295

207296
$order = wc_get_order($order_id);
297+
/**
298+
* 监测订单是否需要支付
299+
*/
208300
if ($order->needs_payment()) {
301+
/**
302+
* 如果需要支付,就输出订单信息。
303+
*/
209304
echo '<script>var order_id = ' . $order_id . ';var contract_address = "' . (string) $this->contract_address . '";var abiArray = ' . $this->abi_array . '; var target_address = "' . $this->target_address . '"; </script>';
210305
echo __('<h2 class="h2thanks">Use Metamask Pay this Order</h2>', 'woocommerce-erc20-payment-gateway');
211306
echo __('Click Button Below, Pay this order.<br>', 'woocommerce-erc20-payment-gateway');
212307
echo '<button onclick="requestPayment(' . (string) $order->get_total() . ')">' . __('Open Metamask', 'woocommerce-erc20-payment-gateway') . '</button>';
213308

214309
} else {
310+
/**
311+
* 不需要支付就显示不需要支付。
312+
*/
215313
echo __('<h2>Your Order is already Payment done.</h2>', 'woocommerce-erc20-payment-gateway');
216314
}
217315

218316
}
317+
/**
318+
* 设置 thankyou 页面的 title
319+
*/
219320
public function title_order_received($title, $id) {
220321
if (function_exists('is_order_received_page') &&
221322
is_order_received_page() && get_the_ID() === $id) {
@@ -224,21 +325,23 @@ public function title_order_received($title, $id) {
224325
return $title;
225326

226327
}
328+
/**
329+
* 添加新的货币
330+
*/
227331
public function inkerk_add_my_currency($currencies) {
228332
$currencies['ERC20'] = 'ERC20';
229333
return $currencies;
230334
}
335+
/**
336+
* 设置货币的 Symbol
337+
*/
231338
public function inkerk_add_my_currency_symbol($currency_symbol, $currency) {
232339
switch ($currency) {
233340
case 'ERC20':$currency_symbol = $this->symbol;
234341
break;
235342
}
236343
return $currency_symbol;
237344
}
238-
public function inkerk_erc20_add_settings_link($links) {
239-
$settings_link = '<a href="admin.php?page=wc-settings&tab=checkout">' . __('Settings') . '</a>';
240-
array_push($links, $settings_link);
241-
return $links;
242-
}
345+
243346
}
244347
}

0 commit comments

Comments
 (0)