From 998ecdd541d357466a115794e3dc99f1e15eb0d2 Mon Sep 17 00:00:00 2001 From: dmattke Date: Tue, 24 Apr 2018 08:33:51 +0200 Subject: [PATCH] version 1.4.0 --- GFDibsDao.php | 651 +++++++++++++-------------- GFDibsHook.php | 1001 ++++++++++++++++++++++-------------------- README.md | 2 +- gravityformsdibs.php | 62 ++- 4 files changed, 873 insertions(+), 843 deletions(-) diff --git a/GFDibsDao.php b/GFDibsDao.php index d1b23bc..e8f3b21 100644 --- a/GFDibsDao.php +++ b/GFDibsDao.php @@ -2,401 +2,406 @@ // add_action( 'init', array('GFDibsDao', 'updateDb') ); class GFDibsDao { - protected $dibs_table_name; - protected $gf_table_name; - protected $db; + protected $dibs_table_name; + protected $gf_table_name; + protected $db; - function log($message = null){ - if( WP_DEBUG === true ){ - error_log( print_r( $message, true ) ); - } - } - function __construct(){ - global $wpdb; - $this->db = $wpdb; - - $this->dibs_table_name = self::getDibsTableName(); - $this->gf_table_name = self::getDibsTransactionTableName(); + function log($message = null){ + if( WP_DEBUG === true ){ + error_log( print_r( $message, true ) ); } + } - public static function updateDb(){ - global $wpdb; - $sql = sprintf("ALTER TABLE %s MODIFY COLUMN order_id varchar(50) NOT NULL", $wpdb->prefix.'rg_dibs_transaction' ) ; + function __construct(){ + global $wpdb; + $this->db = $wpdb; - $wpdb->query($sql); - } + $this->dibs_table_name = self::getDibsTableName(); + $this->gf_table_name = self::getDibsTransactionTableName(); + } - function getAvailableForms( $current_form_id = null){ - $forms = RGFormsModel::get_forms(); - $available_forms = array(); + function getDibsTableName(){ + global $wpdb; + return $wpdb->prefix . "rg_dibs"; + } - foreach($forms as $form) { - if ( !$this->isDibsForm($form->id) || $current_form_id == $form->id ){ - $available_forms[] = $form; - } - } - return $available_forms; - } + function getDibsTransactionTableName(){ + global $wpdb; + return $wpdb->prefix . "rg_dibs_transaction"; + } - /* - ------------------------------------------------------------------------------------ - Transaction - ------------------------------------------------------------------------------------ - */ - function createTransaction( $post ){ - $this->log('GFDibsDao::createTransaction()'); - $this->log('POST variables'); - $this->log($post); - - $this->db->insert( - $this->gf_table_name, - array( - 'order_id' => $post['orderId'], - 'lead_id' => $post['leadId'], - 'payment_type' => $post['paymentType'], - 'test' => ( isset($post['test']) && $post['test'] == '1' ) ? 1 : 0, - 'amount' => $post['amount'], - 'date_created' => date('Y-m-d H:i:s') - ), - array( - '%s', // order_id - '%d', // lead_id - '%s', // payment_type - '%d', // test - '%d', // amount - '%s' // date - ) - ); - return $this->db->insert_id; - } + public static function updateDb(){ + global $wpdb; + $sql = sprintf("ALTER TABLE %s MODIFY COLUMN order_id varchar(50) NOT NULL", $wpdb->prefix.'rg_dibs_transaction' ) ; - function updateTransaction( $post ){ - // _log('GFDibsDao::updateTransaction'); - // _log($post); + $wpdb->query($sql); + } - if ( isset( $post['transaction']) ){ - $transaction_id = $post['transaction']; - } - elseif ( isset( $post['transact']) ){ - $transaction_id = $post['transact']; - } - $paytype = null; - if ( isset($post['cardnomask']) ){ - $paytype = $post['cardnomask']; - } + function getAvailableForms( $current_form_id = null){ + $forms = RGFormsModel::get_forms(); + $available_forms = array(); - if ( isset($post['paytype']) ){ - if ( trim($paytype) ){ - $paytype .= ' ('.$post['paytype'].')'; - } - else{ - $paytype .= $post['paytype']; - } + foreach($forms as $form) { + if ( !$this->isDibsForm($form->id) || $current_form_id == $form->id ){ + $available_forms[] = $form; } + } + + return $available_forms; + } + /* + ------------------------------------------------------------------------------------ + Transaction + ------------------------------------------------------------------------------------ + */ + function createTransaction( $post ){ + $this->log('GFDibsDao::createTransaction()'); + $this->log('POST variables'); + $this->log($post); - $this->db->update( + $this->db->insert( $this->gf_table_name, array( - 'completed' => 1, - 'transaction_id' => $transaction_id, - 'ticket' => ( isset($post['ticket']) ) ? $post['ticket'] : false, - 'paytype' => $paytype, + 'order_id' => $post['orderId'], + 'lead_id' => $post['leadId'], + 'payment_type' => $post['paymentType'], + 'test' => ( isset($post['test']) && $post['test'] == '1' ) ? 1 : 0, + 'amount' => $post['amount'], + 'date_created' => date('Y-m-d H:i:s') ), - array( 'order_id' => $post['orderId'] ), array( - '%d', - '%s', - '%s', - '%s', + '%s', // order_id + '%d', // lead_id + '%s', // payment_type + '%d', // test + '%d', // amount + '%s' // date ) - ); - } + ); - function getTransactionByLeadId($lead_id){ - $sql = sprintf('SELECT * FROM %s where lead_id = %d', $this->gf_table_name, $lead_id); - // _log($sql); - return $this->db->get_row($sql); - } + return $this->db->insert_id; + } - function getTransactionByOrderId($order_id){ - $sql = sprintf("SELECT * FROM %s where order_id = '%s'", $this->gf_table_name, $order_id); - // _log($sql); - return $this->db->get_row($sql); - } - /* - --------------------------------------------------------------------------------------------------- - Feed - --------------------------------------------------------------------------------------------------- - */ - function isDibsForm($form_id){ - global $wpdb; + function updateTransaction( $post ){ + // _log('GFDibsDao::updateTransaction'); + // _log($post); - $sql = sprintf('SELECT id from %s WHERE form_id=%s', $this->dibs_table_name, $form_id); + if ( isset( $post['transaction']) ){ + $transaction_id = $post['transaction']; + } + elseif ( isset( $post['transact']) ){ + $transaction_id = $post['transact']; + } - $feed_id = $this->db->get_var($sql); + $paytype = null; + if ( isset($post['cardnomask']) ){ + $paytype = $post['cardnomask']; + } - return $feed_id; + if ( isset($post['paytype']) ){ + if ( trim($paytype) ){ + $paytype .= ' ('.$post['paytype'].')'; + } + else{ + $paytype .= $post['paytype']; + } } - function getDefaultField(){ - $field = - array( - 'adminLabel' => null, - 'adminOnly' => null, - 'allowsPrepopulate' => null, - 'defaultValue' => null, - 'description' => null, - 'content' => null, - 'cssClass' => null, - 'errorMessage' => null, - 'id' => null, - 'inputName' => null, - 'isRequired' => null, - 'label' => null, - 'noDuplicates' => null, - 'size' => null, - 'type' => null, - 'postCustomFieldName' => null, - 'displayAllCategories' => null, - 'displayCaption' => null, - 'displayDescription' => null, - 'displayTitle' => null, - 'inputType' => null, - 'rangeMin' => null, - 'rangeMax' => null, - 'calendarIconType' => null, - 'calendarIconUrl' => null, - 'dateType' => null, - 'dateFormat' => null, - 'phoneFormat' => null, - 'addressType' => null, - 'defaultCountry' => null, - 'defaultProvince' => null, - 'defaultState' => null, - 'hideAddress2' => null, - 'hideCountry' => null, - 'hideState' => null, - 'inputs' => null, - 'nameFormat' => null, - 'allowedExtensions' => null, - 'captchaType' => null, - 'pageNumber' => null, - 'captchaTheme' => null, - 'simpleCaptchaSize' => null, - 'simpleCaptchaFontColor' => null, - 'simpleCaptchaBackgroundColor' => null, - 'failed_validation' => null, - 'productField' => null, - 'enablePasswordInput' => null, - 'maxLength' => null, - 'enablePrice' => null, - 'basePrice' => null, - 'formId' => null, - 'descriptionPlacement' => null - ); - } + $this->db->update( + $this->gf_table_name, + array( + 'completed' => 1, + 'transaction_id' => $transaction_id, + 'ticket' => ( isset($post['ticket']) ) ? $post['ticket'] : false, + 'paytype' => $paytype, + ), + array( 'order_id' => $post['orderId'] ), + array( + '%d', + '%s', + '%s', + '%s', + ) + ); + } - /* database */ - function setupTables(){ - global $wpdb; + function getTransactionByLeadId($lead_id){ + $sql = sprintf('SELECT * FROM %s where lead_id = %d', $this->gf_table_name, $lead_id); + // _log($sql); + return $this->db->get_row($sql); + } - if ( ! empty($wpdb->charset) ) - $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; - if ( ! empty($wpdb->collate) ) - $charset_collate .= " COLLATE $wpdb->collate"; - $sql = "CREATE TABLE IF NOT EXISTS $this->dibs_table_name ( - id mediumint(8) unsigned not null auto_increment, - form_id mediumint(8) unsigned not null, - is_active tinyint(1) not null default 1, - meta longtext, - PRIMARY KEY (id), - KEY form_id (form_id) - )$charset_collate;"; + function getTransactionByOrderId($order_id){ + $sql = sprintf("SELECT * FROM %s where order_id = '%s'", $this->gf_table_name, $order_id); + // _log($sql); + return $this->db->get_row($sql); + } - $wpdb->query($sql); + /* + --------------------------------------------------------------------------------------------------- + Feed + --------------------------------------------------------------------------------------------------- + */ + function isDibsForm($form_id){ + global $wpdb; + $sql = sprintf('SELECT id from %s WHERE form_id=%s', $this->dibs_table_name, $form_id); + $feed_id = $this->db->get_var($sql); - // error_log($sql); - - $transaction_table = self::getDibsTransactionTableName(); - $sql = "CREATE TABLE IF NOT EXISTS $transaction_table ( - `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, - `order_id` varchar(50) NOT NULL, - `completed` int(1) DEFAULT NULL, - `transaction_id` varchar(50) DEFAULT NULL, - `payment_type` varchar(50) DEFAULT NULL, - `paytype` varchar(50) DEFAULT NULL, - `test` int(1) DEFAULT NULL, - `amount` int(10) DEFAULT NULL, - `date_created` datetime DEFAULT NULL, - `date_completed` datetime DEFAULT NULL, - `ticket` varchar(50) DEFAULT NULL, - `lead_id` int(10) unsigned NOT NULL, - PRIMARY KEY (`id`) - )$charset_collate;"; + return $feed_id; + } - $wpdb->query($sql); + function getDefaultField(){ + $field = + array( + 'adminLabel' => null, + 'adminOnly' => null, + 'allowsPrepopulate' => null, + 'defaultValue' => null, + 'description' => null, + 'content' => null, + 'cssClass' => null, + 'errorMessage' => null, + 'id' => null, + 'inputName' => null, + 'isRequired' => null, + 'label' => null, + 'noDuplicates' => null, + 'size' => null, + 'type' => null, + 'postCustomFieldName' => null, + 'displayAllCategories' => null, + 'displayCaption' => null, + 'displayDescription' => null, + 'displayTitle' => null, + 'inputType' => null, + 'rangeMin' => null, + 'rangeMax' => null, + 'calendarIconType' => null, + 'calendarIconUrl' => null, + 'dateType' => null, + 'dateFormat' => null, + 'phoneFormat' => null, + 'addressType' => null, + 'defaultCountry' => null, + 'defaultProvince' => null, + 'defaultState' => null, + 'hideAddress2' => null, + 'hideCountry' => null, + 'hideState' => null, + 'inputs' => null, + 'nameFormat' => null, + 'allowedExtensions' => null, + 'captchaType' => null, + 'pageNumber' => null, + 'captchaTheme' => null, + 'simpleCaptchaSize' => null, + 'simpleCaptchaFontColor' => null, + 'simpleCaptchaBackgroundColor' => null, + 'failed_validation' => null, + 'productField' => null, + 'enablePasswordInput' => null, + 'maxLength' => null, + 'enablePrice' => null, + 'basePrice' => null, + 'formId' => null, + 'descriptionPlacement' => null + ); + } - try{ - // update $transaction_table - $sql = sprintf( "SHOW COLUMNS FROM %s LIKE 'paytype'; ", $transaction_table); - $has_column = $wpdb->query($sql); - if ( !$has_column ){ - $sql = sprintf("ALTER TABLE %s ADD paytype varchar(40) DEFAULT NULL", $transaction_table ) ; - $wpdb->query($sql); - } - } - catch(Excecption $e){} - } + /* database */ + function setupTables(){ + global $wpdb; + + if ( ! empty($wpdb->charset) ) + $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; + if ( ! empty($wpdb->collate) ) + $charset_collate .= " COLLATE $wpdb->collate"; + + $sql = "CREATE TABLE IF NOT EXISTS $this->dibs_table_name ( + id mediumint(8) unsigned not null auto_increment, + form_id mediumint(8) unsigned not null, + is_active tinyint(1) not null default 1, + meta longtext, + PRIMARY KEY (id), + KEY form_id (form_id) + )$charset_collate;"; + + $wpdb->query($sql); + + // error_log($sql); + + $transaction_table = self::getDibsTransactionTableName(); + $sql = "CREATE TABLE IF NOT EXISTS $transaction_table ( + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, + `order_id` varchar(50) NOT NULL, + `completed` int(1) DEFAULT NULL, + `transaction_id` varchar(50) DEFAULT NULL, + `payment_type` varchar(50) DEFAULT NULL, + `paytype` varchar(50) DEFAULT NULL, + `test` int(1) DEFAULT NULL, + `amount` int(10) DEFAULT NULL, + `date_created` datetime DEFAULT NULL, + `date_completed` datetime DEFAULT NULL, + `ticket` varchar(50) DEFAULT NULL, + `lead_id` int(10) unsigned NOT NULL, + PRIMARY KEY (`id`) + )$charset_collate;"; + $wpdb->query($sql); - function setDateCompleted($lead_id){ - $sql = sprintf('UPDATE %s SET date_completed="%s" where lead_id=%d', $this->gf_table_name, date('Y-m-d H:i:s'), $lead_id ); - error_log($sql); - $this->db->query($sql); - } - function getDateCompleted($lead_id){ - $sql = sprintf('SELECT date_completed FROM %s where lead_id=%d', $this->gf_table_name, $lead_id ); - error_log($sql); - return $this->db->get_var($sql); + try{ + // update $transaction_table + $sql = sprintf( "SHOW COLUMNS FROM %s LIKE 'paytype'; ", $transaction_table); + $has_column = $wpdb->query($sql); + if ( !$has_column ){ + $sql = sprintf("ALTER TABLE %s ADD paytype varchar(40) DEFAULT NULL", $transaction_table ) ; + $wpdb->query($sql); + } } + catch(Excecption $e){} + } - function getDibsMeta($feed_id = 0 ){ - global $wpdb; - $sql = sprintf( 'SELECT form_id, meta FROM %s WHERE id=%d', self::getDibsTableName(), $feed_id ); - $feed = $wpdb->get_row($sql); + function setDateCompleted($lead_id){ + $sql = sprintf('UPDATE %s SET date_completed="%s" where lead_id=%d', $this->gf_table_name, date('Y-m-d H:i:s'), $lead_id ); + error_log($sql); + $this->db->query($sql); + } - if ( is_object($feed) ){ - $feed->meta = maybe_unserialize($feed->meta ); - } - else{ - $feed = new stdClass(); - $feed->meta = - array( - 'dibs_feed_id' => null, - 'gf_dibs_mode' => null, - 'gf_dibs_type' => null, - 'gf_dibs_no_confirmations' => 1, - 'gf_dibs_form' => null, - 'gf_dibs_capture_now' => null, - 'billingFirstName' => null, - 'billingLastName' => null, - 'billingEmail' => null, - 'billingMobile' => null, - 'billingAddress' => null, - 'billingPostalCode' => null, - 'billingPostalPlace' => null - ); - } + function getDateCompleted($lead_id){ + $sql = sprintf('SELECT date_completed FROM %s where lead_id=%d', $this->gf_table_name, $lead_id ); + error_log($sql); + return $this->db->get_var($sql); + } - return $feed; - } + function getDibsMeta($feed_id = 0 ){ + global $wpdb; + $sql = sprintf( 'SELECT form_id, meta FROM %s WHERE id=%d', self::getDibsTableName(), $feed_id ); - function setDibsMeta($meta, $feed_id = null){ + $feed = $wpdb->get_row($sql); - // get form id - $form_id = $meta['gf_dibs_form']; + if ( is_object($feed) ){ + $feed->meta = maybe_unserialize($feed->meta ); + } + else{ + $feed = new stdClass(); + $feed->meta = + array( + 'dibs_feed_id' => null, + 'gf_dibs_mode' => null, + 'gf_dibs_type' => null, + 'gf_dibs_no_confirmations' => 1, + 'gf_dibs_form' => null, + 'gf_dibs_capture_now' => null, + 'billingFirstName' => null, + 'billingLastName' => null, + 'billingEmail' => null, + 'billingMobile' => null, + 'billingAddress' => null, + 'billingPostalCode' => null, + 'billingPostalPlace' => null + ); + } + return $feed; + } - // unset fields - // unset($meta['gf_dibs_form']); - unset($meta['update']); - unset($meta['gf_paypal_submit']); - global $wpdb; - $this->dibs_table_name = self::getDibsTableName(); + function setDibsMeta($meta, $feed_id = null){ + // get form id + $form_id = $meta['gf_dibs_form']; - if ( !$feed_id || $feed_id == 0){ - $wpdb->insert( - $this->dibs_table_name, - array( - 'form_id' => $form_id, - 'meta' => maybe_serialize($meta ) - ), - array( - '%d', - '%s' - ) - ); + // unset fields + // unset($meta['gf_dibs_form']); + unset($meta['update']); + unset($meta['gf_paypal_submit']); - $feed_id = $wpdb->insert_id; + global $wpdb; + $this->dibs_table_name = self::getDibsTableName(); - } - else{ - $wpdb->update( - $this->dibs_table_name, - array( - 'form_id' => $form_id, - 'meta' => maybe_serialize($meta ) - ), - array( 'id' => $feed_id ), - array( - '%d', - '%s' - ), - array( '%d' ) - ); - } + if ( !$feed_id || $feed_id == 0){ + $wpdb->insert( + $this->dibs_table_name, + array( + 'form_id' => $form_id, + 'meta' => maybe_serialize($meta ) + ), + array( + '%d', + '%s' + ) + ); + $feed_id = $wpdb->insert_id; - return $feed_id; + } + else{ + $wpdb->update( + $this->dibs_table_name, + array( + 'form_id' => $form_id, + 'meta' => maybe_serialize($meta ) + ), + array( 'id' => $feed_id ), + array( + '%d', + '%s' + ), + array( '%d' ) + ); } - function getFeeds(){ - global $wpdb; - - $this->dibs_table_name = self::getDibsTableName(); - $this->gf_table_name = RGFormsModel::get_form_table_name(); + return $feed_id; + } - $sql = "SELECT s.id, s.is_active, s.form_id, s.meta, f.title as form_title - FROM $this->dibs_table_name s - INNER JOIN $this->gf_table_name f ON s.form_id = f.id"; - $results = $wpdb->get_results($sql, ARRAY_A); + function getFeeds(){ + global $wpdb; - $count = sizeof($results); - for($i=0; $i<$count; $i++){ - $results[$i]["meta"] = maybe_unserialize($results[$i]["meta"]); - } + $this->dibs_table_name = self::getDibsTableName(); + $this->gf_table_name = RGFormsModel::get_form_table_name(); - return $results; - } + $sql = "SELECT s.id, s.is_active, s.form_id, s.meta, f.title as form_title + FROM $this->dibs_table_name s + INNER JOIN $this->gf_table_name f ON s.form_id = f.id"; + $results = $wpdb->get_results($sql, ARRAY_A); - function getDibsTableName(){ - global $wpdb; - return $wpdb->prefix . "rg_dibs"; + $count = sizeof($results); + + for($i=0; $i<$count; $i++){ + $results[$i]["meta"] = maybe_unserialize($results[$i]["meta"]); } - function getDibsTransactionTableName(){ - global $wpdb; - return $wpdb->prefix . "rg_dibs_transaction"; - } + return $results; + } - function deleteFeed($id){ - global $wpdb; - $this->dibs_table_name = self::getDibsTableName(); - $sql = sprintf('DELETE FROM %s where id=%d', $this->dibs_table_name, $id); - $wpdb->query($sql); - } + + + function deleteFeed($id){ + global $wpdb; + $this->dibs_table_name = self::getDibsTableName(); + $sql = sprintf('DELETE FROM %s where id=%d', $this->dibs_table_name, $id); + $wpdb->query($sql); + } function getFormFields($form_id = null){ @@ -433,10 +438,12 @@ function getFormMeta($form_id){ */ function getLeadMetaValue($lead_id, $field_number){ - $sql = sprintf('select value from %srg_lead_detail where lead_id = %d and field_number like %s', $this->db->prefix, $lead_id, $field_number); + _log('GFDibsDao::getLeadMetaValue()'); + // deprecated since GF 2.3.x + // $sql = sprintf('select value from %srg_lead_detail where lead_id = %d and field_number like %s', $this->db->prefix, $lead_id, $field_number); + $sql = sprintf('SELECT meta_value FROM %sgf_entry_meta WHERE entry_id="%d" AND meta_key LIKE "%s"', $this->db->prefix, $lead_id, $field_number); $this->log($sql); return $this->db->get_var($sql); } - } -?> \ No newline at end of file + } // end of class \ No newline at end of file diff --git a/GFDibsHook.php b/GFDibsHook.php index fa45d37..e110bed 100755 --- a/GFDibsHook.php +++ b/GFDibsHook.php @@ -1,577 +1,603 @@ setupTables(); - } - public static function checkDBUpdate() - { - $version = (int) str_replace('.', null, _getConfig('version')); - if ($version >= 130) { - self::setupDBTables(); - } - } +class GFDibsHook{ - public static function includeFrontendScripts() - { - wp_enqueue_script('jquery'); - wp_register_script('gfdibsuser', plugin_dir_url(__FILE__).'/assets/gfdibs_user.js', null, '1.2.0'); - wp_enqueue_script('gfdibsuser'); - } + public static function addAdminBodyClass( $classes ) { + $classes .= ' gf-dibs '; - public static function includeAdminScripts() - { - wp_enqueue_script('jquery'); - wp_register_script('gfdibsadmin', plugin_dir_url(__FILE__).'/assets/gfdibs_admin.js', null, '1.2.0'); - wp_enqueue_script('gfdibsadmin'); + return $classes; + } - wp_register_style('gfdibsadmin_style', plugin_dir_url(__FILE__).'/assets/gfdibs_admin.css', null, '1.2.0'); - wp_enqueue_style('gfdibsadmin_style'); + + public static function setupDBTables() { + $DAO = new GFDibsDao(); + $DAO->setupTables(); + } + + + public static function checkDBUpdate(){ + $version = (int) str_replace('.', null, _getConfig('version')); + if ( $version >= 130 ){ + self::setupDBTables(); } + } - public static function preRenderForm($form) - { - $Dao = new GFDibsDao(); - if ($feed_id = $Dao->isDibsForm($form['id'])) { - global $post; + public static function includeFrontendScripts(){ + wp_enqueue_script('jquery'); + wp_register_script( 'gfdibsuser', plugin_dir_url(__FILE__).'/assets/gfdibs_user.js', null, '1.2.0' ); + wp_enqueue_script('gfdibsuser'); + } - // $feed = $Dao->getDibsMeta($feed_id); - // hidden input form id - $Field = new GF_Field_Hidden(); + public static function includeAdminScripts(){ + wp_enqueue_script('jquery'); + wp_register_script( 'gfdibsadmin', plugin_dir_url(__FILE__).'/assets/gfdibs_admin.js', null, '1.2.0' ); + wp_enqueue_script('gfdibsadmin'); - $Field->type = 'hidden'; - $Field->label = 'dibs_form_id'; - $Field->pageNumber = 1; - $Field->formId = $form['id']; + wp_register_style( 'gfdibsadmin_style', plugin_dir_url(__FILE__).'/assets/gfdibs_admin.css', null, '1.2.0' ); + wp_enqueue_style('gfdibsadmin_style'); + } - $Field->id = 9998; - $Field->inputName = 'dibs_form_id'; - $Field->defaultValue = $form['id']; - array_push($form['fields'], $Field); + public static function preRenderForm($form) { - // hidden input return url - $Field = new GF_Field_Hidden(); + $Dao = new GFDibsDao(); - $Field->type = 'hidden'; - $Field->label = 'return_rl'; - $Field->pageNumber = 1; - $Field->formId = $form['id']; + if ( $feed_id = $Dao->isDibsForm($form['id']) ){ + global $post; - $Field->id = 9999; - $Field->inputName = 'return_rl'; - $Field->defaultValue = get_permalink($post->ID); + // $feed = $Dao->getDibsMeta($feed_id); - array_push($form['fields'], $Field); - } + // hidden input form id + $Field = new GF_Field_Hidden(); - return $form; + $Field->type = 'hidden'; + $Field->label = 'dibs_form_id'; + $Field->pageNumber = 1; + $Field->formId = $form['id']; + + $Field->id = 9998; + $Field->inputName = 'dibs_form_id'; + $Field->defaultValue = $form['id']; + + array_push($form['fields'], $Field); + + + // hidden input return url + $Field = new GF_Field_Hidden(); + + $Field->type = 'hidden'; + $Field->label = 'return_rl'; + $Field->pageNumber = 1; + $Field->formId = $form['id']; + + $Field->id = 9999; + $Field->inputName = 'return_rl'; + $Field->defaultValue = get_permalink($post->ID ); + + array_push($form['fields'], $Field); } - public static function changeColumnData($value, $lead_id, $index, $Entry) - { - if ($index == 'transaction_id') { - $Dao = new GFDibsDao(); - $Transaction = $Dao->getTransactionByLeadId($Entry['id']); - $value = $Transaction->transaction_id; - } elseif ($index == 'order_id') { - $Dao = new GFDibsDao(); - $Transaction = $Dao->getTransactionByLeadId($Entry['id']); - $value = $Transaction->order_id; - } elseif ($index == 'payment_status') { - $Dao = new GFDibsDao(); - $Transaction = $Dao->getTransactionByLeadId($Entry['id']); - $value = (isset($Transaction->completed) && $Transaction->completed == 1) ? 'completed' : 'open'; - } elseif ($index == 'payment_amount') { - $Dao = new GFDibsDao(); - $Transaction = $Dao->getTransactionByLeadId($Entry['id']); - $value = (isset($Transaction->amount) && $Transaction->amount > 0) ? ($Transaction->amount / 100) : 0; - } + return $form; + } - return $value; + + public static function changeColumnData($value, $lead_id, $index, $Entry){ + if ( $index == 'transaction_id'){ + $Dao = new GFDibsDao(); + $Transaction = $Dao->getTransactionByLeadId($Entry['id']); + $value = $Transaction->transaction_id; + } + else if ( $index == 'order_id'){ + $Dao = new GFDibsDao(); + $Transaction = $Dao->getTransactionByLeadId($Entry['id']); + $value = $Transaction->order_id; + } + else if ( $index == 'payment_status'){ + $Dao = new GFDibsDao(); + $Transaction = $Dao->getTransactionByLeadId($Entry['id']); + $value = ( isset($Transaction->completed) && $Transaction->completed == 1 ) ? 'completed' : 'open' ; + } + else if ( $index == 'payment_amount'){ + $Dao = new GFDibsDao(); + $Transaction = $Dao->getTransactionByLeadId($Entry['id']); + $value = ( isset($Transaction->amount) && $Transaction->amount > 0 ) ? ($Transaction->amount/100) : 0 ; } - public static function modifyExportData($leads, $form, $offset) - { - if (is_array($leads)) { - $Dao = new GFDibsDao(); - foreach ($leads as $key => $Lead) { - $Transaction = $Dao->getTransactionByLeadId($Lead['id']); - $leads[$key]['transaction_id'] = (isset($Transaction->transaction_id) && $Transaction->transaction_id) ? $Transaction->transaction_id : ''; - $leads[$key]['payment_date'] = (isset($Transaction->date_completed) && $Transaction->date_completed) ? $Transaction->date_completed : ''; - $leads[$key]['payment_status'] = (isset($Transaction->completed) && $Transaction->completed == 1) ? 'completed' : 'open'; - $leads[$key]['payment_amount'] = (isset($Transaction->amount) && $Transaction->amount) ? $Transaction->amount / 100 : 0; - } - } + return $value; + } + - return $leads; + public static function modifyExportData($leads, $form, $offset){ + if ( is_array($leads) ){ + $Dao = new GFDibsDao(); + foreach ($leads as $key => $Lead) { + $Transaction = $Dao->getTransactionByLeadId($Lead['id']); + $leads[$key]['transaction_id'] = ( isset($Transaction->transaction_id) && $Transaction->transaction_id ) ? $Transaction->transaction_id : '' ; + $leads[$key]['payment_date'] = ( isset($Transaction->date_completed) && $Transaction->date_completed ) ? $Transaction->date_completed : '' ; + $leads[$key]['payment_status'] = ( isset($Transaction->completed) && $Transaction->completed == 1 ) ? 'completed' : 'open' ; + $leads[$key]['payment_amount'] = ( isset($Transaction->amount) && $Transaction->amount ) ? $Transaction->amount/100 : 0 ; + } } - public static function parseNotification($mail) - { - $Dao = new GFDibsDao(); - $Transaction = null; + return $leads; + } + - if (isset($_POST['orderId'])) { // check if orderId exists - if ($Transaction = $Dao->getTransactionByOrderId($_POST['orderId'])) { // check if orderId is related to a transaction + public static function parseNotification( $mail ){ + $Dao = new GFDibsDao(); + $Transaction = null; + + if ( isset($_POST['orderId']) ){ // check if orderId exists + if ( $Transaction = $Dao->getTransactionByOrderId($_POST['orderId']) ){ // check if orderId is related to a transaction $lead = GFFormsModel::get_lead($Transaction->lead_id); // get lead (to get form_id) - if (isset($lead['form_id']) && is_numeric($lead['form_id'])) { // check if form_id is set - if ($feed_id = $Dao->isDibsForm($lead['form_id'])) {// get feed + if ( isset($lead['form_id']) && is_numeric($lead['form_id']) ){ // check if form_id is set + if( $feed_id = $Dao->isDibsForm($lead['form_id']) ) {// get feed $feed = $Dao->getDibsMeta($feed_id); // get feed settings $placeholders = self::getPlaceholders($Transaction, $feed); - // replace placeholders - $mail = self::replacePlaceholders($mail, $placeholders); + // replace placeholders + $mail = self::replacePlaceholders($mail, $placeholders); } } } - } - - return $mail; } - public static function disableNotifications($unknown, $confirmation, $form, $lead) - { - $Dao = new GFDibsDao(); - //_log('GFDibsHook::disableNotifications()'); + return $mail; + } - $is_disabled = false; - if ($feed_id = $Dao->isDibsForm($form['id'])) { - $feed = $Dao->getDibsMeta($feed_id); + public static function disableNotifications($unknown, $confirmation, $form, $lead){ + $Dao = new GFDibsDao(); + //_log('GFDibsHook::disableNotifications()'); - if (self::isDibsPayment($feed, $lead)) { - //$Dao->log($feed->meta['gf_dibs_no_confirmations']); - if (isset($feed->meta['gf_dibs_no_confirmations']) && $feed->meta['gf_dibs_no_confirmations'] == '1') { - $is_disabled = true; - } - } - } + $is_disabled = false; + + if ( $feed_id = $Dao->isDibsForm($form['id']) ){ + $feed = $Dao->getDibsMeta($feed_id); - return $is_disabled; + if( self::isDibsPayment($feed, $lead) ){ + //$Dao->log($feed->meta['gf_dibs_no_confirmations']); + if ( isset($feed->meta['gf_dibs_no_confirmations']) && $feed->meta['gf_dibs_no_confirmations'] == '1' ){ + $is_disabled = true; + } + } } - public static function isDibsPayment($feed, $lead) - { - // _log('$feed'); - // _log($feed); - // _log('$lead'); - // _log($lead); - $is_dibs_payment = true; + return $is_disabled; + } - if (isset($feed->meta['paymentMethods']) && trim($feed->meta['paymentMethods'])) { - $payment_method_field_id = $feed->meta['paymentMethods']; - if (isset($lead[$payment_method_field_id]) && trim($lead[$payment_method_field_id]) && strtolower($lead[$payment_method_field_id]) != 'dibs') { - $is_dibs_payment = false; - } - } + public static function isDibsPayment($feed, $lead){ + // _log('$feed'); + // _log($feed); + // _log('$lead'); + // _log($lead); + $is_dibs_payment = true; + + if ( isset($feed->meta['paymentMethods']) && trim($feed->meta['paymentMethods']) ){ + $payment_method_field_id = $feed->meta['paymentMethods']; - return $is_dibs_payment; + if ( isset($lead[$payment_method_field_id]) && trim($lead[$payment_method_field_id]) && strtolower($lead[$payment_method_field_id]) != 'dibs' ){ + $is_dibs_payment = false; + } } - public static function checkIfCustomMerchantId($feed) - { - $custom_merchant_id = null; - if (isset($feed->meta['gf_dibs_custom_merchant_id'])) { - $tmp_cmid = trim($feed->meta['gf_dibs_custom_merchant_id']); - if (is_numeric($tmp_cmid)) { - $custom_merchant_id = $tmp_cmid; - } - } + return $is_dibs_payment; + } + - return $custom_merchant_id; + public static function checkIfCustomMerchantId($feed){ + $custom_merchant_id = null; + if ( isset($feed->meta['gf_dibs_custom_merchant_id']) ){ + $tmp_cmid = trim($feed->meta['gf_dibs_custom_merchant_id']); + if ( is_numeric($tmp_cmid) ){ + $custom_merchant_id = $tmp_cmid; + } } - public static function dibsTransition($confirmation, $form, $lead, $ajax) - { - $Dao = new GFDibsDao(); - _log('GFDibsHook::dibsTransition()'); - $order_id = _is($_POST, 'orderId'); - if (!$order_id && $feed_id = $Dao->isDibsForm($form['id'])) { - $feed = $Dao->getDibsMeta($feed_id); - //$Dao->log('feed:'); - //$Dao->log($feed); - // check if user has choosen an alternative payment method - if (!self::isDibsPayment($feed, $lead)) { - return $confirmation; - } + return $custom_merchant_id; + } - // dibs test modus - if (isset($feed->meta['gf_dibs_mode']) && $feed->meta['gf_dibs_mode'] == '1') { - $_POST['test'] = 1; - } - // payment type - if (isset($feed->meta['gf_dibs_type'])) { - if ($feed->meta['gf_dibs_type'] == '2') { - $_POST['createTicketAndAuth'] = 1; - $_POST['paymentType'] = 'createTicketAndAuth'; - $_POST['captureNow'] = '1'; - } elseif ($feed->meta['gf_dibs_type'] == '3') { - $_POST['createTicket'] = 1; - $_POST['paymentType'] = 'createTicket'; - } else { - $_POST['paymentType'] = 'noAgreement'; - $_POST['captureNow'] = '1'; - } - } + public static function dibsTransition($confirmation, $form, $lead, $ajax){ + $Dao = new GFDibsDao(); + _log('GFDibsHook::dibsTransition()'); - unset($feed->meta['gf_dibs_form']); - unset($feed->meta['dibs_feed_id']); - unset($feed->meta['gf_dibs_type']); - unset($feed->meta['gf_dibs_mode']); - - foreach ($feed->meta as $key => $value) { - $value = str_replace('.', '_', $value); - //$Dao->log( $key ); - //$Dao->log( 'input_'.$value ); - if (isset($_POST['input_'.$value]) && strlen(trim($_POST['input_'.$value]))) { - $_POST[$key] = $_POST['input_'.$value]; - - if ($key == 'amount') { - $_POST[$key] = $_POST[$key] * 100; - } - - if ($key == 'billingEmail') { - $_POST['email'] = $_POST[$key]; - } - } - } + $order_id = _is($_POST, 'orderId'); + if ( !$order_id && $feed_id = $Dao->isDibsForm($form['id']) ){ + _log('$_POST'); + _log($_POST); + $feed = $Dao->getDibsMeta($feed_id); - // set ordertext - $_POST['ordertext'] = null; - if (isset($_POST['billingFirstName'])) { - $_POST['ordertext'] .= $_POST['billingFirstName'].' '; - } - if (isset($_POST['billingLastName'])) { - $_POST['ordertext'] .= $_POST['billingLastName'].' '; - } + //$Dao->log('feed:'); + //$Dao->log($feed); - if (isset($_POST['email'])) { - $_POST['ordertext'] .= '('.$_POST['email'].')'; - } - $_POST['ordertext'] = trim($_POST['ordertext']); + // check if user has choosen an alternative payment method + if( !self::isDibsPayment($feed, $lead) ){ + return $confirmation; + } - // $_POST['orderId'] = hexdec(uniqid()); - $_POST['leadId'] = $lead['id']; + // dibs test modus + if ( isset($feed->meta['gf_dibs_mode']) && $feed->meta['gf_dibs_mode'] == '1' ){ + $_POST['test'] = 1; + } - $order_id = uniqid(); + // payment type + if ( isset($feed->meta['gf_dibs_type']) ){ + if ( $feed->meta['gf_dibs_type'] == '2' ){ + $_POST['createTicketAndAuth'] = 1; + $_POST['paymentType'] = 'createTicketAndAuth'; + $_POST['captureNow'] = '1'; + } + else if ( $feed->meta['gf_dibs_type'] == '3' ){ + $_POST['createTicket'] = 1; + $_POST['paymentType'] = 'createTicket'; + } + else{ + $_POST['paymentType'] = 'noAgreement'; + $_POST['captureNow'] = '1'; + } + } - if ($suffix = get_option(ORDER_ID_SUFFIX)) { - $order_id = $suffix.'_'.$order_id; - } + unset($feed->meta['gf_dibs_form']); + unset($feed->meta['dibs_feed_id']); + unset($feed->meta['gf_dibs_type']); + unset($feed->meta['gf_dibs_mode']); - // dx - $_POST['orderId'] = $order_id; - // d2 - $_POST['orderid'] = $order_id; + foreach ($feed->meta as $key => $value) { + $value = str_replace('.', '_', $value); + //$Dao->log( $key ); + //$Dao->log( 'input_'.$value ); + if ( isset($_POST['input_'.$value]) && strlen(trim($_POST['input_'.$value])) ){ + $_POST[$key] = $_POST['input_'.$value]; - $_POST['currency'] = get_option('rg_gforms_currency'); - $_POST['language'] = _get_language(); - $_POST['lang'] = _get_language(); - $_POST['merchant'] = trim(get_option(MERCHANT)); + if ( $key == 'amount' ){ + $_POST[$key] = $_POST[$key] * 100; + } - if ($custom_merchant_id = self::checkIfCustomMerchantId($feed)) { - $_POST['merchant'] = $custom_merchant_id; - } + if ( $key == 'billingEmail' ){ + $_POST['email'] = $_POST[$key]; + } + } + } - $_POST['send_to_dibs'] = '1'; + // set ordertext + $_POST['ordertext'] = null; + if ( isset($_POST['billingFirstName']) ){ + $_POST['ordertext'] .= $_POST['billingFirstName']." "; + } + if ( isset($_POST['billingLastName']) ){ + $_POST['ordertext'] .= $_POST['billingLastName']." "; + } - if (isset($_POST['input_9998'])) { // input_9999 => return url - $_POST['dibs_form_id'] = $_POST['input_9998']; - } + if ( isset($_POST['email']) ){ + $_POST['ordertext'] .= "(".$_POST['email'].")"; + } + $_POST['ordertext'] = trim($_POST['ordertext']); - if (isset($_POST['input_9999'])) { // input_9999 => return url - // D2 - $_POST['callbackurl'] = $_POST['input_9999']; - $_POST['accepturl'] = $_POST['input_9999']; - // DX - $_POST['callbackUrl'] = $_POST['input_9999']; - $_POST['acceptReturnUrl'] = $_POST['input_9999']; - //$Dao->log('$_POST'); - //$Dao->log($_POST); - } - if (get_option(USE_MD5)) { - $md5key = null; - $k1 = trim(get_option(MD5_K1)); - $k2 = trim(get_option(MD5_K2)); + // $_POST['orderId'] = hexdec(uniqid()); + $_POST['leadId'] = $lead['id']; - if ($k1 && $k2) { - $md5_vars = 'merchant='.$_POST['merchant'].'&orderid='.$order_id.'¤cy='.$_POST['currency'].'&amount='.$_POST['amount']; - $md5key = md5($k2.md5($k1.$md5_vars)); - } + $order_id = uniqid(); - $_POST['md5key'] = $md5key; - } + if ( $suffix = get_option(ORDER_ID_SUFFIX) ){ + $order_id = $suffix.'_'.$order_id; + } - if ($decorator = get_option(DECORATOR)) { - $_POST['decorator'] = $decorator; - } + // dx + $_POST['orderId'] = $order_id; - $transaction_id = $Dao->createTransaction($_POST); - $Dao->log('wp_rg_dibs_transaction: id'); - $Dao->log($transaction_id); - - $confirmation = '
'; - foreach ($_POST as $key => $value) { - if ( - // !is_numeric(strpos($key, 'input')) && - !is_numeric(strpos($key, 'MAX_FILE_SIZE')) - && !is_numeric(strpos($key, 'state')) - && !is_numeric(strpos($key, 'gform'))) { - $confirmation .= sprintf('', $key, $key, $value); - } - } + // d2 + $_POST['orderid'] = $order_id; - $confirmation .= _e('Videresending til DIBS ...', DIBS_LANG); - $confirmation .= '
'; + $_POST['currency'] = get_option('rg_gforms_currency'); + $_POST['language'] = _get_language(); + $_POST['lang'] = _get_language(); + $_POST['merchant'] = trim(get_option(MERCHANT)); - $confirmation = sprintf("", $form['id']).$confirmation; + if ( $custom_merchant_id = self::checkIfCustomMerchantId($feed) ){ + $_POST['merchant'] = $custom_merchant_id; + } - _log('$confirmation'); - _log($confirmation); - } + $_POST['send_to_dibs'] = '1'; - return $confirmation; - } + if ( isset($_POST['input_9998']) ){ // input_9999 => return url + $_POST['dibs_form_id'] = $_POST['input_9998']; + } - /* - function getPlaceholders - - Placeholders: - @order_id@ - @transaction_id@ - @amount@ - @date_created@ - @ticket@ - @credit_card@ - @firstname@ - @lastname@ - @email@ - @mobile@ - @address@ - @postalcode@ - @postalplace@ - */ - - public static function getPlaceholders($Transaction, $feed) - { - $Dao = new GFDibsDao(); - - $placeholders = array(); - - // _log('get placeholders'); - // _log($Transaction); - // parse placeholders - $placeholders['order_id'] = $Transaction->order_id; - $placeholders['transaction_id'] = $Transaction->transaction_id; - $placeholders['amount'] = (int) $Transaction->amount / 100; - $placeholders['date_created'] = $Transaction->date_created; - $placeholders['ticket'] = $Transaction->ticket; - $placeholders['credit_card'] = (isset($Transaction->paytype)) ? $Transaction->paytype : null; - - $placeholders['firstname'] = null; - $placeholders['lastname'] = null; - $placeholders['email'] = null; - $placeholders['mobile'] = null; - $placeholders['address'] = null; - $placeholders['postalcode'] = null; - $placeholders['postalplace'] = null; - - // get lead values - foreach ($feed->meta as $key => $value) { - if (is_numeric(strpos($key, 'billing'))) { - if (strlen($value)) { - if ($meta_value = $Dao->getLeadMetaValue($Transaction->lead_id, $value)) { - $index = strtolower(str_replace('billing', '', $key)); - $placeholders[$index] = $meta_value; - } - } - } + if ( isset($_POST['input_9999']) ){ // input_9999 => return url + // D2 + $_POST['callbackurl'] = $_POST['input_9999']; + $_POST['accepturl'] = $_POST['input_9999']; + // DX + $_POST['callbackUrl'] = $_POST['input_9999']; + $_POST['acceptReturnUrl'] = $_POST['input_9999']; + //$Dao->log('$_POST'); + //$Dao->log($_POST); + } + + + if ( get_option(USE_MD5) ){ + $md5key = null; + $k1 = trim( get_option(MD5_K1) ); + $k2 = trim( get_option(MD5_K2) ); + + if ( $k1 && $k2 ){ + $md5_vars = "merchant=".$_POST['merchant']."&orderid=".$order_id."¤cy=".$_POST['currency']."&amount=".$_POST['amount']; + $md5key = md5( $k2.md5($k1.$md5_vars) ); } - return $placeholders; - } + $_POST['md5key'] = $md5key; + } + + + if ( $decorator = get_option(DECORATOR ) ){ + $_POST['decorator'] = $decorator; + } - public static function replacePlaceholders($message, $placeholders) - { - foreach ($placeholders as $key => $value) { - $message = str_replace('@'.$key.'@', $value, $message); + $transaction_id = $Dao->createTransaction($_POST); + $Dao->log('transaction id: '.$transaction_id); + + $confirmation = '
'; + foreach ($_POST as $key => $value) { + if ( + // !is_numeric(strpos($key, 'input')) && + !is_numeric(strpos($key, 'MAX_FILE_SIZE')) + && !is_numeric(strpos($key, 'state')) + && !is_numeric(strpos($key, 'gform')) ) + { + $confirmation .= sprintf('', $key, $key, $value ); } + } + + $confirmation .= _e('Videresending til DIBS ...', DIBS_LANG); + $confirmation .= '
'; - return $message; + $confirmation = sprintf("", $form['id'] ).$confirmation; + // _log('$confirmation'); + // _log($confirmation); } - public static function hasRedirect($form) - { - $redirect = null; - if (isset($form['confirmations']) && is_array($form['confirmations'])) { - foreach ($form['confirmations'] as $key => $confirmation) { - if (is_array($confirmation) && $confirmation['isDefault'] == '1') { - if ($confirmation['type'] == 'page') { - $redirect = get_permalink($confirmation['pageId']); - } elseif ($confirmation['type'] == 'redirect') { - $redirect = $confirmation['url']; - } - } - } + return $confirmation; + } + + + /* + function getPlaceholders + + Placeholders: + @order_id@ + @transaction_id@ + @amount@ + @date_created@ + @ticket@ + @credit_card@ + @firstname@ + @lastname@ + @email@ + @mobile@ + @address@ + @postalcode@ + @postalplace@ + */ + + public static function getPlaceholders( $Transaction, $feed){ + _log('GFDibsHooks::getPlaceholders()'); + $Dao = new GFDibsDao(); + + $placeholders = array(); + + // _log('get placeholders'); + // _log($Transaction); + // parse placeholders + $placeholders['order_id'] = $Transaction->order_id; + $placeholders['transaction_id'] = $Transaction->transaction_id; + $placeholders['amount'] = (int)$Transaction->amount/100; + $placeholders['date_created'] = $Transaction->date_created; + $placeholders['ticket'] = $Transaction->ticket; + $placeholders['credit_card'] = ( isset($Transaction->paytype) ) ? $Transaction->paytype : null; + + $placeholders['firstname'] = null; + $placeholders['lastname'] = null; + $placeholders['email'] = null; + $placeholders['mobile'] = null; + $placeholders['address'] = null; + $placeholders['postalcode'] = null; + $placeholders['postalplace'] = null; + + // get lead values + foreach ($feed->meta as $key => $value) { + if ( is_numeric(strpos($key, 'billing')) ){ + if ( strlen($value) ){ + if ( $meta_value = $Dao->getLeadMetaValue($Transaction->lead_id, $value ) ){ + $index = strtolower( str_replace('billing', '', $key) ); + $placeholders[$index] = $meta_value; + } } + } + } + + return $placeholders; + } + - return $redirect; + public static function replacePlaceholders( $message, $placeholders ){ + foreach ($placeholders as $key => $value) { + $message = str_replace('@'.$key.'@', $value, $message); } - public static function grabOrderId($post) - { - $order_id = null; - if (isset($post['orderId']) && strlen($post['orderId'])) { - $order_id = $post['orderId']; - } elseif (isset($post['orderid']) && strlen($post['orderid'])) { - $order_id = $post['orderid']; + return $message; + } + + + public static function hasRedirect($form){ + $redirect = null; + if ( isset($form['confirmations']) && is_array($form['confirmations']) ){ + foreach ($form['confirmations'] as $key => $confirmation) { + if ( is_array($confirmation) && $confirmation['isDefault'] == '1' ){ + if ( $confirmation['type'] == 'page' ){ + $redirect = get_permalink( $confirmation['pageId'] ); + } + elseif ( $confirmation['type'] == 'redirect' ){ + $redirect = $confirmation['url']; + } } + } + } + + return $redirect; + } - return $order_id; + public static function grabOrderId( $post ){ + $order_id = null; + if ( isset($post['orderId']) && strlen($post['orderId']) ){ + $order_id = $post['orderId']; + } + else if ( isset($post['orderid']) && strlen($post['orderid']) ){ + $order_id = $post['orderid']; } - public static function updateLeadAfterPayment() - { - $order_id = self::grabOrderId($_POST); - - if (isset($_POST) && $order_id && isset($_POST['dibs_form_id']) && is_numeric($_POST['dibs_form_id'])) { - _log('GF DIBS add-on'); - _log($order_id); - _log('User is coming back from DIBS'); - _log('Post variables'); - _log($_POST); - $Dao = new GFDibsDao(); - - $form = GFAPI::get_form($_POST['dibs_form_id']); - $feed_id = $Dao->isDibsForm($form['id']); - $feed = $Dao->getDibsMeta($feed_id); - - $block = false; - if (isset($_SERVER['HTTP_USER_AGENT']) && is_numeric(strpos($_SERVER['HTTP_USER_AGENT'], 'Java')) or isset($_SERVER['HTTP_X_ORIG_UA']) && is_numeric(strpos($_SERVER['HTTP_X_ORIG_UA'], 'Java'))) { - $block = true; - } + return $order_id; - if ($feed_id && $order_id && !$block) { - // update Transaction - $Dao->updateTransaction($_POST); - - // send confirmation mails - if ($Transaction = $Dao->getTransactionByOrderId($order_id)) { - _log($Transaction); - /* prod */ - if (isset($feed->meta['gf_dibs_no_confirmations']) && $feed->meta['gf_dibs_no_confirmations'] == '1' && !$Dao->getDateCompleted($Transaction->lead_id)) { - GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id); - $Dao->setDateCompleted($Transaction->lead_id); - } - /* test */ - // GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id); - // - // - - do_action('gravityformsdibs_after_payment', $order_id, $form, GFAPI::get_entry($Transaction->lead_id), $Transaction); - } - - if ($location = self::hasRedirect($form)) { - wp_redirect($location); - die(); - } - } + } + + + public static function updateLeadAfterPayment(){ + + $order_id = self::grabOrderId($_POST); + + if ( isset($_POST) && $order_id && isset($_POST['dibs_form_id']) && is_numeric($_POST['dibs_form_id']) ){ + _log('GF DIBS add-on'); + _log($order_id); + _log('User is coming back from DIBS'); + _log('Post variables'); + _log($_POST); + $Dao = new GFDibsDao(); + + $form = GFAPI::get_form( $_POST['dibs_form_id'] ); + $feed_id = $Dao->isDibsForm($form['id']); + $feed = $Dao->getDibsMeta($feed_id); + + $block = false; + if ( isset($_SERVER['HTTP_USER_AGENT']) && is_numeric(strpos($_SERVER['HTTP_USER_AGENT'], 'Java')) or isset($_SERVER['HTTP_X_ORIG_UA']) && is_numeric(strpos($_SERVER['HTTP_X_ORIG_UA'], 'Java')) ){ + $block = true; + } + + + if ( $feed_id && $order_id && !$block ){ + + // update Transaction + $Dao->updateTransaction($_POST); + + // send confirmation mails + if ( $Transaction = $Dao->getTransactionByOrderId($order_id) ){ + _log($Transaction); + /* prod */ + if ( isset($feed->meta['gf_dibs_no_confirmations']) && $feed->meta['gf_dibs_no_confirmations'] == '1' && !$Dao->getDateCompleted($Transaction->lead_id) ){ + GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id); + $Dao->setDateCompleted($Transaction->lead_id); + } + /* test */ + // GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id); + // + // + + do_action('gravityformsdibs_after_payment', $order_id, $form, GFAPI::get_entry($Transaction->lead_id), $Transaction ); } - } - public static function formTag($form_tag, $form) - { - _log('GFDibsHook::formTag()'); - _log($_POST); - - if (isset($_POST['orderId'])) { - $Dao = new GFDibsDao(); - $feed_id = $Dao->isDibsForm($form['id']); - // get feed settings - $feed = $Dao->getDibsMeta($feed_id); - - $placeholders = array(); - if ($Transaction = $Dao->getTransactionByOrderId($_POST['orderId'])) { - // get confirmation message - $form_meta = $Dao->getFormMeta($form['id']); - $message = null; - - // get and handle confirmation message - _log('lead id'); - _log(_is($_POST, 'leadId')); - - // try to get confirmation by lead - if ($lead = GFAPI::get_entry(_is($_POST, 'leadId'))) { - _log('lead'); - _log($lead); - $message = GFFormDisplay::handle_confirmation($form, $lead); - _log('$message'); - _log($message); - } - // else get default confirmation - else { - if (is_array($form_meta['confirmations'])) { - foreach ($form_meta['confirmations'] as $confirmation) { - if ($confirmation['isDefault'] == '1') { - $message = $confirmation['message']; - } - } - } - } - - // get placeholders - $placeholders = self::getPlaceholders($Transaction, $feed); - // replace placeholders - $message = self::replacePlaceholders($message, $placeholders); - - // sanitize form tag - $form_tag = preg_replace("|action='(.*?)'|", "style='display:none;'", $form_tag); - - printf('

%s

', $message); + + if ( $location = self::hasRedirect($form) ){ + wp_redirect( $location ); + die(); + } + } + } + } + + + public static function formTag($form_tag, $form){ + _log( 'GFDibsHook::formTag()'); + _log( $_POST ); + + if ( isset($_POST['orderId']) ){ + $Dao = new GFDibsDao(); + $feed_id = $Dao->isDibsForm($form['id']); + // get feed settings + $feed = $Dao->getDibsMeta($feed_id); + + $placeholders = array(); + if ( $Transaction = $Dao->getTransactionByOrderId($_POST['orderId']) ){ + // get confirmation message + $form_meta = $Dao->getFormMeta($form['id']); + $message = null; + + + // get and handle confirmation message + _log('lead id'); + _log ( _is($_POST, 'leadId' ) ); + + // try to get confirmation by lead + if ( $lead = GFAPI::get_entry( _is($_POST, 'leadId' ) ) ){ + _log('lead'); + _log($lead); + $message = GFFormDisplay::handle_confirmation( $form, $lead ); + _log('$message'); + _log($message); + } + // else get default confirmation + else{ + if ( is_array($form_meta['confirmations']) ){ + foreach ($form_meta['confirmations'] as $confirmation) { + if ( $confirmation['isDefault'] == '1'){ + $message = $confirmation['message']; + } } + } } + + // get placeholders + $placeholders = self::getPlaceholders($Transaction, $feed ); + // replace placeholders + $message = self::replacePlaceholders($message, $placeholders); - return $form_tag; + // sanitize form tag + $form_tag = preg_replace("|action='(.*?)'|", "style='display:none;'", $form_tag); + + + + printf('

%s

', $message); + } } - public static function addPaymentDetails($form, $lead) - { - $Dao = new GFDibsDao(); - $Transaction = $Dao->getTransactionByLeadId($lead['id']); + return $form_tag; + } - if ($Transaction): + + public static function addPaymentDetails($form, $lead){ + $Dao = new GFDibsDao(); + + $Transaction = $Dao->getTransactionByLeadId($lead['id']); + + if ( $Transaction ): ?> @@ -582,9 +608,7 @@ public static function addPaymentDetails($form, $lead) - + - - - - - - - - + + + + + + + + - + - - + + - +
order_id)) { - echo $Transaction->order_id; - } ?> order_id)) echo $Transaction->order_id; ?>
@@ -597,26 +621,26 @@ public static function addPaymentDetails($form, $lead)
date_created)); ?>date_created) ); ?>
@@ -627,5 +651,6 @@ public static function addPaymentDetails($form, $lead) '.print_r($message, true).''; - } else { - echo '
'.$message.'
'; - } - } +if(!function_exists('_debug')){ + function _debug($message) { + if( WP_DEBUG === true ){ + if( is_array( $message ) || is_object( $message ) ){ + echo "
" . print_r($message, true) . "
"; + } else { + echo "
" . $message . "
"; + } } + } }