AnswerBun.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!

Related Questions

CREATE TABLE with dbDelta does not create table

1  Asked on October 30, 2021 by fcd

     

how to add shortcode widget at end of main blog page

1  Asked on October 30, 2021 by shubhamk

 

Placing background image over section divider

1  Asked on October 30, 2021 by user191925

   

jQuery Click event is not fired at all

1  Asked on October 30, 2021 by islam-hanafi

   

Make the product page as homepage WooCommerce

0  Asked on October 30, 2021 by gabriel-borges

     

How to be sure to get last page version without ANY cache?

2  Asked on October 30, 2021 by j-bizmai

   

How to grab posts in Gutenberg Block?

1  Asked on October 30, 2021 by stephen-sabatini

 

Custom post type permalink returns bad url

1  Asked on October 30, 2021 by ncti

     

Menus don’t appear in admin area / WordPress Multisite

0  Asked on October 30, 2021 by user191642

   

Client side HTTP parameter pollution (reflected)

0  Asked on October 30, 2021 by divided

 

WP Ajax Function Always Returning undefined

1  Asked on October 30, 2021 by brian-bruman

     

Group Posts by Category in Monthly Archive

2  Asked on October 30, 2021 by zero-goki

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir