. */ /* Adds seb payment class to Jigoshop ------------------------------------------------------------ */ add_action( 'plugins_loaded', 'jigoshop_seb_payment_gateway', 0 ); function jigoshop_seb_payment_gateway() { if ( ! class_exists( 'jigoshop_payment_gateway' ) ) return; // if the Jigoshop payment gateway class is not available, do nothing add_action('plugins_loaded', 'jigoshop_seb_gateway_init'); function jigoshop_seb_gateway_init() { load_plugin_textdomain( 'jigoshop-seb-gateway', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } class jigoshop_seb_gateway extends jigoshop_payment_gateway { public function __construct() { parent::__construct(); $this->id = 'jigoshop_seb_gateway'; $this->icon = plugins_url( 'seb.gif', __FILE__ ); $this->has_fields = false; $this->enabled = Jigoshop_Base::get_options()->get_option( 'jigoshop_seb_gateway_enabled' ); $this->title = Jigoshop_Base::get_options()->get_option( 'jigoshop_seb_gateway_title' ); $this->description = Jigoshop_Base::get_options()->get_option( 'jigoshop_seb_gateway_description' ); $this->merchant_id = Jigoshop_Base::get_options()->get_option( 'jigoshop_seb_gateway_merchant_id' ); //add_action( 'thankyou_jigoshop_seb_gateway', array( &$this, 'thankyou_page' ) ); add_action( 'init', array( &$this, 'check_callback' ) ); add_action( 'valid_seb_callback', array( &$this, 'successful_request' ) ); add_action( 'receipt_jigoshop_seb_gateway', array( &$this, 'receipt_page' ) ); } /* Gateway options, displayed in admin menu ------------------------------------------------------ */ protected function get_default_options() { $defaults = array(); $defaults[] = array( 'name' => __( 'SEB', 'jigoshop-seb-gateway' ), 'type' => 'title', 'desc' => __( 'This payment gateway forwards client to SEB bank where he can make the payment.', 'jigoshop-seb-gateway' ) ); $defaults[] = array( 'name' => __( 'Enable SEB', 'jigoshop-seb-gateway' ), 'desc' => '', 'tip' => '', 'id' => 'jigoshop_seb_gateway_enabled', 'std' => 'no', 'type' => 'checkbox', 'choices' => array( 'no' => __( 'No', 'jigoshop-seb-gateway' ), 'yes' => __( 'Yes', 'jigoshop-seb-gateway' ) ) ); $defaults[] = array( 'name' => __( 'Method Title', 'jigoshop-seb-gateway' ), 'desc' => '', 'tip' => __( 'This controls the title which the user sees during checkout.', 'jigoshop-seb-gateway' ), 'id' => 'jigoshop_seb_gateway_title', 'std' => '', 'type' => 'text' ); $defaults[] = array( 'name' => __( 'Customer Message', 'jigoshop-seb-gateway' ), 'desc' => '', 'tip' => __( 'This controls the description which the user sees during checkout.', 'jigoshop-seb-gateway' ), 'id' => 'jigoshop_seb_gateway_description', 'std' => '', 'type' => 'longtext' ); $defaults[] = array( 'name' => __( 'SEB Merchant ID', 'jigoshop-seb-gateway' ), 'desc' => '', 'tip' => __( 'Please insert your SEB merchant ID. This is needed in order to make payment.', 'jigoshop-seb-gateway' ), 'id' => 'jigoshop_seb_gateway_merchant_id', 'std' => '', 'type' => 'text' ); return $defaults; } /* Display description for payment fields ------------------------------------------------------------ */ function payment_fields() { if ( $this->description ) { echo wpautop( wptexturize( $this->description ) ); } do_action( 'jigoshop_seb_gateway_payment_fields' ); // allow for insertion of custom code if needed } /* Display description for thank you page ------------------------------------------------------------ */ /*function thankyou_page() { if ( $this->description ) echo wpautop( wptexturize( $this->description ) ); do_action( 'jigoshop_seb_gateway_thankyou_page' ); // allow for insertion of custom code if needed }*/ /* Generate form ------------------------------------------------------------ */ function generate_form( $order_id ) { $order = new jigoshop_order( $order_id ); function createSignature( &$VK_a ) { $data = _composeData( &$VK_a ); $pkey = _readPrivKey(); $pkeyid = openssl_get_privatekey( _readPrivKey() ); openssl_sign( $data, $signature, $pkeyid ); $VK_MAC = base64_encode( $signature ); openssl_free_key( $pkeyid ); return $VK_MAC; } function _readPrivKey() { $key_file = plugins_url( 'private_key.pem', __FILE__ ); $fp = fopen( $key_file, "r" ); $cert = fread( $fp, 8192 ); fclose( $fp ); return $cert; } function _composeData( &$VK_a ) { $data = ""; foreach( $VK_a as $data_bit ) { $data .= _padIt( $data_bit ); } return $data; } function _padIt( $str = "" ) { return str_pad( strlen( $str ), 3, "0", STR_PAD_LEFT ) . $str; } $currency = Jigoshop_Base::get_options()->get_option( 'jigoshop_currency' ); $payment_message = __( 'Order nr.', 'jigoshop-seb-gateway' ) . " {$order->id}"; $VK_a['VK_SERVICE'] = '1002'; // service nr $VK_a['VK_VERSION'] = '008'; $VK_a['VK_SND_ID'] = $this->merchant_id; // id given for merchant $VK_a['VK_STAMP'] = intval($order->id); // payment id $VK_a['VK_AMOUNT'] = $order->order_total; // payment amount $VK_a['VK_CURR'] = $currency; // currency $VK_a['VK_REF'] = ''; // reference number $VK_a['VK_MSG'] = $payment_message; // payment message $VK_a['VK_MAC'] = createSignature( $VK_a ); //kontrollkood e allkiri $VK_a['VK_RETURN'] = site_url('/jigoshop/seb-callback.php'); // return url $VK_a['VK_CANCEL'] = get_permalink( jigoshop_get_page_id( 'cart' ) ); // returned when unsuccess $VK_a['VK_LANG'] = 'EST'; $VK_a['VK_CHARSET'] = 'UTF-8'; $fields = array(); foreach( $VK_a as $VK_name => $VK_value ) { $fields []= ''; } //$action_url = "https://pangalink.net/banklink/008/seb"; $action_url = "https://www.seb.ee/cgi-bin/unet3.sh/un3min.r"; return '
'; } /* Receipt page ----------------------------------------------------------- */ function receipt_page( $order_id ) { echo '' . __( 'Thank you for your order, please click the button below to pay with SEB.', 'jigoshop-seb-gateway') . '
'; echo $this->generate_form( $order_id ); } /* Process order ------------------------------------------------------------ */ function process_payment( $order_id ) { $order = &new jigoshop_order( $order_id ); return array( 'result' => 'success', 'redirect' => add_query_arg( 'order', $order->id, add_query_arg( 'key', $order->order_key, get_permalink( jigoshop_get_page_id( 'pay' ) ) ) ) ); /*// By default, mark as processing, but can be filtered for any mark $order->update_status( apply_filters( 'tgm_jigoshop_order_update_status', __( 'processing', 'Your order is being processed. Thank you!' ) ) ); // Remove cart jigoshop_cart::empty_cart(); // Return thankyou redirect return array( 'result' => 'success', 'redirect' => add_query_arg( 'key', $order->order_key, add_query_arg( 'order', $order_id, get_permalink( get_option( 'jigoshop_thanks_page_id' ) ) ) ) );*/ } function check_callback() { if ( strpos($_SERVER["REQUEST_URI"], '/jigoshop/seb-callback.php') !== false ) { $_POST = stripslashes_deep($_POST); do_action("valid_seb_callback", $_POST); } } function successful_request( $posted ){ function _padIt( $str = ""){ return str_pad( strlen( $str ), 3, "0", STR_PAD_LEFT) . $str; } function _readPubKey(){ $key_file = plugins_url( 'cert.pem' , __FILE__ ); $fp = fopen( $key_file, "r" ); $cert = fread( $fp, 8192 ); fclose( $fp ); return $cert; } function verifySignature( $VK_a){ $VK_MAC = $VK_a['VK_MAC']; $signature = base64_decode( $VK_MAC ); switch( $VK_a['VK_SERVICE'] ){ case '1101': $data = _padIt( $VK_a['VK_SERVICE'] ) . _padIt( $VK_a['VK_VERSION'] ) . _padIt( $VK_a['VK_SND_ID'] ) . _padIt( $VK_a['VK_REC_ID'] ) . _padIt( $VK_a['VK_STAMP'] ) . _padIt( $VK_a['VK_T_NO'] ) . _padIt( $VK_a['VK_AMOUNT'] ) . _padIt( $VK_a['VK_CURR'] ) . _padIt( $VK_a['VK_REC_ACC'] ) . _padIt( $VK_a['VK_REC_NAME'] ) . _padIt( $VK_a['VK_SND_ACC'] ) . _padIt( $VK_a['VK_SND_NAME'] ) . _padIt( $VK_a['VK_REF'] ) . _padIt( $VK_a['VK_MSG'] ) . _padIt( $VK_a['VK_T_DATE'] ); break; case '1901': $data = _padIt( $VK_a['VK_SERVICE'] ) . _padIt( $VK_a['VK_VERSION'] ) . _padIt( $VK_a['VK_SND_ID'] ) . _padIt( $VK_a['VK_REC_ID'] ) . _padIt( $VK_a['VK_STAMP'] ) . _padIt( $VK_a['VK_REF'] ) . _padIt( $VK_a['VK_MSG'] ); break; } $cert = _readPubKey(); $pubkey = openssl_get_publickey( $cert ); $out = openssl_verify( $data, $signature, $pubkey ); openssl_free_key( $pubkey ); return $out; } $sig_result = verifySignature( $posted ); $order = new jigoshop_order( (int) $posted['VK_STAMP'] ); $redirect_url = get_permalink( jigoshop_get_page_id( 'cart' ) ); switch( $sig_result ){ case 1: switch($posted['VK_SERVICE'] ) { case '1101': $order->add_order_note( __('SEB payment completed', 'jigoshop-seb-gateway') ); $order->payment_complete(); jigoshop_cart::empty_cart(); upload_order_data_to_compucash($order); $redirect_url = add_query_arg( 'key', $order->order_key, add_query_arg( 'order', $order_id, get_permalink( jigoshop_get_page_id('thanks') ) ) ); break; case '1901': $order->update_status('cancelled', __('SEB payment cancelled', 'jigoshop-seb-gateway')); break; default: error_log('doubleFeil'); break; } break; case 0: $order->add_order_note( __('Some error occured in communication with SEB', 'jigoshop-seb-gateway') ); break; default: $order->add_order_note( __('Some error occured with SEB', 'jigoshop-seb-gateway') ); break; } wp_safe_redirect($redirect_url); /*return array( 'result' => 'success', 'redirect' => add_query_arg( 'key', $order->order_key, add_query_arg( 'order', $order_id, get_permalink( get_option( 'jigoshop_thanks_page_id' ) ) ) ) );*/ exit; } } /* Add seb payment gateway to the Jigoshop gateways ------------------------------------------------------------ */ add_filter( 'jigoshop_payment_gateways', 'add_jigoshop_seb_payment_gateway' ); function add_jigoshop_seb_payment_gateway( $methods ) { $methods[] = 'jigoshop_seb_gateway'; return $methods; } }