From 6c72913b08994d7de3f314e88d6071f0de1ba7d0 Mon Sep 17 00:00:00 2001 From: dmattke Date: Thu, 14 Jul 2016 12:10:05 +0200 Subject: [PATCH] =?UTF-8?q?Mulighet=20til=20=C3=A5=20videresende=20etter?= =?UTF-8?q?=20betaling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GFDibsAddOn.php | 2 +- GFDibsDao.php | 33 ++++++++-- GFDibsHook.php | 151 ++++++++++++++++++++++++++++++++++--------- gravityformsdibs.php | 2 +- 4 files changed, 151 insertions(+), 37 deletions(-) diff --git a/GFDibsAddOn.php b/GFDibsAddOn.php index c9cea92..5d7b47a 100644 --- a/GFDibsAddOn.php +++ b/GFDibsAddOn.php @@ -66,7 +66,7 @@ public static function pluginSettingsFields(){ array( DIBS_POST_URL => __("Platform", DIBS_LANG), MERCHANT => __("Merchant ID", DIBS_LANG), - ORDER_ID_SUFFIX => __("Suffix (order id)", DIBS_LANG), + ORDER_ID_SUFFIX => __("Prefix (order id)", DIBS_LANG), LICENCE => __("Licence key", DIBS_LANG), ); diff --git a/GFDibsDao.php b/GFDibsDao.php index 01131f6..fec0f09 100644 --- a/GFDibsDao.php +++ b/GFDibsDao.php @@ -1,4 +1,6 @@ gf_table_name = self::getDibsTransactionTableName(); } + + public static function updateDb(){ + global $wpdb; + $sql = sprintf("ALTER TABLE %s MODIFY COLUMN order_id varchar(50) NOT NULL", $wpdb->prefix.'rg_dibs_transaction' ) ; + + $wpdb->query($sql); + } + + function getAvailableForms( $current_form_id = null){ $forms = RGFormsModel::get_forms(); $available_forms = array(); @@ -52,7 +63,7 @@ function createTransaction( $post ){ 'date_created' => date('Y-m-d H:i:s') ), array( - '%d', // order_id + '%s', // order_id '%d', // lead_id '%s', // payment_type '%d', // test @@ -65,11 +76,21 @@ function createTransaction( $post ){ } function updateTransaction( $post ){ + // _log('GFDibsDao::updateTransaction'); + // _log($post); + + if ( isset( $post['transaction']) ){ + $transaction_id = $post['transaction']; + } + elseif ( isset( $post['transact']) ){ + $transaction_id = $post['transact']; + } + $this->db->update( $this->gf_table_name, array( 'completed' => 1, - 'transaction_id' => ( isset($post['transaction']) ) ? $post['transaction'] : false, + 'transaction_id' => $transaction_id, 'ticket' => ( isset($post['ticket']) ) ? $post['ticket'] : false, ), array( 'order_id' => $post['orderId'] ), @@ -83,11 +104,13 @@ function updateTransaction( $post ){ 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); } function getTransactionByOrderId($order_id){ - $sql = sprintf('SELECT * FROM %s where order_id = %d', $this->gf_table_name, $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); } @@ -189,9 +212,9 @@ function setupTables(){ // error_log($sql); $transaction_table = self::getDibsTransactionTableName(); - $sql = "CREATE TABLE IF NOT EXISTS $transaction_table ( + $sql = "CREATE TABLE IF NOT EXISTS $transaction_table ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, - `order_id` int(15) unsigned NOT NULL, + `order_id` varchar(50) NOT NULL, `completed` int(1) DEFAULT NULL, `transaction_id` varchar(50) DEFAULT NULL, `payment_type` varchar(50) DEFAULT NULL, diff --git a/GFDibsHook.php b/GFDibsHook.php index 9e6cb50..7c58893 100755 --- a/GFDibsHook.php +++ b/GFDibsHook.php @@ -2,7 +2,8 @@ add_filter("gform_pre_render", array("GFDibsHook", "preRenderForm")); // hidden input with return url add_action("gform_confirmation", array("GFDibsHook", "dibsTransition"), 10, 4 ); // before payment on DIBS add_filter("gform_disable_notification", array("GFDibsHook", 'disableNotifications'), 10, 4); // disabled notification on submit -add_filter("gform_form_tag", array("GFDibsHook", "formTag"), 10, 2); // after payment +add_filter("gform_form_tag", array("GFDibsHook", "formTag"), 10, 2); // shows confirmation message after payment +add_action("init", array("GFDibsHook", "updateLeadAfterPayment"), 10, 2); // checks if payment received add_filter("gform_pre_send_email", array("GFDibsHook", "parseNotification"), 10, 1); // send notification mail after payment add_filter("gform_entries_column_filter", array("GFDibsHook" , "changeColumnData") , 10, 5); // gravity forms backend add_action("gform_entry_detail", array("GFDibsHook", 'addPaymentDetails'), 10, 2); // gravity forms back end @@ -52,6 +53,22 @@ public static function preRenderForm($form) { // $feed = $Dao->getDibsMeta($feed_id); + // hidden input form id + $Field = new GF_Field_Hidden(); + + $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'; @@ -218,6 +235,7 @@ public static function dibsTransition($confirmation, $form, $lead, $ajax){ 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 ); @@ -239,7 +257,12 @@ public static function dibsTransition($confirmation, $form, $lead, $ajax){ // $_POST['orderId'] = hexdec(uniqid()); $_POST['leadId'] = $lead['id']; - $order_id = uniqid(get_option(ORDER_ID_SUFFIX)."_"); + $order_id = uniqid(); + + if ( $suffix = get_option(ORDER_ID_SUFFIX) ){ + $order_id = $suffix.'_'.$order_id; + } + // dx $_POST['orderId'] = $order_id; @@ -258,6 +281,10 @@ public static function dibsTransition($confirmation, $form, $lead, $ajax){ $_POST['send_to_dibs'] = '1'; + if ( isset($_POST['input_9998']) ){ // input_9999 => return url + $_POST['dibs_form_id'] = $_POST['input_9998']; + } + if ( isset($_POST['input_9999']) ){ // input_9999 => return url // D2 $_POST['callbackurl'] = $_POST['input_9999']; @@ -354,28 +381,101 @@ public static function replacePlaceholders( $message, $placeholders ){ return $message; } - public static function formTag($form_tag, $form){ - $Dao = new GFDibsDao(); - if ( isset($_POST) && count($_POST) ){ + 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; + } + + 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']; + } + + return $order_id; + + } + + + 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; + } - $feed_id = $Dao->isDibsForm($form['id']); - $block = false; - // $Dao->log($_SERVER); - 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 + _log('confm'); + _log($order_id); + 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); + // + } + + + if ( $location = self::hasRedirect($form) ){ + wp_redirect( $location ); + die(); + } + } } + } + + + - if ( $feed_id && isset($_POST['orderId']) & !$block ){ - // update Transaction - $Dao->updateTransaction($_POST); + + public static function formTag($form_tag, $form){ + + if ( isset($_POST['orderId']) ){ + $Dao = new GFDibsDao(); + $feed_id = $Dao->isDibsForm($form['id']); // get feed settings + $feed = $Dao->getDibsMeta($feed_id); $placeholders = array(); @@ -392,33 +492,24 @@ public static function formTag($form_tag, $form){ } } - /* 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); - // 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); - ?> -
-

-
-

%s

', $message); + } } + return $form_tag; } + public static function addPaymentDetails($form, $lead){ $Dao = new GFDibsDao(); diff --git a/gravityformsdibs.php b/gravityformsdibs.php index 4758a89..38371a3 100755 --- a/gravityformsdibs.php +++ b/gravityformsdibs.php @@ -3,7 +3,7 @@ Plugin Name: Gravity Forms DIBS Plugin URI: http://nettbutikk.mediebruket.no Description: DIBS add-on for Gravity Forms. Supports D2 and DX platform. - Version: 1.2.0 + Version: 1.2.1 Author: Mediebruket Author URI: http://mediebruket.no */