better-staridc-MNBT
1<?php
2/**
3 * WooCommerce 集成:商品扩展、支付回调开通/续费、续费下单。
4 *
5 * @package MnbtWp
6 */
7
8namespace MnbtWp\Woo;
9
10use MnbtWp\Helpers;
11use MnbtWp\Services\Billing;
12use MnbtWp\Services\Provision;
13
14if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16}
17
18/**
19 * WooCommerce 集成。
20 */
21class Woo {
22
23 /**
24 * 构造:挂载钩子。
25 */
26 public function __construct() {
27 add_action( 'woocommerce_product_options_general_product_data', array( $this, 'product_meta_fields' ) );
28 add_action( 'woocommerce_process_product_meta', array( $this, 'save_product_meta' ) );
29 add_action( 'woocommerce_payment_complete', array( $this, 'on_order_paid' ), 20, 1 );
30 add_action( 'woocommerce_order_status_completed', array( $this, 'on_order_paid' ), 20, 1 );
31 }
32
33 /**
34 * 商品常规设置面板输出 MNBT 主机套餐字段。
35 */
36 public function product_meta_fields() {
37 global $post;
38 $pid = (int) $post->ID;
39
40 $enabled = 'yes' === get_post_meta( $pid, '_mnbtwp_enabled', true );
41 $provider = (int) get_post_meta( $pid, '_mnbtwp_provider_id', true );
42 $webdx = (int) get_post_meta( $pid, '_mnbtwp_webdx', true );
43 $sqldx = (int) get_post_meta( $pid, '_mnbtwp_sqldx', true );
44 $flow = (int) get_post_meta( $pid, '_mnbtwp_ll', true );
45 $ymbds = (int) get_post_meta( $pid, '_mnbtwp_ymbds', true );
46 $months = (int) get_post_meta( $pid, '_mnbtwp_period_months', true ) ?: 12;
47
48 global $wpdb;
49 $providers = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}mnbt_providers WHERE qk='true' ORDER BY id ASC", ARRAY_A );
50
51 echo '<div class="options_group show_if_simple">';
52 echo '<p class="form-field"><label style="font-weight:600;">' . esc_html__( 'MNBT 主机套餐', 'wp-seller-plugin' ) . '</label></p>';
53
54 woocommerce_wp_checkbox(
55 array(
56 'id' => '_mnbtwp_enabled',
57 'label' => __( '作为 MNBT 主机套餐', 'wp-seller-plugin' ),
58 'cbvalue' => 'yes',
59 )
60 );
61
62 echo '<p class="form-field _mnbtwp_fields">';
63 echo '<label for="_mnbtwp_provider_id">' . esc_html__( '节点', 'wp-seller-plugin' ) . '</label>';
64 echo '<select id="_mnbtwp_provider_id" name="_mnbtwp_provider_id" class="select short">';
65 echo '<option value="0">' . esc_html__( '选择节点', 'wp-seller-plugin' ) . '</option>';
66 foreach ( $providers as $p ) {
67 $sel = ( $provider === (int) $p['id'] ) ? ' selected' : '';
68 echo '<option value="' . (int) $p['id'] . '"' . $sel . '>' . esc_html( $p['name'] ) . '</option>';
69 }
70 echo '</select></p>';
71
72 woocommerce_wp_text_input( array( 'id' => '_mnbtwp_webdx', 'label' => __( '网站空间 (MB)', 'wp-seller-plugin' ), 'type' => 'number', 'value' => $webdx ) );
73 woocommerce_wp_text_input( array( 'id' => '_mnbtwp_sqldx', 'label' => __( '数据库空间 (MB)', 'wp-seller-plugin' ), 'type' => 'number', 'value' => $sqldx ) );
74 woocommerce_wp_text_input( array( 'id' => '_mnbtwp_ll', 'label' => __( '流量 (MB,0=不限)', 'wp-seller-plugin' ), 'type' => 'number', 'value' => $flow ) );
75 woocommerce_wp_text_input( array( 'id' => '_mnbtwp_ymbds', 'label' => __( '域名绑定数', 'wp-seller-plugin' ), 'type' => 'number', 'value' => $ymbds ) );
76 woocommerce_wp_text_input( array( 'id' => '_mnbtwp_period_months', 'label' => __( '周期 (月)', 'wp-seller-plugin' ), 'type' => 'number', 'value' => $months ) );
77 echo '</div>';
78
79 // 当启用开关变化时显隐字段。
80 ?>
81 <script type="text/javascript">
82 jQuery(function ($) {
83 function mnbtwpToggle() {
84 if ($('#_mnbtwp_enabled').prop('checked')) {
85 $('._mnbtwp_fields').show();
86 } else {
87 $('._mnbtwp_fields').hide();
88 }
89 }
90 $('#_mnbtwp_enabled').on('change', mnbtwpToggle);
91 mnbtwpToggle();
92 });
93 </script>
94 <?php
95 }
96
97 /**
98 * 保存商品 meta。
99 *
100 * @param int $post_id 商品 ID。
101 */
102 public function save_product_meta( $post_id ) {
103 if ( ! empty( $_POST['_mnbtwp_enabled'] ) && 'yes' === $_POST['_mnbtwp_enabled'] ) {
104 update_post_meta( $post_id, '_mnbtwp_enabled', 'yes' );
105 } else {
106 delete_post_meta( $post_id, '_mnbtwp_enabled' );
107 }
108 update_post_meta( $post_id, '_mnbtwp_provider_id', max( 0, (int) ( $_POST['_mnbtwp_provider_id'] ?? 0 ) ) );
109 update_post_meta( $post_id, '_mnbtwp_webdx', max( 0, (int) ( $_POST['_mnbtwp_webdx'] ?? 0 ) ) );
110 update_post_meta( $post_id, '_mnbtwp_sqldx', max( 0, (int) ( $_POST['_mnbtwp_sqldx'] ?? 0 ) ) );
111 update_post_meta( $post_id, '_mnbtwp_ll', max( 0, (int) ( $_POST['_mnbtwp_ll'] ?? 0 ) ) );
112 update_post_meta( $post_id, '_mnbtwp_ymbds', max( 0, (int) ( $_POST['_mnbtwp_ymbds'] ?? 0 ) ) );
113 update_post_meta( $post_id, '_mnbtwp_period_months', max( 1, (int) ( $_POST['_mnbtwp_period_months'] ?? 12 ) ) );
114 }
115
116 /**
117 * 订单支付完成:续费订单走续费,否则走开通。
118 *
119 * @param int $order_id 订单 ID。
120 */
121 public function on_order_paid( $order_id ) {
122 $order = wc_get_order( $order_id );
123 if ( ! $order ) {
124 return;
125 }
126 try {
127 if ( get_post_meta( $order_id, '_mnbtwp_renew_host_id', true ) ) {
128 Billing::renew_order( $order_id );
129 } else {
130 Provision::provision_order( $order_id );
131 }
132 } catch ( \Exception $e ) {
133 // 记录失败(开通失败可在后台重试;续费失败待支付单手动处理)。
134 $order->add_order_note( 'MNBT 处理失败:' . $e->getMessage() );
135 }
136 }
137
138 /**
139 * 创建续费订单(含原商品 + 续费 meta),返回订单对象。
140 *
141 * @param int $host_id 主机 ID。
142 * @param int $months 续费月数。
143 * @return \WC_Order
144 *
145 * @throws \Exception 原商品/订单缺失时抛出。
146 */
147 public static function create_renew_order( $host_id, $months ) {
148 $host = Helpers::get_host( $host_id );
149 if ( ! $host ) {
150 throw new \Exception( __( '主机不存在', 'wp-seller-plugin' ) );
151 }
152 $months = max( 1, (int) $months );
153
154 $order = $host['order_id'] ? wc_get_order( (int) $host['order_id'] ) : null;
155 $product_id = 0;
156 if ( $order ) {
157 foreach ( $order->get_items() as $item ) {
158 $pid = (int) $item->get_product_id();
159 if ( 'yes' === get_post_meta( $pid, '_mnbtwp_enabled', true ) ) {
160 $product_id = $pid;
161 break;
162 }
163 }
164 }
165 if ( ! $product_id ) {
166 throw new \Exception( __( '找不到原主机商品', 'wp-seller-plugin' ) );
167 }
168 $product = wc_get_product( $product_id );
169 if ( ! $product ) {
170 throw new \Exception( __( '原商品不存在', 'wp-seller-plugin' ) );
171 }
172
173 $order = wc_create_order( array( 'customer_id' => (int) $host['user_id'] ) );
174 $order->add_product( $product, $months );
175 $order->add_meta_data( '_mnbtwp_renew_host_id', (int) $host_id );
176 $order->add_meta_data( '_mnbtwp_renew_months', $months );
177 $order->set_status( 'pending' );
178 $order->calculate_totals();
179 $order->save();
180
181 return $order;
182 }
183}