• Resolved Andi Lee Davis

    (@andi-lee-davis)


    Hi there,

    working on a custom gateway, where the /checkout url is used to process the payment as well. This is made via an I-Frame, I have extended the payment gateway fine and calls my overriding function process_payment()

    The requirement on the checkout… (review-order.php)
    1) on checkout confirm details
    2) process payment – order id is created and the page refreshed – we get to this stage
    3) $session = new WC_Session_Handler(); is called and checks if(isset($session->order_awaiting_payment)) to show the i-frame – this never happens because the session gets expunged during the processing stage.

    It also kills the cart and the page is left blank, even though this is also commented out on the extended process. Debugging is on and there is no reason it should exit

    My function for the gateway override:

    public function process_payment( $order_id ) {
    
    			global $woocommerce;
    
    			#$order = wc_create_order();
    			///
    			//// - temp ou $order = new WC_Order( $order_id );
    			$order = wc_get_order( $order_id );
    
    			// Mark as on-hold (we're awaiting the cheque)
    			$order->update_status('on-hold', __( 'Awaiting Confirmation of Funds', 'woocommerce' ));
    
    			// Reduce stock levels
    			///$order->reduce_order_stock();
    
    			// Remove cart
    			///$woocommerce->cart->empty_cart();
    
    			// Return thankyou redirect - override
    			return array(
    				'refresh' => true,
    				'reload' => false,
    				'result' => 'success',
    				'redirect' =>  $this->get_return_url($order),
    				'mn' => 'return_order'
    			);
    			//
    		}
    
    As you can see the empty_cart is commented out but it still empties the cart.
    I have put the same question on Stack Overflow since October.
    
    in process_checkout() function

    $result = $available_gateways[ $this->posted[‘payment_method’] ]->process_payment( $order_id );

    After this order is created in the back-end but the session and the cart is dead.

    Some things spring to mind.
    Nonce is failing it and the cart throws an exception and kills it in the default function wc_clear_cart_after_payment()

    there is some-kind of abstraction going on.

    Right now I have been battling away at this for months. It seems simple but it is not.

    Can anyone please help, I would be very grateful.

    Thanks

    Andi

    https://www.ads-software.com/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Andi Lee Davis

    (@andi-lee-davis)

    Ok one step closer

    this function… runs and checks if an order is received or not.
    in includes/wc-cart-functions.php
    wc_clear_cart_after_payment()

    not sure…

    Thread Starter Andi Lee Davis

    (@andi-lee-davis)

    Ok figured this one out.

    There is a header action that clears the cart. We needed to add some more statuses to the array so that it ignores it.

    1st remove the action and then add your own method.

    function st_wc_clear_cart_after_payment( $methods ) {
        global $wp, $woocommerce;
    
        if ( ! empty( $wp->query_vars['order-received'] ) ) {
    
            $order_id = absint( $wp->query_vars['order-received'] );
    
            if ( isset( $_GET['key'] ) )
                $order_key = $_GET['key'];
            else
                $order_key = '';
    
            if ( $order_id > 0 ) {
                $order = wc_get_order( $order_id );
    
                if ( $order->order_key == $order_key ) {
                   WC()->cart->empty_cart();
                }
            }
    
        }
    
        if ( WC()->session->order_awaiting_payment > 0 ) {
    
            $order = wc_get_order( WC()->session->order_awaiting_payment );
    
            if ( $order->id > 0 ) {
                // If the order has not failed, or is not pending, the order must have gone through
                if ( ! $order->has_status( array( 'failed', 'pending','pending-st-cleared-funds','on-hold' ) ) ) { ///// <- add your custom status here....
                    WC()->cart->empty_cart();
                }
             }
        }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom gateway process_payment(), still kills order when its empty_cart() remov’ is closed to new replies.