0
votes

I am developing a payment gateway plugin (For Cybersource Payment) for WooCommerce. I developed it to almost at the end, but one thing is intercepting me to finish and that is the part after redirecting from payment gateway website after successful payment back to my wordpress page (Purchase Confirmation Page - New Wordpress Page created using template in wp-content/themes/my-theme/order-confirm-template.php). But i dont know how to handle the response coming back from payment website. I looked for some woocommerce hooks but nothing worked. I found there is a hook woocommerce_thankyou but that is also not working for me.

Can somebody help me out here in these two points below

1) How to handle response and placed the order properly and remove the items from the cart when coming back after successful payment from gateway website.

2) which page should i redirect back from gateway website? back to the same checkout page or some custom page just like i did.

Any Help with code will be very appreciated. Thanks.

1

1 Answers

2
votes

I have used the following code in my plugin, I hope this works for you too :)

First add this code,

function receipt_page($order){
    echo $this -> ResponceHandler($order);
}

Now the code for ResponceHandler($order) function,

 public function ResponceHandler($order_id){




            if(!isset($_POST['ResponseCode'])){

            global $woocommerce;
        echo '<p>'.__('Thank you for your order, please click the button below to pay with XYZ', 'woocommerce').'</p>';
            $order = new WC_Order($order_id);

            $order_id = $order_id.'_'.date("ymds");



            $post_data = get_post_meta($order_id,'_post_data',true);

            update_post_meta($order_id,'_post_data',array());
###Your Form Code HERE###
   echo '<form><input value="Proceed To Payment" type="submit" /> </form>'; 
}
###Haandle the response###
 if(isset($_POST['ResponseCode']))
            {
             if($_POST['ResponseCode']==0){
                                    global $woocommerce;
                                    session_start();
                                    $_SESSION['post']=$_POST;

                                    $order = new WC_Order($order_id);

                                    $order_id = $order_id.'_'.date("ymds");



                                    $post_data = get_post_meta($order_id,'_post_data',true);

                                    update_post_meta($order_id,'_post_data',array());
                                                if($order->status != 'processing'){

                                                $order ->status ='Processing';
                                                $order->payment_complete();

                                                $order -> add_order_note('XYZ Payment Gateway <br>Response message :'.$_POST['ResponseMessage'].'<br>Payment ID :'.$_POST['PaymentID'].'<br>Merchant Reference Number :'.$_POST['MerchantRefNo'].'<br>Transaction ID :'.$_POST['TransactionID'].'');

                                                add_post_meta( $order->id, '_paymentid', sanitize_text_field( $_POST['PaymentID'] ) );
                                                add_post_meta( $order->id, '_trno', sanitize_text_field( $_POST['TransactionID'] ) );
                                                $woocommerce -> cart -> empty_cart();
                                                wp_redirect( $order->get_checkout_order_received_url());

                                            }
                                }
            else {

                                                if($order->status != 'failed'){
                                                $order ->status ='failed';
                                echo "Payment failed!<br><br><br>Possible Error : ".$_POST['ResponseMessage']."<br>PaymentID: ".$_POST['PaymentID']."<br><br><br>We request you to save these details for further reference. <br>You can always pay for this order by clicking on your name in the top right corner and visiting your orders section."; }
                }

            }





}