php - Get WooCommerce custom payment gateway values in process_payment function for Blocks checkout - Stack Overflow

时间: 2025-01-06 admin 业界

I am creating custom payment plugin for woocommerce. I am facing issue while getting the checkout form data in process_payment function of woocommerce payment class.

I am using following JS code to make payment gateway compatible with woocommerce Block checkout.

const PaySettings= window.wc.wcSettings.allSettings.paymentMethodData.pay;

const Paylabel = window.wp.htmlEntities.decodeEntities( PaySettings.title );

const PayMethod = ({ id, label,value }) => {

    return React.createElement('div', {className: 'pay-methods'},
        React.createElement('label', {htmlFor: id}, label),
        React.createElement('input', {type: 'radio', name: id, value: value, onChange: (e) => e.target.value}),
    );
};

const Content = () => {
    
    return React.createElement('div', {className: 'pay-methods-cont'}, null,
        React.createElement('img', {src: PaySettings.icon}),
        React.createElement('p', null, window.wp.htmlEntities.decodeEntities(PaySettings.description ||  '')),
        React.createElement(PayMethod, {id: 'pay_method', label: 'JazzCash',value: 'jazzcash' }),
        React.createElement(PayMethod, {id: 'pay_method', label: 'EasyPaisa',value: 'easypaisa' })
    );

}
const Icon = () => {
    return React.createElement('div', {className: 'custom-input-field'},
        React.createElement('img', {src: PaySettings.icon}))
}


const AP_Gateway = {
    name: 'pay',
    label: Paylabel,
    icon: Icon,
    content: Object(window.wp.element.createElement)(Content, null),
    edit: Object(window.wp.element.createElement)(Content, null),
    canMakePayment: () => true,
    ariaLabel: Paylabel,
    supports: {
        features: ['products'],//settings.supports,
    },
};

window.wc.wcBlocksRegistry.registerPaymentMethod( AP_Gateway )

It is showing radio options on checkout as payment gateway options but when I select any of the option I am not able to receive value in process_payment function.
public function process_payment( $order_id ) {
            
   echo "<pre>";print_r($_POST);echo "</pre>";die;
            
}


I am doing something wrong or missing something ? Please help.