0

I am trying to create a custom payment gateway.

This error shows when I place the order: SyntaxError: Unexpected token < in JSON at position 0

I suspect the error is in the variable $response.

Any idea what I might be doing wrong?

<?php
/*
Plugin Name: EasyPay Gateway
Version: 1.0
Author: Klevis Miho
*/

add_action( 'plugins_loaded', 'init_easypay_gateway_class' );

function init_easypay_gateway_class() {
   class WC_Easypay_Gateway extends WC_Payment_Gateway {


        function __construct() {

            $this->id = "woocommerce_easypay";
            $this->method_title = "Easypay"; // Title in admin
            $this->method_description = "This adds the EasyPay button";
            $this->has_fields = false;
            $this->icon = null;

            // Text that shows on cart
            $this->title = 'Pay with EasyPay';
            $this->description = 'You need to be a EasyPay customer to pay';

            $this->init_form_fields();
            $this->init_settings();


            // Save settings
            if ( is_admin() ) {
                add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
            }

        }


        // Admin fields 
        public function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                    'title'     => 'Enable / Disable',
                    'label'     => 'Enable EasyPay button',
                    'type'      => 'checkbox',
                    'default'   => 'no',
                ),
            );      
        }


        // Response handled for payment gateway
        public function process_payment( $order_id ) {
            global $woocommerce;

            $customer_order = new WC_Order( $order_id );

            //print_r($customer_order);


            $orderId = time();
            $value = "200";
            $code = "2e93e56a5efe4eeab942445c9f5a451b";
            $button_id = 61;
            $response = startEasypay($orderId, $value, $code, $button_id);

            return array(
               'result' => 'success',
               'redirect' => 'http://google.com'
            );

        }


        /* Function from EasyPay */
        public function startEasypay($orderId, $value, $code, $button_id ) {
            $url = "http://iscs-al.info/pay-sandbox/start.php";
            $data = "button_id=$button_id&order_id=$orderId&value=$value&code=$code";

            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                'Content-length: '.strlen($data),
                                'Referer: ' . $_SERVER['SERVER_NAME']
                ));
            $r = curl_exec($ch);
            curl_close($ch);    
            return json_decode($r);
        }

   }

}


function add_easypay_gateway_class( $methods ) {
    $methods[] = 'WC_Easypay_Gateway'; 
    return $methods;
}

add_filter( 'woocommerce_payment_gateways', 'add_easypay_gateway_class' );
  • you may need to explicitly set curl_setopt($ch, CURLOPT_POST, 1); also if posting data. then debug by outputting $r response to a file and checking it, as you are probably getting HTML back not JSON if the first character is <. – majick Mar 6 '18 at 12:21
  • If I put this code in another php file (not Worpress related) this works fine and redirects me to the payment gateway. It has something to do with Woocommerce. – Klevis Miho Mar 6 '18 at 14:05
  • I don't understand why you are not checking the actual contents of the response, which is where you need to focus on debugging $r. This code returns payment success regardless of the response. – majick Mar 7 '18 at 6:50
  • This is the response from $r: {"success":true,"transId":"4dbe700a-dc03-4485-8d27-c1461747456f","url":"http:\/\/iscs-al.info\/pay-sandbox\/checkout.php?transId=4dbe700a-dc03-4485-8d27-c1461747456f"} – Klevis Miho Mar 7 '18 at 16:35
  • try return json_decode($r, true); to return those values as an array instead of an object. do if ($response['success']) {$customer_order->payment_complete(); etc and then you can simply return $response instead of the success array. you may want to add to your question where you are seeing the syntax error, as there is no < in the response maybe this is not where the error is coming from after all. – majick Mar 7 '18 at 23:14
0

I managed to solve it by myself.

I had missed a $this variable.

The correct way should be $response = $this->startEasypay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.