| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Cashfree |
| 4 |
* Version: 4.8.1 |
| 5 |
* Plugin URI: https://github.com/cashfree/cashfree-woocommerce |
| 6 |
* Description: Payment gateway plugin by Cashfree Payments for Woocommerce sites. |
| 7 |
* Author: devcashfree |
| 8 |
* Author URI: https://cashfree.com |
| 9 |
* Developer: Cashfree Dev |
| 10 |
* Developer URI: techsupport@gocashfree.com |
| 11 |
* Text Domain: woocommerce-extension |
| 12 |
* Domain Path: /languages |
| 13 |
* Requires at least: 4.4 |
| 14 |
* Tested up to: 7.1 |
| 15 |
* WC requires at least: 3.0 |
| 16 |
* WC tested up to: 11.0 |
| 17 |
* |
| 18 |
* |
| 19 |
* License: GPLv3 |
| 20 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 21 |
*/ |
| 22 |
|
| 23 |
defined( 'ABSPATH' ) || exit; |
| 24 |
|
| 25 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 26 |
|
| 27 |
// Adding this for making compatible with HPOC |
| 28 |
add_action('before_woocommerce_init', function() { |
| 29 |
if (class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class)) |
| 30 |
{ |
| 31 |
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true); |
| 32 |
} |
| 33 |
}); |
| 34 |
|
| 35 |
/** |
| 36 |
* Cashfree main class. |
| 37 |
*/ |
| 38 |
class WC_Cashfree { |
| 39 |
|
| 40 |
/** |
| 41 |
* Payment gateway id. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
const PAYMENT_GATEWAY_ID = 'cashfree'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Payment gateway settings. |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
private $settings; |
| 53 |
|
| 54 |
private $enabled; |
| 55 |
|
| 56 |
/** |
| 57 |
* A log object returned by wc_get_logger(). |
| 58 |
* |
| 59 |
* @var WC_Logger |
| 60 |
*/ |
| 61 |
public static $log; |
| 62 |
|
| 63 |
/** |
| 64 |
* Constructor. |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
$plugin_data = get_file_data( __FILE__, array( 'Version' => 'Version' ), false ); |
| 68 |
|
| 69 |
define( 'WC_CASHFREE_FILE', __FILE__ ); |
| 70 |
define( 'WC_CASHFREE_DIR_PATH', plugin_dir_path( __FILE__ ) ); |
| 71 |
define( 'WC_CASHFREE_DIR_URL', plugin_dir_url( __FILE__ ) ); |
| 72 |
define( 'WC_CASHFREE_VERSION', $plugin_data['Version'] ); |
| 73 |
|
| 74 |
$this->settings = get_option( 'woocommerce_' . self::PAYMENT_GATEWAY_ID . '_settings' ); |
| 75 |
if($this->settings) { |
| 76 |
$this->enabled = 'yes' === $this->settings['enabled']; |
| 77 |
} |
| 78 |
|
| 79 |
require_once WC_CASHFREE_DIR_PATH . 'includes/wc-cashfree-functions.php'; |
| 80 |
require_once WC_CASHFREE_DIR_PATH . 'includes/http/class-wc-cashfree-adapter.php'; |
| 81 |
|
| 82 |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( __CLASS__, 'plugin_action_links' ) ); |
| 83 |
add_filter( 'woocommerce_payment_gateways', array( __CLASS__, 'load_gateways' ) ); |
| 84 |
add_filter( 'woocommerce_before_add_to_cart_form' , array( $this, 'wp_cashfree_offers' ) ); |
| 85 |
|
| 86 |
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) ); |
| 87 |
add_action( 'woocommerce_blocks_loaded', array( __CLASS__, 'woocommerce_gateway_cashfree_woocommerce_block_support' ) ); |
| 88 |
} |
| 89 |
|
| 90 |
public function wp_cashfree_offers() { |
| 91 |
if ( isset($this->settings['enabledOffers']) && |
| 92 |
$this->settings['enabledOffers'] === 'yes' && |
| 93 |
isset($this->settings['sandbox']) && |
| 94 |
$this->settings['sandbox'] === 'no') { |
| 95 |
// External Scripts |
| 96 |
wp_register_script('cf-woocommerce-js', 'https://sdk.cashfree.com/js/widget/1.0.1/cashfree-widget.prod.js', null, null, true ); |
| 97 |
wp_enqueue_script('cf-woocommerce-js'); |
| 98 |
|
| 99 |
add_filter( 'cf-woocommerce_enqueue_styles', '__return_false' ); |
| 100 |
|
| 101 |
global $product; |
| 102 |
$price = $product->get_price(); |
| 103 |
|
| 104 |
echo'<div id="cashfree-widget" data-amount='.$price.' data-appId='.$this->settings['app_id'].' data-isOffers='.$this->settings['offers'].' data-isPayLater='.$this->settings['payLater'].' data-isEmi='.$this->settings['emi'].'></div>'; |
| 105 |
|
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Add Cashfree payment gateway. |
| 111 |
* |
| 112 |
* @param array $methods List of payment methods. |
| 113 |
* |
| 114 |
* @return array |
| 115 |
*/ |
| 116 |
public static function load_gateways( $methods ) { |
| 117 |
if ( ! class_exists( 'WC_Payment_Gateway' ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
include_once WC_CASHFREE_DIR_PATH . 'includes/gateways/class-wc-cashfree-gateway.php'; |
| 122 |
include_once WC_CASHFREE_DIR_PATH . 'includes/gateways/class-wc-cashfree-payments.php'; |
| 123 |
|
| 124 |
array_push( $methods, 'WC_Cashfree_Payments' ); |
| 125 |
return $methods; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Show action links on the plugin screen. |
| 130 |
* |
| 131 |
* @param mixed $links Plugin Action links. |
| 132 |
* |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
public static function plugin_action_links( $links ) { |
| 136 |
$action_links = array( |
| 137 |
'settings' => '<a href="' . admin_url( |
| 138 |
'admin.php?page=wc-settings&tab=checkout§ion=' . self::PAYMENT_GATEWAY_ID |
| 139 |
) . '">' . __( 'Settings', 'cashfree' ) . '</a>', |
| 140 |
); |
| 141 |
|
| 142 |
return array_merge( $action_links, $links ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Register/queue frontend scripts. |
| 147 |
*/ |
| 148 |
public function load_scripts() { |
| 149 |
if ( $this->enabled && ! is_checkout() ) { |
| 150 |
wc_cashfree_js( $this->settings ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Logging method. |
| 156 |
* |
| 157 |
* @param string $message Log message. |
| 158 |
* @param string $level Optional. Default 'info'. |
| 159 |
*/ |
| 160 |
public static function log( $message, $level = 'info' ) { |
| 161 |
if ( ! isset( self::$log ) ) { |
| 162 |
self::$log = wc_get_logger(); |
| 163 |
} |
| 164 |
|
| 165 |
self::$log->log( $level, $message, array( 'source' => self::PAYMENT_GATEWAY_ID ) ); |
| 166 |
} |
| 167 |
|
| 168 |
public static function woocommerce_gateway_cashfree_woocommerce_block_support() { |
| 169 |
if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { |
| 170 |
require_once WC_CASHFREE_DIR_PATH . 'includes/gateways/class-wc-cashfree-block-support.php'; |
| 171 |
add_action( |
| 172 |
'woocommerce_blocks_payment_method_type_registration', |
| 173 |
function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) { |
| 174 |
$container = Automattic\WooCommerce\Blocks\Package::container(); |
| 175 |
// registers as shared instance. |
| 176 |
$container->register( |
| 177 |
WC_Cashfree_Blocks_Support::class, |
| 178 |
function() { |
| 179 |
return new WC_Cashfree_Blocks_Support(); |
| 180 |
} |
| 181 |
); |
| 182 |
$payment_method_registry->register( |
| 183 |
$container->get( WC_Cashfree_Blocks_Support::class ) |
| 184 |
); |
| 185 |
}, |
| 186 |
5 |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
new WC_Cashfree(); |