jeudi 12 novembre 2015

Custom Payment gateway not working properly?

I am customize payment gateway and below i am showing my code which did not redirecting to payment gateway link please help me to correct my code. I am also attached my check out page screen shot. it is not redirecting?

`

// Setup our Gateway's id, description and other values
function __construct() {

    // The global ID for this Payment method
    $this->id = "Qpay_in";

    // The Title shown on the top of the Payment Gateways Page next to all the other Payment Gateways
    $this->method_title = __( "Qpay India", 'spyr-authorizenet-aim' );

    // The description for this Payment Gateway, shown on the actual Payment options page on the backend
    $this->method_description = __( "Qpay India Payment Gateway Plug-in for WooCommerce", 'spyr-authorizenet-aim' );

    // The title to be used for the vertical tabs that can be ordered top to bottom
    $this->title = __( "Qpay India", 'spyr-authorizenet-aim' );

    // If you want to show an image next to the gateway's name on the frontend, enter a URL to an image.
    $this->icon = null;

    // Bool. Can be set to true if you want payment fields to show on the checkout 
    // if doing a direct integration, which we are doing in this case
    $this->has_fields = true;

    // Supports the default credit card form
    //$this->supports = array( 'default_credit_card_form' );

    // This basically defines your settings which are then loaded with init_settings()
    $this->init_form_fields();

    // After init_settings() is called, you can get the settings and load them into variables, e.g:
    // $this->title = $this->get_option( 'title' );
    $this->init_settings();

    // Turn these settings into variables we can use
    foreach ( $this->settings as $setting_key => $value ) {
        $this->$setting_key = $value;
    }

    // Lets check for SSL
    add_action( 'admin_notices', array( $this,  'do_ssl_check' ) );

    // Save settings
    if ( is_admin() ) {
        // Versions over 2.0
        // Save our administration options. Since we are not going to be doing anything special
        // we have not defined 'process_admin_options' in this class so the method in the parent
        // class will be used instead
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
    }       
} // End __construct()

// Build the administration fields for this specific Gateway
public function init_form_fields() {
    $this->form_fields = array(
        'enabled' => array(
            'title'     => __( 'Enable / Disable', 'spyr-authorizenet-aim' ),
            'label'     => __( 'Enable this payment gateway', 'spyr-authorizenet-aim' ),
            'type'      => 'checkbox',
            'default'   => 'no',
        ),
        'title' => array(
            'title'     => __( 'Title', 'spyr-authorizenet-aim' ),
            'type'      => 'text',
            'desc_tip'  => __( 'Payment title the customer will see during the checkout process.', 'spyr-authorizenet-aim' ),
            'default'   => __( 'Credit card', 'spyr-authorizenet-aim' ),
        ),
        'description' => array(
            'title'     => __( 'Description', 'spyr-authorizenet-aim' ),
            'type'      => 'textarea',
            'desc_tip'  => __( 'Payment description the customer will see during the checkout process.', 'spyr-authorizenet-aim' ),
            'default'   => __( 'Pay securely using your credit card and debit cards.', 'spyr-authorizenet-aim' ),
            'css'       => 'max-width:350px;'
        ),
        'QPayID' => array(
            'title'     => __( 'Qpay ID', 'spyr-authorizenet-aim' ),
            'type'      => 'password',
            'desc_tip'  => __( 'The ID issued by QPayIndia.', 'spyr-authorizenet-aim' ),
        ),
        'QPayPWD' => array(
            'title'     => __( 'Qpay Password', 'spyr-authorizenet-aim' ),
            'type'      => 'password',
            'desc_tip'  => __( 'The Password issued by QPayIndia.', 'spyr-authorizenet-aim' ),
        ),
        'CaseNumber' => array(
            'title'     => __( 'Case Number', 'spyr-authorizenet-aim' ),
            'type'      => 'text',
            'desc_tip'  => __( 'Case Number issued by QPayIndia for each Currency set-up.', 'spyr-authorizenet-aim' ),
        ),
        'Currency' => array(
            'title'     => __( 'Currency', 'spyr-authorizenet-aim' ),
            'type'      => 'text',
            'desc_tip'  => __( 'ISO Code for the currency of the payment - INR', 'spyr-authorizenet-aim' ),
        ),
        'TransactionType' => array(
            'title'     => __( 'Transaction Type', 'spyr-authorizenet-aim' ),
            'type'      => 'text',

        ),
        'ResponseURL' => array(
            'title'     => __( 'Response URL', 'spyr-authorizenet-aim' ),
            'type'      => 'text',
            'desc_tip'  => __( 'The fully qualified Merchant URL to which the result of transactions will be posted', 'spyr-authorizenet-aim' ),
        ),
        'Mode' => array(
            'title'     => __( 'Qpay Test Mode', 'spyr-authorizenet-aim' ),
            'label'     => __( 'Enable Test Mode', 'spyr-authorizenet-aim' ),
            'type'      => 'checkbox',
            'description' => __( 'Value should be either test or live', 'spyr-authorizenet-aim' ),
            'default'   => 'no',
        )
    );      
}

// Submit payment and handle response
public function process_payment( $order_id ) {
    global $woocommerce;

    // Get this Order's information so that we know
    // who to charge and how much
    $customer_order = new WC_Order( $order_id );

    // Are we testing right now or is it a real transaction
    $environment = ( $this->environment == "yes" ) ? 'TRUE' : 'FALSE';

    // Decide which URL to post to
    $environment_url = ( "FALSE" == $environment ) 
                       ? 'http://ift.tt/1Ld3FhF'
                       : 'http://ift.tt/1Ld3FhF';

    $QPayID = $this->QPayID.'`'.$this->order_total;

    // This is where the fun stuff begins
    $payload = array(
        // Authorize.net Credentials and API Info
        "QPayID"            => $this->QPayID.'`'.$this->order_total,
        "QPayPWD"               => $this->QPayPWD,
        "CaseNumber"                => $this->CaseNumber,

        "Currency"                  => $this->Currency,
        "TransactionType"               => $this->TransactionType,
        "ResponseURL"               => $this->ResponseURL,

        "Mode"                  => $environment,
        "Amount"                => $customer_order->order_total,
        "OrderID"               => $customer_order->get_order_number(),



    );

    // Send this payload to Authorize.net for processing
    $response = wp_remote_post( $environment_url, array(
        'method'    => 'POST',
        'body'      => http_build_query( $payload ),
        'timeout'   => 90,
        'sslverify' => false,
    ) );

    if ( is_wp_error( $response ) ) 
    {
        throw new Exception( __( 'We are currently experiencing problems trying to connect to this payment gateway. Sorry for the inconvenience.', 'spyr-authorizenet-aim' ) );
    } 
    else if ( empty( $response['body'] ) ) 
    {
        throw new Exception( __( 'Authorize.net\'s Response was empty.', 'spyr-authorizenet-aim' ) );
    }


    // Retrieve the body's resopnse if no errors found
    $response_body = wp_remote_retrieve_body( $response );

    // Parse the response into something we can read
    foreach ( preg_split( "/\r?\n/", $response_body ) as $line ) {
        $resp = explode( "|", $line );
    }

    // Get the values we need
    $r['ResponseCode']             = $resp[0];
    $r['Message']         = $resp[1];
    //$r['response_reason_code']      = $resp[2];
    //$r['Message']      = $resp[3];

    // Test the code to know if the transaction went through or not.
    // 1 or 4 means the transaction was a success
    if ( ( $r['ResponseCode'] == 100 )  ) {
        // Payment has been successful
        $customer_order->add_order_note( __( 'Qpay India payment completed.', 'spyr-authorizenet-aim' ) );

        // Mark order as Paid
        $customer_order->payment_complete();

        // Empty the cart (Very important step)
        $woocommerce->cart->empty_cart();

        // Redirect to thank you page
        return array(
            'result'   => 'success',
            'redirect' => $this->get_return_url( $customer_order ),
        );
    } else {
        // Transaction was not succesful
        // Add notice to the cart
        wc_add_notice( $r['Message'], 'error' );
        // Add note to the order for your reference
        $customer_order->add_order_note( 'Error: '. $r['Message'] );
    }

}

// Validate fields
public function validate_fields() {
    return true;
}

This is my checkout page image which did not redirecting to payment gateway page



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire