diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c4aef23 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +/.gitattributes export-ignore +/.gitignore export-ignore +/phpcs.xml export-ignore \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/README.md b/README.md index 15c0cca..3134d54 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# WooCommerce mPAY24 Gateway +# BC WooCommerce mPAY24 Gateway A WordPress plugin that integrates [mPAY24](https://www.mpay24.com/) payment gateway into [WooCommerce](https://woocommerce.com/). @@ -10,4 +10,8 @@ A WordPress plugin that integrates [mPAY24](https://www.mpay24.com/) payment gat ## Compatibility -Plugin is based on [mPAY24 PHP SDK](https://github.com/mpay24/mpay24-php) in version 4.3.2. \ No newline at end of file +Plugin is based on [mPAY24 PHP SDK](https://github.com/mpay24/mpay24-php) in version 4.3.2. + +## Limitations + +mPAY24 supports integration with a lot of payment providers (like PayPal, Klarna etc.), but the plugin currently only supports credit card payments. \ No newline at end of file diff --git a/bc-woocommerce-mpay24-gateway.php b/bc-woocommerce-mpay24-gateway.php index 8f630e7..8edcaa6 100644 --- a/bc-woocommerce-mpay24-gateway.php +++ b/bc-woocommerce-mpay24-gateway.php @@ -1,9 +1,9 @@ * Author URI: https://www.chesio.com * Requires PHP: 7.1 @@ -48,8 +48,9 @@ // 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(); +add_action('plugins_loaded', function () { + // Construct plugin instance. + $bc_woocommerce_mpay24_gateway = new \BlueChip\WooCommerce\Mpay24Gateway\Plugin(); + // Load the plugin. + $bc_woocommerce_mpay24_gateway->load(); +}, 10, 0); diff --git a/classes/BlueChip/WooCommerce/Mpay24Gateway/Gateway.php b/classes/BlueChip/WooCommerce/Mpay24Gateway/Gateway.php index 8f1815f..c726555 100644 --- a/classes/BlueChip/WooCommerce/Mpay24Gateway/Gateway.php +++ b/classes/BlueChip/WooCommerce/Mpay24Gateway/Gateway.php @@ -19,7 +19,7 @@ class Gateway extends \WC_Payment_Gateway /** - * @var \BlueChip\WooCommerce\MPay24Gateway\Mpay24 + * @var \BlueChip\WooCommerce\Mpay24Gateway\Mpay24 */ private $mpay24; diff --git a/classes/BlueChip/WooCommerce/Mpay24Gateway/IPN.php b/classes/BlueChip/WooCommerce/Mpay24Gateway/IPN.php index 43c18c4..e64e565 100644 --- a/classes/BlueChip/WooCommerce/Mpay24Gateway/IPN.php +++ b/classes/BlueChip/WooCommerce/Mpay24Gateway/IPN.php @@ -114,6 +114,7 @@ protected static function generateTransactionSecret(\WC_Order $order): string */ public static function processConfirmationRequest() { + /** @var array */ $request_data = wp_unslash($_GET); $status = $request_data['STATUS'] ?? ''; @@ -142,7 +143,7 @@ public static function processConfirmationRequest() /** * @param array $data Request data. - * @return null|WC_Order Order instance if confirmation request is valid, null otherwise. + * @return null|\WC_Order Order instance if confirmation request is valid, null otherwise. */ protected static function validateConfirmationRequest(array $data): ?\WC_Order { @@ -211,7 +212,7 @@ protected static function validateConfirmationRequest(array $data): ?\WC_Order * @link https://docs.mpay24.com/docs/transaction-states All transaction states * @link https://docs.mpay24.com/docs/payment-notification#section-notification-values Request data items * - * @param \WC_Order $order_id + * @param \WC_Order $order * @param string $status * @param string $tid * @param string $mpay_tid diff --git a/classes/BlueChip/WooCommerce/Mpay24Gateway/IpTools.php b/classes/BlueChip/WooCommerce/Mpay24Gateway/IpTools.php index 6979118..687c868 100644 --- a/classes/BlueChip/WooCommerce/Mpay24Gateway/IpTools.php +++ b/classes/BlueChip/WooCommerce/Mpay24Gateway/IpTools.php @@ -18,7 +18,7 @@ public static function isIpInSubnet(string $ip, string $subnet): bool [$net, $mask] = explode('/', $subnet); $_net = ip2long($net); - $_mask = ~((1 << (32 - $mask)) - 1); + $_mask = ~((1 << (32 - intval($mask))) - 1); $_ip = ip2long($ip); diff --git a/classes/BlueChip/WooCommerce/Mpay24Gateway/Mpay24.php b/classes/BlueChip/WooCommerce/Mpay24Gateway/Mpay24.php index c3cfc75..0785aab 100644 --- a/classes/BlueChip/WooCommerce/Mpay24Gateway/Mpay24.php +++ b/classes/BlueChip/WooCommerce/Mpay24Gateway/Mpay24.php @@ -110,9 +110,27 @@ protected function buildOrder(\WC_Order $order, string $return_url): \Mpay24\Mpa $mdxi->Order->Price = $order->get_total(); $mdxi->Order->Currency = $order->get_currency(); - // Add customer data. + // Add customer name. $mdxi->Order->Customer = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); + // Add billing address. + $mdxi->Order->BillingAddr->setMode("ReadOnly"); + $mdxi->Order->BillingAddr->Name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); + $mdxi->Order->BillingAddr->Street = $order->get_billing_address_1(); + if ($order->get_billing_address_2()) { + // Set only if provided, because mPAY24 payment page features an empty field otherwise... + $mdxi->Order->BillingAddr->Street2 = $order->get_billing_address_2(); + } + $mdxi->Order->BillingAddr->Zip = $order->get_billing_postcode(); + $mdxi->Order->BillingAddr->City = $order->get_billing_city(); + if ($order->get_billing_state()) { + // Set only if provided, because mPAY24 payment page features an empty field otherwise... + $mdxi->Order->BillingAddr->State = $order->get_billing_state(); + } + $mdxi->Order->BillingAddr->Country->setCode($order->get_billing_country()); + $mdxi->Order->BillingAddr->Email = $order->get_billing_email(); + $mdxi->Order->BillingAddr->Phone = $order->get_billing_phone(); + // Note: Redirect to "thank you" page even in case of error, because the order-received endpoint is not a mere // "Thank you" page. If order status is "failed", it will display proper message and offer an option to repeat // the payment - I believe this is much better UX than canceling the order right away. diff --git a/classes/BlueChip/WooCommerce/Mpay24Gateway/Plugin.php b/classes/BlueChip/WooCommerce/Mpay24Gateway/Plugin.php index aa90f7e..de565e3 100644 --- a/classes/BlueChip/WooCommerce/Mpay24Gateway/Plugin.php +++ b/classes/BlueChip/WooCommerce/Mpay24Gateway/Plugin.php @@ -7,24 +7,13 @@ */ class Plugin { - /** - * Load the plugin by hooking into WordPress actions and filters. - * Method should be invoked immediately on plugin load. - */ - public function load() - { - // Register initialization method. - add_action('plugins_loaded', [$this, 'init'], 10, 0); - } - - /** * Perform initialization tasks. * Method should be run in `plugins_loaded` hook. * * @action https://developer.wordpress.org/reference/hooks/plugins_loaded/ */ - public function init() + public function load() { if (class_exists('WC_Payment_Gateway')) { add_filter('woocommerce_payment_gateways', [$this, 'registerPaymentMethod'], 10, 1);