| Server IP : 43.141.49.92 / Your IP : 113.219.202.141 Web Server : Apache System : Linux VM-8-5-opencloudos 6.6.34-9.oc9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 19 19:35:45 CST 2024 x86_64 User : www ( 1000) PHP Version : 8.1.27 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/www.ucppt.com/wp-content/plugins/wpcom-member-pro/payment/ |
Upload File : |
<?php
namespace WPCOM\Member\Payments;
defined('ABSPATH') || exit;
class Wallet_Woo extends \WC_Payment_Gateway {
public function __construct() {
$this->id = 'wallet';
$this->has_fields = false;
$this->form_fields = [
'enabled' => array(
'title' => '启用/禁用',
'type' => 'checkbox',
'label' => '启用余额付款',
'default' => 'no'
),
'title' => array(
'title' => '标题',
'type' => 'text',
'description' => '顾客支付的时候会看到关于该支付方式的说明',
'default' => '余额支付',
'css' => 'width:400px'
),
'description' => array(
'title' => '描述',
'type' => 'textarea',
'description' => '顾客在你网站上看到的付款方式描述',
'css' => 'width:400px'
),
];
$this->init_settings();
$this->method_title = '钱包余额支付';
$this->method_description = '使用钱包余额进行支付';
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->order_button_text = '余额支付下单';
array_push($this->supports, 'refunds');
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_action('wp_ajax_wpcom_wallet_woo_pay', array($this, 'paynow'));
add_action('wp_ajax_nopriv_wpcom_wallet_woo_pay', array($this, 'paynow'));
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_update_options_payment_gateways', array($this, 'process_admin_options'));
add_filter('woocommerce_payment_gateways', array($this, 'add_gateway'));
}
function log($log, $level = 'debug') {
$logger = wc_get_logger();
$context = array('source' => $this->id);
$logger->log($level, $log, $context);
}
function add_gateway($methods) {
$methods[] = $this;
return $methods;
}
public function process_payment($order_id) {
$order = new \WC_Order($order_id);
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url(true)
);
}
public function process_refund($order_id, $amount = null, $reason = '') {
$order = new \WC_Order($order_id);
if (!$order) return new \WP_Error('invalid_order', '错误的订单');
$trade_no = $order->get_transaction_id();
$order_no = $order->get_order_number();
if (empty($trade_no)) {
return new \WP_Error('invalid_order', '未找到支付交易号或订单未支付');
}
$this->log('#' . $order_id . ' 余额发起退款,订单号:' . $order_no . ';退款金额:' . $amount);
$total = $order->get_total();
if ($amount <= 0 || $amount > $total) {
return new \WP_Error('invalid_order', '退款金额错误');
}
$title = '[退款]#' . $order_no . ':' . ($reason ? $reason : '余额支付退款');
if (\WPCOM\Member\Wallet::use_balance($amount, $order->get_user_id(), $title)) {
$this->log('#' . $order_id . ' 余额退款成功;退款金额:' . $amount);
} else {
$this->log('#' . $order_id . ' 余额退款失败');
throw new \Exception("余额退款失败");
}
return true;
}
public function receipt_page($order_id) {
$order = new \WC_Order($order_id);
if (!$order || $order->is_paid()) return;
$user = wp_get_current_user();
if ($user && isset($user->ID) && $user->ID) {
$balance = $this->get_balance();
if (is_numeric($balance)) {
$price = $order->get_total();
if (is_numeric($price) && $price > 0) {
if ($balance >= $price) { ?>
<div class="text-center" style="color: #666;font-size: 16px;line-height: 1;">可用余额<span style="display: block;margin-top: 6px;font-size: 28px;font-weight:600;color: #f76142;">¥<?php echo $balance; ?></span></div>
<div class="text-center" style="margin-top: 1em;"><button type="button" class="btn btn-primary btn-view btn-pay">立即支付</button></div>
<script>
_ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>";
_nonce = "<?php echo wp_create_nonce('wpcom_wallet_receipt_page'); ?>";
jQuery(function($) {
$(document.body).on('click', '.btn-view.btn-pay', function() {
var $btn = $(this);
if ($btn.hasClass('loading')) return false;
$btn.addClass('loading').html('支付中,请稍候...');
jQuery.ajax({
type: 'POST',
url: _ajax_url,
dataType: 'json',
data: {
order_id: <?php echo $order_id; ?>,
action: 'wpcom_wallet_woo_pay',
nonce: _nonce
}
}).done(function(data) {
if (data && data.redirect) {
location.href = data.redirect;
} else {
$btn.removeClass('loading').html('立即支付');
wpcom_notice('支付异常,请稍候再试或者联系管理员获取帮助', 'error');
}
});
});
})
</script>
<?php } else {
$wallet_url = wpcom_subpage_url('wallet');
echo '<div class="text-center" style="color:#f66;"><p style="margin-bottom: .2em;">钱包余额不足!</p>请先充值或者使用用其他支付方式进行支付。</div><div class="text-center" style="margin-top:1em;"><a class="btn btn-primary btn-view" target="_blank" href="' . esc_url($wallet_url) . '">进入我的钱包充值 <i class="wi wpcom-icon"><svg class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5742" width="128" height="128"><path d="M690.005333 469.333333l-228.864-228.864 60.330667-60.330666L853.333333 512l-331.861333 331.861333-60.330667-60.330666L690.005333 554.666667H170.666667v-85.333334z" p-id="5743"></path></svg></i></a></div>';
}
} else {
echo '<div class="text-center" style="color:#f66;">订单价格异常</div>';
}
} else {
echo '<div class="text-center" style="color:#f66;">余额获取失败</div>';
}
} else {
echo '<div class="text-center" style="color:#f66;">请登录后再试</div>';
}
}
public function get_order_title($order) {
$id = $order->get_order_number();
$title = get_option('blogname');
$order_items = $order->get_items();
if ($order_items && count($order_items) > 0) {
$title = "#{$id} ";
$index = 0;
foreach ($order_items as $item_id => $item) {
if ($index > 0) $title .= '; ';
$title .= $item['name'];
$index++;
}
}
/**
* 用于自定义商品支付订单标题
* @param string $title 标题
* @param object $order 订单信息
* @param string $this->id 使用的支付接口标识
*/
$title = apply_filters('wpcom_woo_payment_title', $title, $order, $this->id);
if (strlen($title) >= 127) {
if (function_exists('mb_strcut')) {
$title = mb_strcut($title, 0, 123, 'UTF-8') . '…';
} else {
// 降级方案:使用正则粗略处理 UTF-8 字符
$truncated = preg_replace('/^(.{0,123})[\s\S]*/u', '$1', $title);
$title = $truncated . '…';
}
}
return $title;
}
function get_balance() {
$user = wp_get_current_user();
if ($user && isset($user->ID) && $user->ID) {
$balance = get_user_option('_wpcom_balance', $user->ID);
if ($balance === '' || $balance === false) $balance = '0.00';
return $balance;
}
}
function paynow() {
$res = array('result' => -1);
$order_id = isset($_POST['order_id']) && $_POST['order_id'] ? $_POST['order_id'] : '';
$nonce = isset($_POST['nonce']) && $_POST['nonce'] ? $_POST['nonce'] : '';
if ($nonce && wp_verify_nonce($nonce, 'wpcom_wallet_receipt_page')) {
$order = $order_id ? new \WC_Order($order_id) : '';
$user_id = get_current_user_id();
if ($order && $order->get_id()) {
$res['redirect'] = $order->get_checkout_order_received_url();
self::log('#' . $order->get_id() . ' 提交余额支付请求', $this->id);
}
if ($user_id && $order && $order->get_id() && (int) $order->get_user_id() === $user_id) {
$order_number = $order->get_order_number();
$balance = $this->get_balance();
if (is_numeric($balance)) {
$price = $order->get_total();
if (is_numeric($price) && $price > 0 && $balance >= $price && $order->needs_payment()) {
$title = $this->get_order_title($order);
$use_balance = \WPCOM\Member\Wallet::use_balance(- ($price), $user_id, $title);
if ($use_balance) {
$res['result'] = 0;
$order->payment_complete($order_number);
self::log('#' . $order_number . ' 订单支付完成!', $this->id);
}
}
}
}
}
wp_send_json($res);
}
}