290 lines
8.7 KiB
PHP
290 lines
8.7 KiB
PHP
<?php
|
||
/**
|
||
* 支付集成测试脚本
|
||
*
|
||
* @package Yoone_Subscriptions
|
||
* @subpackage Tests
|
||
*/
|
||
|
||
// 防止直接访问
|
||
if (!defined('ABSPATH')) {
|
||
exit;
|
||
}
|
||
|
||
/**
|
||
* 支付集成测试类
|
||
*/
|
||
class Yoone_Payment_Integration_Test {
|
||
|
||
private $test_results = array();
|
||
private $customer_id;
|
||
private $payment_token_id;
|
||
|
||
/**
|
||
* 运行完整的支付集成测试
|
||
*/
|
||
public function run_full_test() {
|
||
$this->log_test('开始支付集成测试');
|
||
|
||
try {
|
||
// 1. 环境检查
|
||
$this->test_environment();
|
||
|
||
// 2. 创建测试数据
|
||
$this->setup_test_data();
|
||
|
||
// 3. 测试支付令牌创建
|
||
$this->test_payment_token_creation();
|
||
|
||
// 4. 测试支付令牌验证
|
||
$this->test_payment_token_validation();
|
||
|
||
// 5. 测试支付网关配置
|
||
$this->test_payment_gateway_config();
|
||
|
||
// 6. 测试支付处理(模拟)
|
||
$this->test_payment_processing();
|
||
|
||
// 7. 测试支付令牌管理
|
||
$this->test_payment_token_management();
|
||
|
||
// 8. 清理测试数据
|
||
$this->cleanup_test_data();
|
||
|
||
$this->log_test('支付集成测试完成');
|
||
|
||
} catch (Exception $e) {
|
||
$this->log_test('测试失败: ' . $e->getMessage(), 'error');
|
||
$this->cleanup_test_data();
|
||
}
|
||
|
||
return $this->test_results;
|
||
}
|
||
|
||
/**
|
||
* 环境检查
|
||
*/
|
||
private function test_environment() {
|
||
$this->log_test('检查支付环境');
|
||
|
||
// 检查必要的类是否存在
|
||
$required_classes = array(
|
||
'Yoone_Payment_Token',
|
||
'Yoone_Moneris_Gateway'
|
||
);
|
||
|
||
foreach ($required_classes as $class) {
|
||
if (!class_exists($class)) {
|
||
throw new Exception("必需的类 {$class} 不存在");
|
||
}
|
||
}
|
||
|
||
// 检查数据库表
|
||
global $wpdb;
|
||
$table = $wpdb->prefix . 'yoone_payment_tokens';
|
||
$exists = $wpdb->get_var("SHOW TABLES LIKE '{$table}'");
|
||
if (!$exists) {
|
||
throw new Exception("数据库表 {$table} 不存在");
|
||
}
|
||
|
||
$this->log_test('支付环境检查通过', 'success');
|
||
}
|
||
|
||
/**
|
||
* 创建测试数据
|
||
*/
|
||
private function setup_test_data() {
|
||
$this->log_test('创建支付测试数据');
|
||
|
||
// 创建测试客户
|
||
$this->customer_id = Yoone_Test_Config::create_test_customer();
|
||
if (!$this->customer_id) {
|
||
throw new Exception('创建测试客户失败');
|
||
}
|
||
|
||
$this->log_test("支付测试数据创建成功 - 客户ID: {$this->customer_id}", 'success');
|
||
}
|
||
|
||
/**
|
||
* 测试支付令牌创建
|
||
*/
|
||
private function test_payment_token_creation() {
|
||
$this->log_test('测试支付令牌创建');
|
||
|
||
$token = new Yoone_Payment_Token();
|
||
$token->set_customer_id($this->customer_id);
|
||
$token->set_gateway_id('moneris');
|
||
$token->set_token('test_token_' . time());
|
||
$token->set_token_type('credit_card');
|
||
$token->set_card_type('visa');
|
||
$token->set_last_four('4242');
|
||
$token->set_expiry_month('12');
|
||
$token->set_expiry_year('2025');
|
||
$token->set_default(true);
|
||
|
||
// 保存令牌
|
||
$this->payment_token_id = $token->save();
|
||
|
||
if (!$this->payment_token_id) {
|
||
throw new Exception('支付令牌创建失败');
|
||
}
|
||
|
||
// 验证令牌数据
|
||
$saved_token = new Yoone_Payment_Token($this->payment_token_id);
|
||
if ($saved_token->get_customer_id() !== $this->customer_id) {
|
||
throw new Exception('支付令牌数据验证失败');
|
||
}
|
||
|
||
$this->log_test("支付令牌创建成功 - ID: {$this->payment_token_id}", 'success');
|
||
}
|
||
|
||
/**
|
||
* 测试支付令牌验证
|
||
*/
|
||
private function test_payment_token_validation() {
|
||
$this->log_test('测试支付令牌验证');
|
||
|
||
$token = new Yoone_Payment_Token($this->payment_token_id);
|
||
|
||
// 测试令牌有效性
|
||
if (!$token->is_valid()) {
|
||
throw new Exception('支付令牌验证失败 - 令牌无效');
|
||
}
|
||
|
||
// 测试过期检查
|
||
if ($token->is_expired()) {
|
||
throw new Exception('支付令牌验证失败 - 令牌已过期');
|
||
}
|
||
|
||
$this->log_test('支付令牌验证通过', 'success');
|
||
}
|
||
|
||
/**
|
||
* 测试支付网关配置
|
||
*/
|
||
private function test_payment_gateway_config() {
|
||
$this->log_test('测试支付网关配置');
|
||
|
||
// 检查Moneris网关是否可用
|
||
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
|
||
|
||
if (!isset($available_gateways['moneris'])) {
|
||
$this->log_test('Moneris支付网关未启用,跳过配置测试', 'warning');
|
||
return;
|
||
}
|
||
|
||
$gateway = $available_gateways['moneris'];
|
||
|
||
// 检查基本配置
|
||
if (empty($gateway->get_option('store_id'))) {
|
||
$this->log_test('Moneris Store ID未配置', 'warning');
|
||
}
|
||
|
||
if (empty($gateway->get_option('api_token'))) {
|
||
$this->log_test('Moneris API Token未配置', 'warning');
|
||
}
|
||
|
||
$this->log_test('支付网关配置检查完成', 'success');
|
||
}
|
||
|
||
/**
|
||
* 测试支付处理(模拟)
|
||
*/
|
||
private function test_payment_processing() {
|
||
$this->log_test('测试支付处理(模拟)');
|
||
|
||
// 由于这是测试环境,我们只模拟支付处理流程
|
||
// 实际的支付处理需要真实的Moneris凭据和测试环境
|
||
|
||
$token = new Yoone_Payment_Token($this->payment_token_id);
|
||
|
||
// 模拟支付数据
|
||
$payment_data = array(
|
||
'amount' => 29.99,
|
||
'currency' => 'CAD',
|
||
'description' => '测试支付',
|
||
'customer_id' => $this->customer_id,
|
||
'payment_token' => $token->get_token()
|
||
);
|
||
|
||
// 验证支付数据
|
||
if (empty($payment_data['amount']) || $payment_data['amount'] <= 0) {
|
||
throw new Exception('支付金额无效');
|
||
}
|
||
|
||
if (empty($payment_data['payment_token'])) {
|
||
throw new Exception('支付令牌为空');
|
||
}
|
||
|
||
$this->log_test('支付处理模拟测试通过', 'success');
|
||
}
|
||
|
||
/**
|
||
* 测试支付令牌管理
|
||
*/
|
||
private function test_payment_token_management() {
|
||
$this->log_test('测试支付令牌管理');
|
||
|
||
// 测试获取客户令牌
|
||
$customer_tokens = Yoone_Payment_Token::get_customer_tokens($this->customer_id);
|
||
if (empty($customer_tokens)) {
|
||
throw new Exception('获取客户支付令牌失败');
|
||
}
|
||
|
||
// 测试获取默认令牌
|
||
$default_token = Yoone_Payment_Token::get_default_token($this->customer_id, 'moneris');
|
||
if (!$default_token || $default_token->get_id() !== $this->payment_token_id) {
|
||
throw new Exception('获取默认支付令牌失败');
|
||
}
|
||
|
||
// 测试令牌显示
|
||
$token = new Yoone_Payment_Token($this->payment_token_id);
|
||
$display_info = $token->get_display_name();
|
||
if (empty($display_info)) {
|
||
throw new Exception('支付令牌显示信息为空');
|
||
}
|
||
|
||
$this->log_test('支付令牌管理测试通过', 'success');
|
||
}
|
||
|
||
/**
|
||
* 清理测试数据
|
||
*/
|
||
private function cleanup_test_data() {
|
||
$this->log_test('清理支付测试数据');
|
||
|
||
// 删除测试支付令牌
|
||
if ($this->payment_token_id) {
|
||
$token = new Yoone_Payment_Token($this->payment_token_id);
|
||
$token->delete();
|
||
}
|
||
|
||
// 删除测试客户
|
||
if ($this->customer_id) {
|
||
wp_delete_user($this->customer_id);
|
||
}
|
||
|
||
$this->log_test('支付测试数据清理完成', 'success');
|
||
}
|
||
|
||
/**
|
||
* 记录测试结果
|
||
*/
|
||
private function log_test($message, $type = 'info') {
|
||
$this->test_results[] = array(
|
||
'timestamp' => current_time('mysql'),
|
||
'message' => $message,
|
||
'type' => $type
|
||
);
|
||
|
||
// 同时记录到日志系统
|
||
Yoone_Logger::info('支付集成测试: ' . $message);
|
||
}
|
||
|
||
/**
|
||
* 获取测试结果
|
||
*/
|
||
public function get_test_results() {
|
||
return $this->test_results;
|
||
}
|
||
} |