Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
chesio committed Jul 29, 2019
0 parents commit 08f3e18
Show file tree
Hide file tree
Showing 54 changed files with 9,750 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# WooCommerce mPAY24 Gateway

A WordPress plugin that integrates [mPAY24](https://www.mpay24.com/) payment gateway into [WooCommerce](https://woocommerce.com/).

## Requirements

* [PHP](https://www.php.net/) 7.1 or newer
* [WordPress](https://wordpress.org/) 4.9 or newer
* [WooCommerce](https://woocommerce.com/) 3.4 or newer

## Compatibility

Plugin is based on [mPAY24 PHP SDK](https://github.com/mpay24/mpay24-php) in version 4.3.2.
28 changes: 28 additions & 0 deletions assets/images/mpay24.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Register autoloader for classes shipped with the plugin.
*
* @package BC_WooCommerce_mPAY24_Gateway
*/

// Register autoload function
spl_autoload_register(function (string $class) {
// Only autoload classes shipped with the plugin.
if (strpos($class, 'BlueChip\\WooCommerce\\Mpay24Gateway') !== 0) {
return;
}

// Get absolute name of class file
$file = __DIR__ . '/classes/' . str_replace('\\', '/', $class) . '.php';

// If the class file is readable, load it!
if (is_readable($file)) {
require_once $file;
}
});
55 changes: 55 additions & 0 deletions bc-woocommerce-mpay24-gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Plugin Name: WooCommerce mPAY24 Gateway
* Plugin URI: https://github.com/chesio/bc-woocommerce-mpay24-gateway
* Description: Integrate mPAY24 payment gateway into WooCommerce
* Version: 1.0.0
* Author: Česlav Przywara <[email protected]>
* Author URI: https://www.chesio.com
* Requires PHP: 7.1
* Requires WP: 4.9
* Tested up to: 5.2
* Text Domain: bc-woocommerce-mpay24-gateway
* GitHub Plugin URI: https://github.com/chesio/bc-woocommerce-mpay24-gateway
*/

if (version_compare(PHP_VERSION, '7.1', '<')) {
// Warn user that his/her PHP version is too low for this plugin to function.
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p>';
echo esc_html(
sprintf(
__('WooCommerce mPAY24 Gateway plugin requires PHP 7.1 to function properly, but you have version %s installed. The plugin has been auto-deactivated.', 'bc-woocommerce-mpay24-gateway'),
PHP_VERSION
)
);
echo '</p></div>';
// https://make.wordpress.org/plugins/2015/06/05/policy-on-php-versions/
if (isset($_GET['activate'])) {
unset($_GET['activate']);
}
}, 10, 0);

// Self deactivate.
add_action('admin_init', function () {
deactivate_plugins(plugin_basename(__FILE__));
}, 10, 0);

// Bail.
return;
}

// Load constants.
require_once __DIR__ . '/constants.php';

// Register autoloader for this plugin.
require_once __DIR__ . '/autoload.php';

// Bootstrap mPAY24 PHP SDK (= effectively register autoloader for the SDK).
require_once __DIR__ . '/includes/mpay24-php/bootstrap.php';

// Construct plugin instance.
$bc_woocommerce_mpay24_gateway = new \BlueChip\WooCommerce\Mpay24Gateway\Plugin();

// Load the plugin.
$bc_woocommerce_mpay24_gateway->load();
195 changes: 195 additions & 0 deletions classes/BlueChip/WooCommerce/Mpay24Gateway/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace BlueChip\WooCommerce\Mpay24Gateway;

/**
* @link https://docs.woocommerce.com/document/payment-gateway-api/
*/
class Gateway extends \WC_Payment_Gateway
{
/**
* @var string Name of test mode.
*/
const TEST_MODE = 'test';

/**
* @var string Name of production mode.
*/
const PRODUCTION_MODE = 'production';


/**
* @var \BlueChip\WooCommerce\MPay24Gateway\Mpay24
*/
private $mpay24;


public function __construct()
{
// Set fields required by WooCommerce Gateway API.
$this->id = 'bc-woocommerce-mpay24-gateway';
$this->icon = apply_filters('bc-woocommerce-mpay24-gateway/filter:icon', plugins_url('assets/images/mpay24.svg', PLUGIN_FILE));
$this->has_fields = false;
$this->method_title = __('mPAY24 Gateway', 'bc-woocommerce-mpay24-gateway');
$this->method_description = __('Online payment via mPAY24 payment page', 'bc-woocommerce-mpay24-gateway');

// Load the form fields.
$this->init_form_fields();

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

// Load WooCommerce settings.
$this->description = $this->get_option('description');
$this->title = $this->get_option('title');

// Load mPAY24 settings.
$mpay24_test_mode = $this->get_option('mpay24_mode') !== self::PRODUCTION_MODE;
$mpay24_user = $mpay24_test_mode
? $this->get_option('mpay24_test_user', '')
: $this->get_option('mpay24_production_user', '')
;
$mpay24_password = $mpay24_test_mode
? $this->get_option('mpay24_test_password', '')
: $this->get_option('mpay24_production_password', '')
;
$mpay24_debug = $this->get_option('mpay24_debug', 'no') === 'yes';

// Initialize mPAY24 gateway integration.
$this->mpay24 = new Mpay24($mpay24_user, $mpay24_password, $mpay24_test_mode, $mpay24_debug);

// Set hook that saves gateway settings on change.
add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
}


/**
* @return bool True if the gateway still needs to be set up, false otherwise.
*/
public function needs_setup() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
return !$this->mpay24->isSetupOk();
}


/**
* Initialize form fields.
*/
public function init_form_fields() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
$this->form_fields = [
'woocommerce_settings' => [
'title' => __('Woocommerce Settings', 'bc-woocommerce-mpay24-gateway'),
'type' => 'title',
'description' => '',
],
'enabled' => [
'title' => __('Enable/Disable', 'bc-woocommerce-mpay24-gateway'),
'label' => __('Enable mPAY24', 'bc-woocommerce-mpay24-gateway'),
'type' => 'checkbox',
'description' => '',
'default' => 'no',
],
'title' => [
'title' => __('Title', 'bc-woocommerce-mpay24-gateway'),
'type' => 'text',
'description' => __('The title which user sees during checkout.', 'bc-woocommerce-mpay24-gateway'),
'desc_tip' => true,
'default' => __('Credit card', 'bc-woocommerce-mpay24-gateway'),
],
'description' => [
'title' => __('Description', 'bc-woocommerce-mpay24-gateway'),
'type' => 'textarea',
'description' => __('The description which user sees during checkout.', 'bc-woocommerce-mpay24-gateway'),
'desc_tip' => true,
'default' => __('Pay with your credit card via mPAY24.', 'bc-woocommerce-mpay24-gateway'),
],
'mpay24_settings' => [
'title' => __('mPAY24 Settings', 'bc-woocommerce-mpay24-gateway'),
'type' => 'title',
'description' => '',
],
'mpay24_mode' => [
'title' => __('Mode', 'bc-woocommerce-mpay24-gateway'),
'type' => 'select',
'description' => __('Mode determines against which mPAY24 system (test or productive) payments are made.', 'bc-woocommerce-mpay24-gateway'),
'default' => self::TEST_MODE,
'desc_tip' => true,
'options' => [
self::TEST_MODE => __('Test', 'bc-woocommerce-mpay24-gateway'),
self::PRODUCTION_MODE => __('Production', 'bc-woocommerce-mpay24-gateway'),
],
],
'mpay24_test_user' => [
'title' => __('Test username', 'bc-woocommerce-mpay24-gateway'),
'type' => 'text',
'description' => __('SOAP API username (without leading u) for test mode.', 'bc-woocommerce-mpay24-gateway'),
'desc_tip' => true,
'default' => '',
],
'mpay24_test_password' => [
'title' => __('Test password', 'bc-woocommerce-mpay24-gateway'),
'type' => 'password',
'description' => __('SOAP API password for test mode.', 'bc-woocommerce-mpay24-gateway'),
'desc_tip' => true,
'default' => '',
],
'mpay24_production_user' => [
'title' => __('Production username', 'bc-woocommerce-mpay24-gateway'),
'type' => 'text',
'description' => __('SOAP API username (without leading u) for production mode.', 'bc-woocommerce-mpay24-gateway'),
'desc_tip' => true,
'default' => '',
],
'mpay24_production_password' => [
'title' => __('Production password', 'bc-woocommerce-mpay24-gateway'),
'type' => 'password',
'description' => __('SOAP API password for production mode.', 'bc-woocommerce-mpay24-gateway'),
'desc_tip' => true,
'default' => '',
],
'mpay24_debug' => [
'title' => __('Debugging', 'bc-woocommerce-mpay24-gateway'),
'label' => __('Enable debug logging', 'bc-woocommerce-mpay24-gateway'),
'type' => 'checkbox',
'description' => sprintf(__('Log mPAY24 events inside WC_LOG_DIR: <code>%s</code>', 'bc-woocommerce-mpay24-gateway'), WC_LOG_DIR),
'default' => 'no',
],
];
}


/**
* Show extra notice before gateway description when test mode is enabled.
*/
public function payment_fields() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if ($this->mpay24->isTestModeEnabled()) {
echo '<p>' . \esc_html__('TEST MODE ENABLED', 'bc-woocommerce-mpay24-gateway') . '</p>';
}
parent::payment_fields();
}


/**
* Tell WooCommerce where to redirect the user when he/she decides to use the gateway.
*
* @param int $order_id
* @return array
*/
public function process_payment($order_id) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
$order = \wc_get_order($order_id);

if (!($order instanceof \WC_Order)) {
return [];
}

$payment_page_location = $this->mpay24->getPaymentPageLocation($order, $this->get_return_url($order));

return [
'result' => 'success',
'redirect' => $payment_page_location,
];
}
}
Loading

0 comments on commit 08f3e18

Please sign in to comment.