TransWikia.com

Custom payment gateway issue

WordPress Development Asked by Klevis Miho on November 26, 2020

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' );

One Answer

I managed to solve it by myself.

I had missed a $this variable.

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

Answered by Klevis Miho on November 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP