• marisolfll

    (@marisolfll)


    Hi!

    I make a plugin to integrate a new payment gateway for woocommerce but when it run I obtain the message {“result”:”failure”,”messages”:””,”refresh”:”false”,”reload”:”false”}

    Can you help me?
    Thanks!

    My code is:

    <?php

    /*
    Plugin Name: WooCommerce Aztive Payment Gateway
    Plugin URI:
    Description: Aztive Payment gateway for Woocommerce
    Version: 1.0
    Author: —
    Author URI: —
    */

    add_action(‘plugins_loaded’, ‘woocommerce_aztive_pay_init’, 0);
    function woocommerce_aztive_pay_init(){
    if(!class_exists(‘WC_Payment_Gateway’)) return;

    class WC_Aztive_Pay extends WC_Payment_Gateway{

    private static $onepay = ‘https://azpay.aztive.com&#8217;;
    private static $urlD = ‘/Direct/pay.php’;
    private static $urlM = ‘/Method/pay.php’;
    private static $push = ‘/pushSMS/push.php’;

    private $customerid = NULL;
    private $terminal = NULL;
    private $secret = NULL;

    private $lastOrderId = NULL;

    private $httpAuthUser = NULL;
    private $httpAuthPwd = NULL;

    /**
    * Constructor for the gateway.
    *
    * @access public
    * @return void
    */
    public function __construct(){

    $this->id = ‘aztive’;
    $this->icon = apply_filters(‘woocommerce_aztive_icon’, ”);
    $this->has_fields = false;
    $this->method_title = __( ‘Aztive’, ‘woocommerce’ );

    // Load the settings.
    $this->init_form_fields();
    $this->init_settings();

    // Define user set variables
    $this->customerid = $this->settings[‘customer’];
    $this->terminal = $this->settings[‘terminal’];
    $this->secret = $this->settings[‘secret’];
    $this->title = $this->settings[‘title’];
    $this->description = $this->settings[‘description’];

    // Actions
    add_action( ‘woocommerce_update_options_payment_gateways_’ . $this->id, array( $this, ‘process_admin_options’ ) );

    }

    /**
    * Initialise Gateway Settings Form Fields
    *
    * @access public
    * @return void
    */
    function init_form_fields() {

    $this->form_fields = array(
    ‘enabled’ => array(
    ‘title’ => __( ‘Enable/Disable’, ‘woocommerce’ ),
    ‘type’ => ‘checkbox’,
    ‘label’ => __( ‘Enable Aztive Payment’, ‘woocommerce’ ),
    ‘default’ => ‘yes’
    ),
    ‘title’ => array(
    ‘title’ => __( ‘Title’, ‘woocommerce’ ),
    ‘type’ => ‘text’,
    ‘description’ => __( ‘This controls the title which the user sees during checkout.’, ‘woocommerce’ ),
    ‘default’ => __( ‘Aztive Payment’, ‘woocommerce’ ),
    ‘desc_tip’ => true,
    ),
    ‘description’ => array(
    ‘title’ => __( ‘Customer Message’, ‘woocommerce’ ),
    ‘type’ => ‘textarea’,
    ‘description’ => __( ‘Let the customer know the payee and where they should be sending the cheque to and that their order won\’t be shipping until you receive it.’, ‘woocommerce’ ),
    ‘default’ => __( ‘Please send your cheque to Store Name, Store Street, Store Town, Store State / County, Store Postcode.’, ‘woocommerce’ )
    ),
    ‘customer’ => array(
    ‘title’ => __( ‘Customer’, ‘woocommerce’ ),
    ‘type’ => ‘text’,
    ‘description’ => __( ‘This controls the title which the user sees during checkout.’, ‘woocommerce’ ),
    ‘default’ => __( ‘Aztive Payment’, ‘woocommerce’ ),
    ‘desc_tip’ => true,
    ),
    ‘terminal’ => array(
    ‘title’ => __( ‘Terminal’, ‘woocommerce’ ),
    ‘type’ => ‘text’,
    ‘description’ => __( ‘This controls the title which the user sees during checkout.’, ‘woocommerce’ ),
    ‘default’ => __( ‘Aztive Payment’, ‘woocommerce’ ),
    ‘desc_tip’ => true,
    ),
    ‘secret’ => array(
    ‘title’ => __( ‘Secret’, ‘woocommerce’ ),
    ‘type’ => ‘text’,
    ‘description’ => __( ‘This controls the title which the user sees during checkout.’, ‘woocommerce’ ),
    ‘default’ => __( ‘Aztive Payment’, ‘woocommerce’ ),
    ‘desc_tip’ => true,
    ),
    );

    }

    /**
    * Admin Panel Options
    * – Options for bits like ‘title’ and availability on a country-by-country basis
    *
    * @access public
    * @return void
    */
    public function admin_options() {

    ?>
    <h3><?php _e( ‘Aztive Payment’, ‘woocommerce’ ); ?></h3>
    <p><?php _e( ‘Allows Aztive payments.’, ‘woocommerce’ ); ?></p>
    <table class=”form-table”>
    <?php
    // Generate the HTML For the settings form.
    $this->generate_settings_html();
    ?>
    </table><!–/.form-table–>
    <?php
    }

    /**
    * Process the payment and return the result
    *
    * @access public
    * @param int $order_id
    * @return array
    */
    function process_payment( $order_id ) {
    global $woocommerce;

    $recurrency = NULL;
    $APoptional = NULL;
    $Toptional = NULL;

    $order = new WC_Order( $order_id );

    // make sure the amount does not contain a decimal point
    $amount = $this->normalizeAmount($amount);

    // Let OnePay class generate an orderId
    if ($orderid == ”) $orderid = $order;

    // Setup common params
    $params = array(‘orderid’ => $order_id,
    ‘amount’ => $order -> order_total,
    ‘customerid’ => $this->customerid,
    ‘terminal’ => $this->terminal);

    // add id
    if ($id != ”) $params[‘id’] = $id;

    // add recurrency parameters
    if ($recurrency != NULL) {
    $params[‘recurring’] = $recurrency[‘recurring’];
    if (isset($recurrency[‘rDescription’])) $params[‘rDescription’] = $recurrency[‘rDescription’];
    if (isset($recurrency[‘rPeriod’])) $params[‘rPeriod’] = $recurrency[‘rPeriod’];
    if (isset($recurrency[‘rFrequency’])) $params[‘rFrequency’] = $recurrency[‘rFrequency’];
    if (isset($recurrency[‘rCycles’])) $params[‘rCycles’] = $recurrency[‘rCycles’];
    if (isset($recurrency[‘rAmount’])) {
    $params[‘rAmount’] = $recurrency[‘rAmount’];
    $params[‘rCurrency’] = isset($recurrency[‘rCurrency’]) ? $recurrency[‘rCurrency’] : ‘EUR’;
    }
    }

    // add tokens parameters
    if ($Toptional != NULL) {
    if (isset($Toptional[‘username’])) $params[‘username’] = $Toptional[‘username’];
    if (isset($Toptional[‘ratio’])) $params[‘ratio’] = $Toptional[‘ratio’];
    }

    // compute the signature
    $params[‘signature’] = $this->signature($params);

    // add the optional parameters
    if ($APoptional != NULL) {
    if (isset($APoptional[‘currency’])) $params[‘currency’] = $APoptional[‘currency’];
    if (isset($APoptional[‘lang’])) $params[‘lang’] = $APoptional[‘lang’];
    if (isset($APoptional[‘name’])) $params[‘name’] = $APoptional[‘name’];
    if (isset($APoptional[‘product_name’])) $params[‘product_name’] = $APoptional[‘product_name’];
    }

    if ($id == ”)
    return $this->genURL(WC_Aztive_Pay::$urlD, $params);
    return $this->genURL(WC_Aztive_Pay::$urlM, $params);

    // Mark as on-hold (we’re awaiting the payment)
    $order->update_status(‘on-hold’, __( ‘Awaiting BACS payment’, ‘woocommerce’ ));

    // Reduce stock levels
    $order->reduce_order_stock();

    // Remove cart
    $woocommerce->cart->empty_cart();

    // Return thankyou redirect
    return array(
    ‘result’ => ‘success’,
    ‘redirect’ => add_query_arg(‘key’, $order->order_key, add_query_arg(‘order’, $order->id, get_permalink(woocommerce_get_page_id(‘thanks’))))
    );
    }

    /***********************************************************************************************/
    /** MISCELLANEOUS ******************************************************************************/
    /***********************************************************************************************/

    /**
    * Sets HTTP authentication for all AzPay to merchant responses.
    *
    * @param string $user HTTP authentication username
    * @param string $pwd HTTP authentication password
    *
    * @deprecated
    */
    public function setHttpAuthentication($user, $pwd)
    {
    $this->httpAuthUser = $user;
    $this->httpAuthPwd = $pwd;
    }

    /**
    * Returns whether the signature in the response corresponds
    * with the signature computed locally.
    *
    * @param array $response The whole response data. Tipically $_GET.
    *
    * @return boolean True if the signatures are equal. False otherwise.
    */
    public function validateResponseData($response)
    {
    $params = array(
    ‘order’ => (isset($response[‘onepay_customer_order’]) ? $response[‘onepay_customer_order’] : ”),
    ‘code’ => (isset($response[‘onepay_customer_code’]) ? $response[‘onepay_customer_code’] : ”),
    ‘terminal’ => (isset($response[‘onepay_customer_terminal’]) ? $response[‘onepay_customer_terminal’] : ”),
    ‘auth_code’ => (isset($response[‘onepay_authorization_code’]) ? $response[‘onepay_authorization_code’] : ”),
    ‘response’ => (isset($response[‘onepay_response’]) ? $response[‘onepay_response’] : ”),
    ‘card_no’ => (isset($response[‘onepay_card_number’]) ? $response[‘onepay_card_number’] : ”)
    );

    // local signature
    $local = $this->signature($params);

    return ($local === $response[‘onepay_signature’]);
    }

    /**
    * Method to get the last used order id
    *
    * @return string Last used order id or NULL if no transaction has been made.
    */
    public function getLastOrderId()
    {
    return $this->lastOrderId;
    }

    /***********************************************************************************************/
    /** Push SMS ***********************************************************************************/
    /***********************************************************************************************/

    /**
    * Performs a request to send a sms with the provided message to the provided number.
    *
    * @param $msg string Message to send.
    * @param string|int $destination Message destination (number with country prefix).
    * @param string $sender Sender of the message.
    *

      *

    • If it is not set, or set to “”, then the default value stored in Aztive is used.
    • *

    * @param bool $trim Whether to limit the message to fit 1 sms or not.
    *

      *

    • “true”: The message will be trimmed to fit 1 sms length, that is 160 characters.
    • *

    • “false”: The message will be sent as it is.
    • *

    * @return array
    */
    public function pushSMS($msg, $destination, $sender = “”, $trim = true)
    {
    $url = WC_Aztive_Pay::$onepay . WC_Aztive_Pay::$push;
    $params = array(
    “customerid” => $this->customerid,
    “terminal” => $this->terminal,
    “text” => $msg,
    “destino” => $destination,
    “sender” => $sender,
    “concat” => $trim ? 0 : 1,
    );

    $params[‘signature’] = $this->signature($params);

    $query = $url . ‘?’ . http_build_query($params);
    $ch = curl_init($query);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $ret = curl_exec($ch);

    curl_close($ch);

    return json_decode($ret, true);
    }

    /**
    * Request to send mutliple sms as specified in $request
    *
    * @param array $request
    * array format:
    * [0:n] => array(
    * [0] => message
    * [1] => destination tel numbers (international format, eg: 34699999999) separated by coma
    * )
    * @param string $sender Sender of the messages.
    *

      *

    • If it is not set, or set to “”, then the default value stored in Aztive is used.
    • *

    * @param bool $trim Whether to limit the message to fit 1 sms or not.
    *

      *

    • “true”: The message will be trimmed to fit 1 sms length, that is 160 characters.
    • *

    • “false”: The message will be sent as it is.
    • *

    *
    * @return array
    */
    public function pushSMS_multi($request, $sender = “”, $trim = true)
    {
    $parsed = array();
    $parsed[0] = count($request);

    // join both arrays
    $parsed = array_merge($parsed, $request);

    // append sender and trim info
    $parsed[‘sender’] = $sender;
    $parsed[‘concat’] = $trim ? 0 : 1;

    $url = WC_Aztive_Pay::$onepay . WC_Aztive_Pay::$push;
    $params = array(
    “customerid” => $this->customerid,
    “terminal” => $this->terminal,
    “params” => json_encode($parsed),
    );
    $params[‘signature’] = $this->signature($params);

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_POST, count($params));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $ret = curl_exec($ch);

    curl_close($ch);

    return json_decode($ret, true);
    }

    /**
    * Requests to send multiple sms as specified in the provided CSV file.
    *
    * Accepted format is as follows:
    * [sender, sender_name]
    * [trim, {true|false}]
    * msg, message1
    * dst, tel1, .., teln
    * msg, message2
    * dst, tel1, .., teln
    *
    * @param $filepath string Path to CSV file to read and parse.
    * @return bool|array An associative array with the received response or false if the file does not exist or cannot be opened.
    */
    public function pushSMS_CSV($filepath)
    {
    $ret = false;
    if (file_exists($filepath)) {
    if (($handler = fopen($filepath, “r”)) !== FALSE) {
    $sender = “”;
    $trim = true;
    $parsed = array();
    $parsed[0] = 0;
    $i = 1;

    while (($data = fgetcsv($handler, “,”)) !== FALSE) {
    if ($data[0] == “sender”) {
    $sender = isset($data[1]) ? $data[1] : “”;
    }
    else if ($data[0] == “trim”) {
    $trim = isset($data[1]) ? ($data[1] == true) : false;
    }
    else if ($data[0] == “msg”) {
    $msg = isset($data[1]) ? $data[1] : “”;
    if ($trim) $msg = substr($msg, 0, 160);
    $parsed[$i] = array(
    $msg,
    “”
    );
    $parsed[0]++;
    }
    else {
    unset($data[0]);
    $parsed[$i][1] = implode(‘,’, $data);
    $i++;
    }
    }

    // add sender and trim
    $parsed[‘sender’] = $sender;
    $parsed[‘concat’] = $trim ? 0 : 1;

    $url = WC_Aztive_Pay::$onepay . WC_Aztive_Pay::$push;
    $params = array(
    “customerid” => $this->customerid,
    “terminal” => $this->terminal,
    “params” => json_encode($parsed),
    );
    $params[‘signature’] = $this->signature($params);

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_POST, count($params));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $ret = curl_exec($ch);

    curl_close($ch);
    }
    else
    return false;
    }
    else
    return false;

    return json_decode($ret, true);
    }

    /***********************************************************************************************/
    /** PRIVATE METHODS ****************************************************************************/
    /***********************************************************************************************/

    /**
    * Computes the signature with the given parameters.
    *
    * @param array $params Parameters to use to compute the signature. Note that the order in which the parameters are into the array is important.
    *
    * @return string The computed signature.
    */
    private function signature($params)
    {
    //error_log(“[onepayclass] implode: ” . implode(”, $params) . $this->secret);
    return (sha1(implode(”, $params) . $this->secret));
    }

    /**
    * Computes a new unique order id.
    *
    * @return string A new unique order id.
    */
    private function genUniqueOrderId()
    {
    $orderid = number_format(microtime(true), 4);
    $orderid = str_replace(‘.’, ”, $orderid);
    $orderid = str_replace(‘,’, ”, $orderid);
    $orderid = substr($orderid, 0, 12); // Unix time in seconds and 2 second decimals
    return $orderid;
    }

    /**
    * Builts the url based on the parameters.
    *
    * @param string
    *
    * @param array
    *
    * @return string The built url.
    */
    private function genURL($type, $params)
    {
    return (WC_Aztive_Pay::$onepay . $type . ‘?’ . http_build_query($params));
    }

    /**
    * Normalizes the provided amount depending on the format it is received.
    *
    * @param mixed $amount
    *
    * @return string Numeric string with the amount representation to send to onepay.
    */
    private function normalizeAmount($amount)
    {
    $amount = (float)$amount;
    $amount = $amount * 100;

    return (int)$amount;
    }
    }

    /**
    * Add the Gateway to WooCommerce
    **/
    function woocommerce_add_aztive_pay_gateway($methods) {
    $methods[] = ‘WC_Aztive_Pay’;
    return $methods;
    }

    add_filter(‘woocommerce_payment_gateways’, ‘woocommerce_add_aztive_pay_gateway’ );
    }

  • The topic ‘New plugin to gateway in Woocommerce’ is closed to new replies.