• Resolved bonini81

    (@bonini81)


    Hi, well i am back on this cool forum! Got a question, i am builing a custom payment gateway plugin for WP. But the callback i receive from the payment gateway in terms of wether the payment was processed correctly is via Javascript not PHP TOKEN or IPN call back like most payment gateways do, so it is via this JS function or once the payment is processed correctly this function response is called:

    var onAuthorize = function(response) {

    //Here i can make my custom script to trigger something like redirect to a thank you page cause order was processed correctly but in WordPress i need to trigger a PHP function from the Woocommerce API to process the payment: public function process_payment( $order_id ) So i would to call that function inside this JS function in order to process and close the order.

    };

    Thefore i need to trigger this PHP function (from the woocomerce API to process the payment, more info here https://docs.woocommerce.com/document/payment-gateway-api/) to process the payment but i would need to call the function, mentioned below this comment. inside the previous JS function:

    
    public function process_payment( $order_id ) {
    
    		global $woocommerce;		
    		$customer_order = new WC_Order( $order_id );
    		// Payment successful
    $customer_order->add_order_note( __( 'Xchange complete payment.', 'cwoa-authorizenet-aim' ) 
    );
    			// paid order marked
    			$customer_order->payment_complete();
    
    			// this is important part for empty cart
    			$woocommerce->cart->empty_cart();
    
    			// Redirect to thank you page
    			return array(
    				'result'   => 'success',
    				'redirect' => $this->get_return_url( $customer_order ),
    			);
    

    So the question is can you call a PHP function inside a JS / Ajax function, better to call it on the same file in which i am handling the payment processing, cause sending the checkout process to another page or refreshing the page i guess will loose cart details information which happened to me via GET, via POST will have to double check.

    Anyone had to deal with this problem before, calling a PHP function inside JS? Anyone know if there is JS version of the default Woocommerce API process payment PHP function: public function process_payment( $order_id )

    Would love the help. Thanx

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Stuart Duff – a11n

    (@stuartduff)

    Automattic Happiness Engineer

    Hey @bonini81,

    I saw you haven’t received an answer to this as yet here on the forums as it’s quite a technical question, maybe asking in the Advanced WooCommerce Facebook Group, along with the WooCommerce Slack Community.

    It’s very possible other people have asked the same question before, and someone will be able to help you out there.

    Thread Starter bonini81

    (@bonini81)

    Hi @stuartduff, well found the answer, you may use a hack with AJAX to skip that Woocommerce API function to process the order or
    public function process_payment( $order_id ), you can use this script for example. Explanation in brackets:

    
    jQuery.ajax({
    				url: 'your-plugin-folder-name/?wc-ajax=checkout', (use that wc-ajax=checkout url to send via post the data of the Checkout form fields and then woocommerce will put the order as complete)
    				type: "POST",
    				data: jQuery(".woocommerce-checkout").serialize(),
    				success: function(respuesta) {
    					console.log(respuesta);
    					parent.location.href = respuesta.redirect; 
    				},
    				error: function() {
    			        console.log("Error, credit card not valid, for example");
    			    }
    			}); 

    Well of course this Ajax script will have to shoot once there is a response from the payment processor, in my case was OnAuthorize that processed the order inside this JS function or:

    var onAuthorize = function(response) {
    //Custom Ajax function to process Payment via WC-AJAX
    jQuery.ajax({

    url: ‘your-plugin-folder-name/?wc-ajax=checkout’,
    HERE Would go the rest of the script shown above…

    Well that is it if you ever find yourself to process the order not via PHP but via JS, which might happen at times.
    So, happy to sharre the knowledge, cause not much info around.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to Call the Process_Payment($order_id ) inside a JS function’ is closed to new replies.