yoone-wc-moneris-payments/includes/blocks/class-yoone-blocks-moneris.php

113 lines
4.7 KiB
PHP

<?php
if (! defined('ABSPATH')) { exit; }
// Minimal WooCommerce Blocks integration to register Moneris as a Blocks payment method
// and capture payment data posted via the Store API.
/**
* Store API checkout payment data capturing.
* Defined as a standalone function to avoid hard dependency on Blocks classes during autoload.
*/
function yoone_moneris_store_payment_data($order, $result, $request) {
if (! $order || ! is_object($order)) {
return;
}
// Determine the payment method used in request.
$pm = '';
if (is_object($request) && method_exists($request, 'get_param')) {
$pm = (string) $request->get_param('payment_method');
} elseif (is_array($result) && isset($result['payment_method'])) {
$pm = (string) $result['payment_method'];
}
if ('yoone_moneris' !== $pm) {
return;
}
// Extract payment method data.
$data = array();
if (is_object($request) && method_exists($request, 'get_param')) {
$data = $request->get_param('payment_method_data');
} elseif (is_array($result) && isset($result['payment_method_data'])) {
$data = $result['payment_method_data'];
}
if (is_array($data) && isset($data['yoone_moneris']) && is_array($data['yoone_moneris'])) {
$payload = $data['yoone_moneris'];
} else {
$payload = is_array($data) ? $data : array();
}
if (! empty($payload)) {
$order->update_meta_data('_yoone_moneris_pm_data', $payload);
$order->save();
}
}
// Only declare the Blocks payment method class when the Blocks base type exists to prevent type resolution issues.
if (class_exists('Automattic\WooCommerce\Blocks\Payments\PaymentMethodType') && ! class_exists('Yoone_Moneris_Blocks_Payment_Method')) {
/**
* 使用 use 导入基类,避免 IDE 报 Undefined type
*/
class Yoone_Moneris_Blocks_Payment_Method extends Automattic\WooCommerce\Blocks\Payments\PaymentMethodType {
/**
* Unique name of the payment method in Blocks.
* This MUST match the gateway id used by WooCommerce (yoone_moneris).
*/
public function get_name() {
return 'yoone_moneris';
}
/**
* Whether this payment method should be active in Blocks.
* Mirrors the gateway availability: enabled + CAD/USD + HTTPS or sandbox.
*/
public function is_active() {
$settings = get_option('woocommerce_yoone_moneris_settings', array());
$enabled = isset($settings['enabled']) ? (string)$settings['enabled'] : 'no';
if ('yes' !== strtolower($enabled)) {
return false;
}
$currency = function_exists('get_woocommerce_currency') ? get_woocommerce_currency() : 'CAD';
if (! in_array($currency, array('CAD', 'USD'), true)) {
return false;
}
$is_ssl = function_exists('is_ssl') ? is_ssl() : false;
$sandbox = isset($settings['sandbox']) ? (string)$settings['sandbox'] : 'yes';
if (! $is_ssl && 'yes' !== strtolower($sandbox)) {
return false;
}
return true;
}
/**
* Script handles required to render this payment method in Blocks checkout.
* The script is registered in the plugin bootstrap and provides UI + data posting.
*/
public function get_payment_method_script_handles() {
return array('yoone-moneris-blocks');
}
/**
* Data exposed to the frontend via wcSettings for the JS to consume.
*/
public function get_payment_method_data() {
$settings = get_option('woocommerce_yoone_moneris_settings', array());
$title = isset($settings['title']) ? (string)$settings['title'] : __('Credit Card (Moneris)', 'yoone-moneris');
return array(
'title' => $title,
'enabled' => isset($settings['enabled']) ? ('yes' === strtolower((string)$settings['enabled'])) : false,
'sandbox' => isset($settings['sandbox']) ? (string)$settings['sandbox'] : 'yes',
'currency' => function_exists('get_woocommerce_currency') ? get_woocommerce_currency() : 'CAD',
);
}
/**
* Declares supported features in Blocks.
*/
public function get_supported_features() {
return array('products');
}
}
}
// Register hooks to capture Store API checkout data for Blocks.
add_action('woocommerce_store_api_checkout_order_processed', 'yoone_moneris_store_payment_data', 10, 3);
// Legacy/alternate hook name for some Woo versions.
add_action('woocommerce_blocks_checkout_order_processed', 'yoone_moneris_store_payment_data', 10, 3);