user2870431
user2870431

Reputation: 47

woocommerce: How to install the Custom Payment Gateway

I have created a class for new payment gateway. I just duplicated the existing payment class & changed the class name, file name and also incorporated the install code.

But I couldn't find the way to install a new payment gateway in admin panel. Please advice.

Wordpress Ver: 3.5.1 Class:

<?php
//if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * braintreedemo Payment Gateway
 *
 * Provides a braintreedemo Payment Gateway, mainly for testing purposes.
 *
 * @class       WC_Gateway_braintreedemo
 * @extends     WC_Payment_Gateway
 * @version     2.1.0
 * @package     WooCommerce/Classes/Payment
 * @author      WooThemes
 */

 add_action('plugins_loaded', 'woocommerce_braintree_init', 0);

 function woocommerce_braintree_init() {
if(!class_exists('WC_Payment_Gateway')) return;

class WC_Gateway_Braintree extends WC_Payment_Gateway {

    /**
     * Constructor for the gateway.
     */
    public function __construct() {
        $this->id                 = 'braintree';
        $this->icon               = apply_filters('woocommerce_cheque_icon', '');
        $this->has_fields         = false;
        $this->method_title       = __( 'Craintreedemo', 'woocommerce' );
        $this->method_description = __( 'Allows braintree payments. Why would you take braintree in this day and age? Well you probably wouldn\'t but it does allow you to make test purchases for testing order emails and the \'success\' pages etc.', 'woocommerce' );

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

        // Define user set variables
        $this->title        = $this->get_option( 'title' );
        $this->description  = $this->get_option( 'description' );
        $this->instructions = $this->get_option( 'instructions', $this->description );

        // Actions
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
        add_action( 'woocommerce_thankyou_braintree', array( $this, 'thankyou_page' ) );

        // Customer Emails
        add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 2 );
    }

    /**
     * Initialise Gateway Settings Form Fields
     */
    public function init_form_fields() {

        $this->form_fields = array(
            'enabled' => array(
                'title'   => __( 'Enable/Disable', 'woocommerce' ),
                'type'    => 'checkbox',
                'label'   => __( 'Enable braintree Payment', 'woocommerce' ),
                'default' => 'yes'
            ),
            'title' => array(
                'title'       => __( 'Title', 'woocommerce' ),
                'type'        => 'text',
                'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                'default'     => __( 'braintree Payment', 'woocommerce' ),
                'desc_tip'    => true,
            ),
            'description' => array(
                'title'       => __( 'Description', 'woocommerce' ),
                'type'        => 'textarea',
                'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
                'default'     => __( 'Please send your braintree to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', 'woocommerce' ),
                'desc_tip'    => true,
            ),
            'instructions' => array(
                'title'       => __( 'Instructions', 'woocommerce' ),
                'type'        => 'textarea',
                'description' => __( 'Instructions that will be added to the thank you page and emails.', 'woocommerce' ),
                'default'     => '',
                'desc_tip'    => true,
            ),
        );
    }

    /**
     * Output for the order received page.
     */
    public function thankyou_page() {
        if ( $this->instructions )
            echo wpautop( wptexturize( $this->instructions ) );
    }

    /**
     * Add content to the WC emails.
     *
     * @access public
     * @param WC_Order $order
     * @param bool $sent_to_admin
     */
    public function email_instructions( $order, $sent_to_admin ) {
        if ( $sent_to_admin || $order->status !== 'on-hold' || $order->payment_method !== 'braintree' )
            return;

        if ( $this->instructions )
            echo wpautop( wptexturize( $this->instructions ) );
    }

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

        $order = new WC_Order( $order_id );

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

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

        // Remove cart
        WC()->cart->empty_cart();

        // Return thankyou redirect
        return array(
            'result'    => 'success',
            'redirect'  => $this->get_return_url( $order )
        );
    }
}

/**
 * Add the Gateway to WooCommerce
 **/
function woocommerce_add_braintree_gateway($methods) {
    $methods[] = 'WC_Braintree';
    return $methods;
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_braintree_gateway' );
}

Upvotes: 2

Views: 11103

Answers (3)

hamish
hamish

Reputation: 1182

step1. create this folder: /wp-content/plugins/woocommerce-payment-gateway-braintreedemo/

step2. put your code into this file woocommerce-payment-gateway-braintreedemo.php and place it in that folder from step1.

step3. make sure this is at the top of your php file

<?php
/*
Plugin Name: WooCommerce braintreedemo Payment Gateway
Plugin URI: http://www.braintreedemo.com
Description: braintreedemo Payment gateway for woocommerce
Version: 1.0
Author: Your Name
Author URI: http://www.mywebsite.com
*/

Step 4. goto the wordpress wp-admin plugins page and scroll down to your plugin and press activate.

Step 5. any errors will be displayed at the top of the page. delete the php file to restore your site if there are really bad syntax errors.

Upvotes: 2

Anthony Essel
Anthony Essel

Reputation: 21

download this plugin: https://wordpress.org/plugins/woocommerce-custom-payment-gateways/

Look at the code and anywhere you see 'custom_payment_gateway' put the name of your gateway.

The above code should guide you, i have used it to create a custom gateway.

Upvotes: 1

Federico Giust
Federico Giust

Reputation: 1793

You need to install the same way you install a WordPress plugin.

  • Zip the folder that contains the plugin
  • Go to your wordpress admin panel
  • Go to Plugins - Add New
  • Click on upload
  • Upload your plugin and activate

If your plugin is correct, then when you go to WooCommerce settings you should see the option to enable your gateway.

Upvotes: 0

Related Questions