Skip to content
This repository has been archived by the owner on Nov 2, 2022. It is now read-only.

Commit

Permalink
Mulighet til å videresende etter betaling
Browse files Browse the repository at this point in the history
  • Loading branch information
dmattke committed Jul 14, 2016
1 parent 5ba0948 commit 6c72913
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 37 deletions.
2 changes: 1 addition & 1 deletion GFDibsAddOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);

Expand Down
33 changes: 28 additions & 5 deletions GFDibsDao.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

add_action( 'init', array('GFDibsDao', 'updateDb') );
class GFDibsDao {
protected $dibs_table_name;
protected $gf_table_name;
Expand All @@ -18,6 +20,15 @@ function GFDibsDao(){
$this->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();
Expand Down Expand Up @@ -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
Expand All @@ -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'] ),
Expand All @@ -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);
}

Expand Down Expand Up @@ -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,
Expand Down
151 changes: 121 additions & 30 deletions GFDibsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 );
Expand All @@ -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;

Expand All @@ -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'];
Expand Down Expand Up @@ -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();
Expand All @@ -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);
?>
<div class="thank-you" style="background:#e5e4e1;padding:20px 20px;border-radius:10px;">
<p><?php echo $message; ?></p>
</div>
<?php
// sanitize form tag
$form_tag = preg_replace("|action='(.*?)'|", "style='display:none;'", $form_tag);

printf('<div class="thank-you"><p>%s</p></div>', $message);
}
}


return $form_tag;
}


public static function addPaymentDetails($form, $lead){
$Dao = new GFDibsDao();

Expand Down
2 changes: 1 addition & 1 deletion gravityformsdibs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 6c72913

Please sign in to comment.