跳到主要内容

自定义网关开发

网关类

class My_Custom_Gateway extends \WP_Ultimo\Gateways\Base_Gateway {

public $id = 'my_gateway';

public function __construct() {
$this->title = 'My Payment Gateway';
$this->description = 'Custom payment processing';
$this->supports = ['one-time', 'recurring'];

parent::__construct();
}

public function process_single_payment($payment, $cart, $order) {
$result = $this->api_call('charge', [
'amount' => $payment->get_total(),
'currency' => $payment->get_currency(),
'customer' => $payment->get_customer_id()
]);

if ($result->success) {
$payment->set_gateway_payment_id($result->transaction_id);
$payment->set_status('completed');
return true;
}

return new WP_Error('payment_failed', $result->error_message);
}

public function process_signup($membership, $customer, $cart, $order) {
$subscription = $this->api_call('subscription/create', [
'customer_id' => $customer->get_gateway_customer_id(),
'plan_id' => $membership->get_plan()->get_gateway_plan_id(),
'trial_days' => $membership->get_trial_days()
]);

if ($subscription->success) {
$membership->set_gateway_subscription_id($subscription->id);
return true;
}

return new WP_Error('subscription_failed', $subscription->error);
}
}

注册网关

add_filter('wu_payment_gateways', function($gateways) {
$gateways['my_gateway'] = 'My_Custom_Gateway';
return $gateways;
});

关键方法

方法目的
process_single_payment()处理一次性支付
process_signup()设置定期订阅
process_refund()处理退款请求
get_payment_methods()返回客户保存的支付方式

小贴士

  • 始终返回 WP_Error 以便 Ultimate Multisite 能够处理错误显示
  • 设置 $this->supports 来声明您的网关支持哪些支付类型(one-timerecurring
  • 使用 wu_log_add() 进行网关特定日志记录