├── LICENSE ├── readme.txt ├── alphagateway.php ├── README.md └── alphagateway_class.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Antreasgr 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Plugin Name === 2 | Contributors: antreasgribas 3 | Tags: Alpha Bank, payment, gateway, woocommerce 4 | Requires at least: 3.0.1 5 | Tested up to: 3.4 6 | Stable tag: 4.3 7 | License: GPLv2 or later 8 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 9 | 10 | HA payment gateway plugin for the WooCommerce platform to receive payments 11 | via the Alpha Bank(Greece) online payment system. 12 | 13 | == Description == 14 | 15 | A payment gateway plugin for the WooCommerce platform to receive payments 16 | via the Alpha Bank(Greece) online payment system. 17 | 18 | == Installation == 19 | 20 | This section describes how to install the plugin and get it working. 21 | 22 | 1. Upload the plugin files to the `/wp-content/plugins/plugin-name` directory, or install the plugin through the WordPress plugins screen directly. 23 | 1. Activate the plugin through the 'Plugins' screen in WordPress 24 | 1. Use the Settings->Plugin Name screen to configure the plugin 25 | 1. Under WooCommerce->Settings->Checkout click the Alpha Bank Gateway 26 | 1. Configure your Merchant Id and Secret 27 | 28 | == Changelog == 29 | 30 | = 1.0 = 31 | * Inital Commit 32 | 33 | 34 | -------------------------------------------------------------------------------- /alphagateway.php: -------------------------------------------------------------------------------- 1 | 34 | * 35 | 36 | ## License 37 | 38 | ### MIT Open Source License 39 | 40 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 41 | 42 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 43 | 44 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /alphagateway_class.php: -------------------------------------------------------------------------------- 1 | plugin_url() . '/includes/gateways/paypal/assets/images/paypal.png' ); 26 | $this->id = 'alpha'; 27 | $this->icon = apply_filters( 'woocommerce_cod_icon', '' ); 28 | $this->method_title = __( 'Alpha Bank', 'woocommerce' ); 29 | $this->method_description = __( 'Alpha bank web payment system.', 'woocommerce' ); 30 | $this->has_fields = false; 31 | 32 | // Load the settings 33 | $this->init_form_fields(); 34 | $this->init_settings(); 35 | 36 | // Get settings 37 | $this->title = $this->get_option( 'title' ); 38 | $this->description = $this->get_option( 'description' ); 39 | $this->instructions = $this->get_option( 'instructions', $this->description ); 40 | $this->MerchantId = $this->get_option('MerchantId'); 41 | $this->Secret = $this->get_option('Secret'); 42 | 43 | $this->AlphaBankUrl = $this->get_option('testmode') === 'yes' ? "https://alpha.test.modirum.com/vpos/shophandlermpi" : "https://www.alphaecommerce.gr/vpos/shophandlermpi"; 44 | 45 | $this->InstallmentsActive = $this->get_option('installmentsActive') === 'yes' ? true : false; 46 | 47 | $this->autosubmitPaymentForm = $this->get_option('autosubmitPaymentForm') === 'yes' ? true : false; 48 | 49 | // Customer Emails 50 | add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 ); 51 | 52 | //Actions 53 | add_action('woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' )); 54 | add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); 55 | add_action('woocommerce_thankyou_alpha', array( $this, 'thankyou_page' ) ); 56 | // Payment listener/API hook 57 | add_action('woocommerce_api_wc_gateway_alpha', array($this, 'check_response')); 58 | 59 | // Set the installments array 60 | $this->installmentsArray = Array(100 => 4, 200 => 8, 300 => 12); 61 | } 62 | 63 | /** 64 | * Check if this gateway is enabled. 65 | * 66 | * @return bool 67 | */ 68 | public function is_available() { 69 | if ( 'yes' !== $this->enabled ) { 70 | return false; 71 | } 72 | 73 | if ( ! $this->MerchantId || ! $this->Secret ) { 74 | return false; 75 | } 76 | 77 | return true; 78 | } 79 | 80 | /** 81 | * Initialise Gateway Settings Form Fields. 82 | */ 83 | public function init_form_fields() { 84 | $shipping_methods = array(); 85 | 86 | if ( is_admin() ) 87 | foreach ( WC()->shipping()->load_shipping_methods() as $method ) { 88 | $shipping_methods[ $method->id ] = $method->get_title(); 89 | } 90 | 91 | $this->form_fields = array( 92 | 'enabled' => array( 93 | 'title' => __( 'Enable Alpha Bank', 'woocommerce' ), 94 | 'label' => __( 'Enabled', 'woocommerce' ), 95 | 'type' => 'checkbox', 96 | 'description' => '', 97 | 'default' => 'no' 98 | ), 99 | 'title' => array( 100 | 'title' => __( 'Title', 'woocommerce' ), 101 | 'type' => 'text', 102 | 'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ), 103 | 'default' => __( 'Alpha Bank', 'woocommerce' ), 104 | 'desc_tip' => true, 105 | ), 106 | 'description' => array( 107 | 'title' => __( 'Description', 'woocommerce' ), 108 | 'type' => 'textarea', 109 | 'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ), 110 | 'default' => __( 'Πληρωμή μέσω Alpha Bank', 'woocommerce' ), 111 | 'desc_tip' => true, 112 | ), 113 | 'instructions' => array( 114 | 'title' => __( 'Instructions', 'woocommerce' ), 115 | 'type' => 'textarea', 116 | 'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ), 117 | 'default' => __( 'Πληρωμή μέσω Alpha Bank', 'woocommerce' ), 118 | 'desc_tip' => true, 119 | ), 120 | 'testmode' => array( 121 | 'title' => __( 'Test mode', 'woocommerce' ), 122 | 'label' => __( 'Enable test mode', 'woocommerce' ), 123 | 'type' => 'checkbox', 124 | 'description' => 'uncheck this to disable test mode', 125 | 'default' => 'yes' 126 | ), 127 | 'MerchantId' => array( 128 | 'title' => __('Alpha Bank Merchant ID', 'woocommerce'), 129 | 'type' => 'text', 130 | 'description' => __('Enter Your Alpha Bank Merchant ID', 'woocommerce'), 131 | 'default' => '', 132 | 'desc_tip' => true 133 | ), 134 | 'Secret' => array( 135 | 'title' => __('Alpha Bank Secret Code', 'woocommerce'), 136 | 'type' => 'text', 137 | 'description' => __('Enter Your Alpha Bank Secret Code', 'woocommerce'), 138 | 'default' => '', 139 | 'desc_tip' => true 140 | ), 141 | 'installmentsActive' => array( 142 | 'title' => __('Enable installments?', 'woocommerce'), 143 | 'type' => 'checkbox', 144 | 'description' => __('Check this to enable installments', 'woocommerce'), 145 | 'default' => 'no' 146 | ), 147 | 'autosubmitPaymentForm' => array( 148 | 'title' => __( 'Auto-submit payment form', 'woocommerce' ), 149 | 'label' => __( 'Enable', 'woocommerce' ), 150 | 'type' => 'checkbox', 151 | 'description' => 'If you check this, buyers will be re-directed to the payment gateway automatically. ', 152 | 'default' => 'no' 153 | ) 154 | ); 155 | } 156 | 157 | 158 | protected function get_alpha_args( $order, $uniqid, $installments ) { 159 | // WC_Gateway_Paypal::log( 'Generating payment form for order ' . $order->get_order_number() . '. Notify URL: ' . $this->notify_url ); 160 | $return = WC()->api_request_url( 'WC_Gateway_Alpha' ); 161 | $address = array( 162 | 'address_1' => ( WC()->version >= '3.0.0' ) ? $order->get_billing_address_1() : $order->billing_address_1, 163 | 'address_2' => ( WC()->version >= '3.0.0' ) ? $order->get_billing_address_2() : $order->billing_address_2, 164 | 'city' => ( WC()->version >= '3.0.0' ) ? $order->get_billing_city() : $order->billing_city, 165 | 'state' => ( WC()->version >= '3.0.0' ) ? $order->get_billing_state() : $order->billing_state, 166 | 'postcode' => ( WC()->version >= '3.0.0' ) ? $order->get_billing_postcode() : $order->billing_postcode, 167 | 'country' => ( WC()->version >= '3.0.0' ) ? $order->get_billing_country() : $order->billing_country 168 | ); 169 | 170 | $lang = 'en'; 171 | if (substr(get_locale(), 0, 2) == 'el') { 172 | $lang = 'el'; 173 | } 174 | 175 | $args = array( 176 | 'mid' => $this->MerchantId, 177 | 'lang' => $lang, 178 | 'orderid' => $uniqid . 'AlphaBankOrder' . ( ( WC()->version >= '3.0.0' ) ? $order->get_id() : $order->id ), 179 | 'orderDesc' => 'Name: ' . $order->get_formatted_billing_full_name() . ' Address: ' . implode(",", $address) , 180 | 'orderAmount' => wc_format_decimal($order->get_total(), 2, false), 181 | 'currency' => 'EUR', 182 | 'payerEmail' => ( WC()->version >= '3.0.0' ) ? $order->get_billing_email() : $order->billing_email, 183 | 'billCountry' => $address['country'], 184 | 'billState' => $address['state'], 185 | 'billZip' => $address['postcode'], 186 | 'billCity' => $address['city'], 187 | 'billAddress' => $address['address_1'] 188 | ); 189 | 190 | if ($installments > 0) { 191 | $args['extInstallmentoffset'] = 0; 192 | $args['extInstallmentperiod'] = $installments; 193 | }; 194 | 195 | $args = array_merge($args, array( 196 | 'confirmUrl' => add_query_arg( 'confirm', ( WC()->version >= '3.0.0' ) ? $order->get_id() : $order->id , $return), 197 | 'cancelUrl' => add_query_arg( 'cancel', ( WC()->version >= '3.0.0' ) ? $order->get_id() : $order->id , $return), 198 | )); 199 | 200 | return apply_filters( 'woocommerce_alpha_args', $args , $order ); 201 | } 202 | 203 | /** 204 | * Output for the order received page. 205 | * */ 206 | public function receipt_page($order_id) { 207 | echo '

' . __('Thank you - your order is now pending payment. Please click the button below to proceed.', 'woocommerce') . '

'; 208 | $order = wc_get_order( $order_id ); 209 | $uniqid = uniqid(); 210 | 211 | $form_data = $this->get_alpha_args($order, $uniqid, 0); 212 | $digest = base64_encode(sha1(implode("", array_merge($form_data, array('secret' => $this->Secret))), true)); 213 | 214 | $html_form_fields = array(); 215 | foreach ($form_data as $key => $value) { 216 | $html_form_fields[] = ''; 217 | } 218 | 219 | ?> 220 | 221 | autosubmitPaymentForm ) :?> 222 | 223 | 236 |
237 | 240 | 241 | 242 | InstallmentsActive) { 244 | $this->installments(wc_format_decimal($order->get_total(), 2, false), $uniqid, $order); 245 | } 246 | ?> 247 | 248 | 249 | 250 | 251 |
252 | update_status( 'pending', __( 'Sent request to Alpha bank with orderID: ' . $form_data['orderid'] , 'woocommerce' ) ); 256 | } 257 | 258 | /** 259 | * Process the payment and return the result. 260 | * 261 | * @param int $order_id 262 | * @return array 263 | */ 264 | public function process_payment( $order_id ) { 265 | $order = wc_get_order( $order_id ); 266 | 267 | return array( 268 | 'result' => 'success', 269 | 'redirect' => $order->get_checkout_payment_url( true ) // $this->get_return_url( $order ) 270 | ); 271 | } 272 | 273 | /** 274 | * Verify a successful Payment! 275 | * */ 276 | public function check_response() { 277 | $required_response = array( 278 | 'mid' => '', 279 | 'orderid' => '', 280 | 'status' => '', 281 | 'orderAmount' => '', 282 | 'currency' => '', 283 | 'paymentTotal' => '' 284 | ); 285 | 286 | $notrequired_response = array( 287 | 'message' => '', 288 | 'riskScore' => '', 289 | 'payMethod' => '', 290 | 'txId' => '', 291 | 'sequence' => '', 292 | 'seqTxId' => '', 293 | 'paymentRef' => '' 294 | ); 295 | 296 | if (!isset($_REQUEST['digest'])){ 297 | wp_die( 'Alpha Bank Request Failure', 'Alpha Bank Gateway', array( 'response' => 500 ) ); 298 | } 299 | 300 | foreach ($required_response as $key => $value) { 301 | if (isset($_REQUEST[$key])){ 302 | $required_response[$key] = $_REQUEST[$key]; 303 | } 304 | else{ 305 | // required parameter not set 306 | wp_die( 'Alpha Bank Request Failure', 'Alpha Bank Gateway', array( 'response' => 500 ) ); 307 | } 308 | } 309 | 310 | foreach ($notrequired_response as $key => $value) { 311 | if (isset($_REQUEST[$key])){ 312 | $required_response[$key] = $_REQUEST[$key]; 313 | } 314 | else{ 315 | } 316 | } 317 | 318 | $string_form_data = array_merge($required_response, array('secret' => $this->Secret)); 319 | $digest = base64_encode(sha1(implode("", $string_form_data), true)); 320 | 321 | if ($digest != $_REQUEST['digest']){ 322 | wp_die( 'Alpha Bank Digest Error', 'Alpha Bank Gateway', array( 'response' => 500 ) ); 323 | } 324 | 325 | if(isset($_REQUEST['cancel'])){ 326 | $order = wc_get_order(wc_clean($_REQUEST['cancel'])); 327 | if (isset($order)){ 328 | $order->add_order_note('Alpha Bank Payment ' . $required_response['status'] . '. txId: ' . $required_response['txId'] . '. ' . $required_response['message'] ); 329 | wp_redirect( $order->get_cancel_order_url_raw()); 330 | exit(); 331 | } 332 | } 333 | else if (isset($_REQUEST['confirm'])){ 334 | $order = wc_get_order(wc_clean($_REQUEST['confirm'])); 335 | if (isset($order)){ 336 | if ($required_response['orderAmount'] == wc_format_decimal($order->get_total(), 2, false)){ 337 | $order->add_order_note('Alpha Bank Payment ' . $required_response['status'] . '. txId: ' . $required_response['txId'] . '. payMethod: ' . $required_response['payMethod']. '. paymentRef: ' . $required_response['paymentRef'] . '. ' . $required_response['message'] ); 338 | $order->payment_complete('Alpha Bank Payment ' . $required_response['status'] . '. txId: ' . $required_response['txId'] ); 339 | wp_redirect($this->get_return_url( $order )); 340 | exit(); 341 | } 342 | else{ 343 | $order->add_order_note('Payment received with incorrect amount. Alpha Bank Payment ' . $required_response['status'] . '. '. $required_response['message'] ); 344 | } 345 | } 346 | } 347 | 348 | // something went wrong so die 349 | wp_die( 'Unspecified Error', 'Payment Gateway error', array( 'response' => 500 ) ); 350 | } 351 | 352 | /** 353 | * Output for the order received page. 354 | */ 355 | public function thankyou_page() { 356 | if ( $this->instructions ) { 357 | echo wpautop( wptexturize( $this->instructions ) ); 358 | } 359 | } 360 | 361 | /** 362 | * Add content to the WC emails. 363 | * 364 | * @access public 365 | * @param WC_Order $order 366 | * @param bool $sent_to_admin 367 | * @param bool $plain_text 368 | */ 369 | public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { 370 | if ( $this->instructions && ! $sent_to_admin && $this->id === $order->payment_method ) { 371 | echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL; 372 | } 373 | } 374 | 375 | private function installments($price, $uniqid, $order) { 376 | $installments = 0; 377 | 378 | foreach($this->installmentsArray as $priceRange => $numOfInstallments){ 379 | if ($price > $priceRange) { 380 | continue; 381 | } 382 | else{ 383 | $installments = $numOfInstallments; 384 | break; 385 | } 386 | } 387 | 388 | $installMentsField = ''; 389 | if ($installments > 0 && is_int($installments)) { 390 | $installMentsField = ''; 401 | 402 | $installMentsField .= ""; 403 | 404 | echo '
405 |

406 | 407 | ' . $installMentsField 408 | . '

409 |
410 |
'; 411 | 412 | wc_enqueue_js(' 413 | var max = ' . $installments . '; 414 | jQuery("#shopform1").submit(function (e) { 415 | var i = parseInt(this.extInstallmentperiod.value); 416 | 417 | if (isNaN(i) || i <= 0 || i > max){ 418 | $(this.extInstallmentperiod).attr("disabled", "disabled"); 419 | $(this.extInstallmentoffset).attr("disabled", "disabled"); 420 | } 421 | 422 | this.digest.value = $(this.extInstallmentperiod).find(":selected").data("digest"); 423 | }); 424 | '); 425 | 426 | } 427 | } 428 | } 429 | --------------------------------------------------------------------------------