Woocommerce 自定义支付网关 IPN 不起作用

Woocommerce Custom Payment Gateway IPN not working

我正在 WordPress 插件上开发支付网关。我在 Java 后端和 WordPress 插件之间遇到回调问题。 Java 后端要向 WordPress 插件发送订单更新。我有两个选择:

选项 1:我使用回调函数(按照说明进行操作 https://woocommerce.com/document/payment-gateway-api/#section-2) 这是我整合的代码

<?php

/**
 * Plugin Name: H Payments
 * Plugin URI:
 * Author Name: H Team
 * Author URI:
 * Description:
 * Version: 0.1.0
 * License: 0.1.0
 * License URL:
 * text-domain: woocommerce-h-pay-woo
 */

if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    return;
}

add_action( 'plugins_loaded', 'payment_init' );

function payment_init() {
    if ( class_exists( 'WC_Payment_Gateway' ) ) {
        class WC_H_Payment_Gateway extends WC_Payment_Gateway {
            function __construct() {
                $this->code               = 'WC_H_Payment_Gateway';
                $this->path               = 'hpay/v1';
                $this->id                 = 'h_payment';
                $this->icon               = apply_filters( 'woocommerce_h_icon', plugins_url( '/assets/icon/icon.png', __FILE__ ) );
                $this->has_fields         = true;
                $this->method_title       = __( 'H Payment', 'woocommerce-h-pay-woo' );
                $this->method_description = __( 'H Payment Systems', 'woocommerce-h-pay-woo' );
                $this->title              = $this->get_option( 'title' );
                $this->description        = $this->get_option( 'description' );
                $this->init_form_fields();
                $this->init_settings();
                add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array(
                    $this,
                    'process_admin_options'
                ) );

                add_action( 'woocommerce_api_wc_h_payment_gateway', array( $this, 'check_h_payment_response' ) );
            }

            public function init_rest_api_callback() {
                register_rest_route( 'hpay/v1', '/callback', [
                    'methods'  => 'GET',
                    'callback' => [ $this, 'check_h_payment_response' ],
                ] );
            }

            public function init_form_fields() {
                $this->form_fields = apply_filters( 'h_payment_fields', array(
                    'enable' => array(
                        'title'   => __( 'Enable/Disable', 'woocommerce-h-pay-woo' ),
                        'type'    => 'checkbox',
                        'label'   => __( 'Enable or Disable H Payment', 'woocommerce-h-pay-woo' ),
                        'default' => true
                    ),
                    'title' => array(
                        'title'       => __( 'H Payments Gateway', 'woocommerce-h-pay-woo' ),
                        'type'        => 'text',
                        'default'     => __( 'H Payments Gateway', 'woocommerce-h-pay-woo' ),
                        'desc_tip'    => true,
                        'description' => __( 'Add a new title for the H Payments Gateway that customers will see when they are in the checkout page.', 'noob-pay-woo' )
                    ),
                    'description' => array(
                        'title'       => __( 'H Pay Description', 'woocommerce-h-pay-woo' ),
                        'type'        => 'textarea',
                        'default'     => __( 'By using H Payment you agree to our terms of service and privacy statement', 'woocommerce-h-pay-woo' ),
                        'description' => __( 'Simple description', 'woocommerce-h-pay-woo' ),
                        'desc_tip'    => true
                    ),
                ) );
            }

            public function process_payment( $order_id ) {
                $order = wc_get_order( $order_id );
                $order->update_status( 'on-hold',  __( 'Awaiting Payment', 'woocommerce-h-pay-woo') );
                $order->reduce_order_stock();
                WC()->cart->empty_cart();
                return array(
                    'result'   => 'success',
                    'redirect' => $this->get_return_url( $order ),
                );
            }

            public function check_h_payment_response() {
                // do sth
                if ( isset( $_GET['h_transaction_uuid'] ) ) {
                    return "SUCCESS";
                } else {
                    return "ERROR";
                }
            }
        }
    }
}

add_filter( 'woocommerce_payment_gateways', 'add_to_h_payment_gateway' );

function add_to_h_payment_gateway( $gateways ) {
    $gateways[] = 'WC_H_Payment_Gateway';

    return $gateways;
}

然后我通过URL http:///localhost/mystore/wc-api/wc_h_payment_gateway?h_transaction_uuid=1234 调用但是结果是-1 (https://i.stack.imgur.com/FhSry.png)

选项 2:我使用 register_rest_route 注册休息 api:(https://i.stack.imgur.com/jle1Z.png) 这是我整合的代码

<?php

/**
 * Plugin Name: H Payments
 * Plugin URI:
 * Author Name: H Team
 * Author URI:
 * Description:
 * Version: 0.1.0
 * License: 0.1.0
 * License URL:
 * text-domain: woocommerce-h-pay-woo
 */

if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    return;
}

add_action( 'plugins_loaded', 'payment_init' );

function payment_init() {
    if ( class_exists( 'WC_Payment_Gateway' ) ) {
        class WC_H_Payment_Gateway extends WC_Payment_Gateway {
            function __construct() {
                $this->code               = 'WC_H_Payment_Gateway';
                $this->path               = 'hpay/v1';
                $this->id                 = 'h_payment';
                $this->icon               = apply_filters( 'woocommerce_h_icon', plugins_url( '/assets/icon/icon.png', __FILE__ ) );
                $this->has_fields         = true;
                $this->method_title       = __( 'H Payment', 'woocommerce-h-pay-woo' );
                $this->method_description = __( 'H Payment Systems', 'woocommerce-h-pay-woo' );
                $this->title              = $this->get_option( 'title' );
                $this->description        = $this->get_option( 'description' );
                $this->init_form_fields();
                $this->init_settings();
                add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array(
                    $this,
                    'process_admin_options'
                ) );

                add_action( 'rest_api_init_custom', array( $this, 'init_rest_api_callback' ) );
                do_action( 'rest_api_init_custom' );
            }

            public function init_rest_api_callback() {
                register_rest_route( 'hpay/v1', '/callback', [
                    'methods'  => 'GET',
                    'callback' => [ $this, 'check_h_payment_response' ],
                ] );
            }

            public function init_form_fields() {
                $this->form_fields = apply_filters( 'h_payment_fields', array(
                    'enable' => array(
                        'title'   => __( 'Enable/Disable', 'woocommerce-h-pay-woo' ),
                        'type'    => 'checkbox',
                        'label'   => __( 'Enable or Disable H Payment', 'woocommerce-h-pay-woo' ),
                        'default' => true
                    ),
                    'title' => array(
                        'title'       => __( 'H Payments Gateway', 'woocommerce-h-pay-woo' ),
                        'type'        => 'text',
                        'default'     => __( 'H Payments Gateway', 'woocommerce-h-pay-woo' ),
                        'desc_tip'    => true,
                        'description' => __( 'Add a new title for the H Payments Gateway that customers will see when they are in the checkout page.', 'noob-pay-woo' )
                    ),
                    'description' => array(
                        'title'       => __( 'H Pay Description', 'woocommerce-h-pay-woo' ),
                        'type'        => 'textarea',
                        'default'     => __( 'By using H Payment you agree to our terms of service and privacy statement', 'woocommerce-h-pay-woo' ),
                        'description' => __( 'Simple description', 'woocommerce-h-pay-woo' ),
                        'desc_tip'    => true
                    ),
                ) );
            }

            public function process_payment( $order_id ) {
                $order = wc_get_order( $order_id );
                $order->update_status( 'on-hold',  __( 'Awaiting Payment', 'woocommerce-h-pay-woo') );
                $order->reduce_order_stock();
                WC()->cart->empty_cart();
                return array(
                    'result'   => 'success',
                    'redirect' => $this->get_return_url( $order ),
                );
            }

            public function check_h_payment_response() {
                // do sth
                if ( isset( $_GET['h_transaction_uuid'] ) ) {
                    return "SUCCESS";
                } else {
                    return "ERROR";
                }
            }
        }
    }
}

add_filter( 'woocommerce_payment_gateways', 'add_to_h_payment_gateway' );

function add_to_h_payment_gateway( $gateways ) {
    $gateways[] = 'WC_H_Payment_Gateway';

    return $gateways;
}

Rest API 已注册,但当我点击结帐时出现错误“内部服务器错误”

[15-May-2022 15:44:30 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_payment_gateway_ids() on null in C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\Schemas\V1\CheckoutSchema.php:115
Stack trace:
#0 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\Schemas\V1\AbstractSchema.php(62): Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema->get_properties()
#1 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\Routes\V1\AbstractRoute.php(85): Automattic\WooCommerce\StoreApi\Schemas\V1\AbstractSchema->get_item_schema()
#2 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\Routes\V1\AbstractRoute.php(263): Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute->get_item_schema()
#3 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\Routes\V1\Checkout.php(70): Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute->get_context_param(Array)
#4 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\RoutesController.php(113): Automattic\WooCommerce\StoreApi\Routes\V1\Checkout->get_args()
#5 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\RoutesController.php(68): Automattic\WooCommerce\StoreApi\RoutesController->register_routes('v1', 'wc/store')
#6 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\packages\woocommerce-blocks\src\StoreApi\StoreApi.php(26): Automattic\WooCommerce\StoreApi\RoutesController->register_all_routes()
#7 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(307): Automattic\WooCommerce\StoreApi\StoreApi->Automattic\WooCommerce\StoreApi\{closure}(Object(WP_REST_Server))
#8 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array)
#9 C:\xampp\htdocs\hodlerext\wp-includes\plugin.php(474): WP_Hook->do_action(Array)
#10 C:\xampp\htdocs\hodlerext\wp-includes\rest-api.php(553): do_action('rest_api_init', Object(WP_REST_Server))
#11 C:\xampp\htdocs\hodlerext\wp-includes\rest-api.php(109): rest_get_server()
#12 C:\xampp\htdocs\hodlerext\wp-content\plugins\test-plugin\test-plugin.php(49): register_rest_route('hpay/v1', '/callback', Array)
#13 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(307): WC_H_Payment_Gateway->init_rest_api_callback('')
#14 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(331): WP_Hook->apply_filters('', Array)
#15 C:\xampp\htdocs\hodlerext\wp-includes\plugin.php(474): WP_Hook->do_action(Array)
#16 C:\xampp\htdocs\hodlerext\wp-content\plugins\test-plugin\test-plugin.php(43): do_action('rest_api_init_c...')
#17 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-wc-payment-gateways.php(97): WC_H_Payment_Gateway->__construct()
#18 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-wc-payment-gateways.php(70): WC_Payment_Gateways->init()
#19 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-wc-payment-gateways.php(43): WC_Payment_Gateways->__construct()
#20 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-woocommerce.php(890): WC_Payment_Gateways::instance()
#21 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-woocommerce.php(163): WooCommerce->payment_gateways()
#22 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-wc-checkout.php(880): WooCommerce->__get('payment_gateway...')
#23 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-wc-checkout.php(1172): WC_Checkout->validate_checkout(Array, Object(WP_Error))
#24 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-wc-ajax.php(461): WC_Checkout->process_checkout()
#25 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(307): WC_AJAX::checkout('')
#26 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(331): WP_Hook->apply_filters('', Array)
#27 C:\xampp\htdocs\hodlerext\wp-includes\plugin.php(474): WP_Hook->do_action(Array)
#28 C:\xampp\htdocs\hodlerext\wp-content\plugins\woocommerce\includes\class-wc-ajax.php(90): do_action('wc_ajax_checkou...')
#29 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(307): WC_AJAX::do_wc_ajax('')
#30 C:\xampp\htdocs\hodlerext\wp-includes\class-wp-hook.php(331): WP_Hook->apply_filters(false, Array)
#31 C:\xampp\htdocs\hodlerext\wp-includes\plugin.php(474): WP_Hook->do_action(Array)
#32 C:\xampp\htdocs\hodlerext\wp-includes\template-loader.php(13): do_action('template_redire...')
#33 C:\xampp\htdocs\hodlerext\wp-blog-header.php(19): require_once('C:\xampp\htdocs...')
#34 C:\xampp\htdocs\hodlerext\index.php(17): require('C:\xampp\htdocs...')
#35 {main}

我做错了什么?

我是 PHP Woocommerce 的新手,我们将不胜感激。

提前致谢

我选择方法一,回调函数命名错误

在__contruct中创建一个id:

$this->id = 'h_payment';

添加回调操作:

add_action( 'woocommerce_api_' . $this->id, array( $this, 'check_h_payment_response' ) );

为处理程序回调创建函数:

public function check_h_payment_response() {//todo}

而 URL 将是

home_url() . '/wc-api/' . $this->id

似乎调用邮递员的结果总是-1,但一切仍然有效