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

Commit 6c72913

Browse files
committed
Mulighet til å videresende etter betaling
1 parent 5ba0948 commit 6c72913

File tree

4 files changed

+151
-37
lines changed

4 files changed

+151
-37
lines changed

GFDibsAddOn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function pluginSettingsFields(){
6666
array(
6767
DIBS_POST_URL => __("Platform", DIBS_LANG),
6868
MERCHANT => __("Merchant ID", DIBS_LANG),
69-
ORDER_ID_SUFFIX => __("Suffix (order id)", DIBS_LANG),
69+
ORDER_ID_SUFFIX => __("Prefix (order id)", DIBS_LANG),
7070
LICENCE => __("Licence key", DIBS_LANG),
7171
);
7272

GFDibsDao.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
add_action( 'init', array('GFDibsDao', 'updateDb') );
24
class GFDibsDao {
35
protected $dibs_table_name;
46
protected $gf_table_name;
@@ -18,6 +20,15 @@ function GFDibsDao(){
1820
$this->gf_table_name = self::getDibsTransactionTableName();
1921
}
2022

23+
24+
public static function updateDb(){
25+
global $wpdb;
26+
$sql = sprintf("ALTER TABLE %s MODIFY COLUMN order_id varchar(50) NOT NULL", $wpdb->prefix.'rg_dibs_transaction' ) ;
27+
28+
$wpdb->query($sql);
29+
}
30+
31+
2132
function getAvailableForms( $current_form_id = null){
2233
$forms = RGFormsModel::get_forms();
2334
$available_forms = array();
@@ -52,7 +63,7 @@ function createTransaction( $post ){
5263
'date_created' => date('Y-m-d H:i:s')
5364
),
5465
array(
55-
'%d', // order_id
66+
'%s', // order_id
5667
'%d', // lead_id
5768
'%s', // payment_type
5869
'%d', // test
@@ -65,11 +76,21 @@ function createTransaction( $post ){
6576
}
6677

6778
function updateTransaction( $post ){
79+
// _log('GFDibsDao::updateTransaction');
80+
// _log($post);
81+
82+
if ( isset( $post['transaction']) ){
83+
$transaction_id = $post['transaction'];
84+
}
85+
elseif ( isset( $post['transact']) ){
86+
$transaction_id = $post['transact'];
87+
}
88+
6889
$this->db->update(
6990
$this->gf_table_name,
7091
array(
7192
'completed' => 1,
72-
'transaction_id' => ( isset($post['transaction']) ) ? $post['transaction'] : false,
93+
'transaction_id' => $transaction_id,
7394
'ticket' => ( isset($post['ticket']) ) ? $post['ticket'] : false,
7495
),
7596
array( 'order_id' => $post['orderId'] ),
@@ -83,11 +104,13 @@ function updateTransaction( $post ){
83104

84105
function getTransactionByLeadId($lead_id){
85106
$sql = sprintf('SELECT * FROM %s where lead_id = %d', $this->gf_table_name, $lead_id);
107+
// _log($sql);
86108
return $this->db->get_row($sql);
87109
}
88110

89111
function getTransactionByOrderId($order_id){
90-
$sql = sprintf('SELECT * FROM %s where order_id = %d', $this->gf_table_name, $order_id);
112+
$sql = sprintf("SELECT * FROM %s where order_id = '%s'", $this->gf_table_name, $order_id);
113+
// _log($sql);
91114
return $this->db->get_row($sql);
92115
}
93116

@@ -189,9 +212,9 @@ function setupTables(){
189212
// error_log($sql);
190213

191214
$transaction_table = self::getDibsTransactionTableName();
192-
$sql = "CREATE TABLE IF NOT EXISTS $transaction_table (
215+
$sql = "CREATE TABLE IF NOT EXISTS $transaction_table (
193216
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
194-
`order_id` int(15) unsigned NOT NULL,
217+
`order_id` varchar(50) NOT NULL,
195218
`completed` int(1) DEFAULT NULL,
196219
`transaction_id` varchar(50) DEFAULT NULL,
197220
`payment_type` varchar(50) DEFAULT NULL,

GFDibsHook.php

Lines changed: 121 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
add_filter("gform_pre_render", array("GFDibsHook", "preRenderForm")); // hidden input with return url
33
add_action("gform_confirmation", array("GFDibsHook", "dibsTransition"), 10, 4 ); // before payment on DIBS
44
add_filter("gform_disable_notification", array("GFDibsHook", 'disableNotifications'), 10, 4); // disabled notification on submit
5-
add_filter("gform_form_tag", array("GFDibsHook", "formTag"), 10, 2); // after payment
5+
add_filter("gform_form_tag", array("GFDibsHook", "formTag"), 10, 2); // shows confirmation message after payment
6+
add_action("init", array("GFDibsHook", "updateLeadAfterPayment"), 10, 2); // checks if payment received
67
add_filter("gform_pre_send_email", array("GFDibsHook", "parseNotification"), 10, 1); // send notification mail after payment
78
add_filter("gform_entries_column_filter", array("GFDibsHook" , "changeColumnData") , 10, 5); // gravity forms backend
89
add_action("gform_entry_detail", array("GFDibsHook", 'addPaymentDetails'), 10, 2); // gravity forms back end
@@ -52,6 +53,22 @@ public static function preRenderForm($form) {
5253

5354
// $feed = $Dao->getDibsMeta($feed_id);
5455

56+
// hidden input form id
57+
$Field = new GF_Field_Hidden();
58+
59+
$Field->type = 'hidden';
60+
$Field->label = 'dibs_form_id';
61+
$Field->pageNumber = 1;
62+
$Field->formId = $form['id'];
63+
64+
$Field->id = 9998;
65+
$Field->inputName = 'dibs_form_id';
66+
$Field->defaultValue = $form['id'];
67+
68+
array_push($form['fields'], $Field);
69+
70+
71+
// hidden input return url
5572
$Field = new GF_Field_Hidden();
5673

5774
$Field->type = 'hidden';
@@ -218,6 +235,7 @@ public static function dibsTransition($confirmation, $form, $lead, $ajax){
218235
unset($feed->meta['gf_dibs_type']);
219236
unset($feed->meta['gf_dibs_mode']);
220237

238+
221239
foreach ($feed->meta as $key => $value) {
222240
$value = str_replace('.', '_', $value);
223241
//$Dao->log( $key );
@@ -239,7 +257,12 @@ public static function dibsTransition($confirmation, $form, $lead, $ajax){
239257
// $_POST['orderId'] = hexdec(uniqid());
240258
$_POST['leadId'] = $lead['id'];
241259

242-
$order_id = uniqid(get_option(ORDER_ID_SUFFIX)."_");
260+
$order_id = uniqid();
261+
262+
if ( $suffix = get_option(ORDER_ID_SUFFIX) ){
263+
$order_id = $suffix.'_'.$order_id;
264+
}
265+
243266
// dx
244267
$_POST['orderId'] = $order_id;
245268

@@ -258,6 +281,10 @@ public static function dibsTransition($confirmation, $form, $lead, $ajax){
258281
$_POST['send_to_dibs'] = '1';
259282

260283

284+
if ( isset($_POST['input_9998']) ){ // input_9999 => return url
285+
$_POST['dibs_form_id'] = $_POST['input_9998'];
286+
}
287+
261288
if ( isset($_POST['input_9999']) ){ // input_9999 => return url
262289
// D2
263290
$_POST['callbackurl'] = $_POST['input_9999'];
@@ -354,28 +381,101 @@ public static function replacePlaceholders( $message, $placeholders ){
354381
return $message;
355382
}
356383

357-
public static function formTag($form_tag, $form){
358-
$Dao = new GFDibsDao();
359384

360-
if ( isset($_POST) && count($_POST) ){
385+
public static function hasRedirect($form){
386+
$redirect = null;
387+
if ( isset($form['confirmations']) && is_array($form['confirmations']) ){
388+
foreach ($form['confirmations'] as $key => $confirmation) {
389+
if ( is_array($confirmation) && $confirmation['isDefault'] == '1' ){
390+
if ( $confirmation['type'] == 'page' ){
391+
$redirect = get_permalink( $confirmation['pageId'] );
392+
}
393+
elseif ( $confirmation['type'] == 'redirect' ){
394+
$redirect = $confirmation['url'];
395+
}
396+
}
397+
}
398+
}
399+
400+
return $redirect;
401+
}
402+
403+
public static function grabOrderId( $post ){
404+
$order_id = null;
405+
if ( isset($post['orderId']) && strlen($post['orderId']) ){
406+
$order_id = $post['orderId'];
407+
}
408+
else if ( isset($post['orderid']) && strlen($post['orderid']) ){
409+
$order_id = $post['orderid'];
410+
}
411+
412+
return $order_id;
413+
414+
}
415+
416+
417+
public static function updateLeadAfterPayment(){
418+
419+
$order_id = self::grabOrderId($_POST);
420+
421+
if ( isset($_POST) && $order_id && isset($_POST['dibs_form_id']) && is_numeric($_POST['dibs_form_id']) ){
361422
_log('GF DIBS add-on');
423+
_log($order_id);
362424
_log('User is coming back from DIBS');
363425
_log('Post variables');
364426
_log($_POST);
365-
}
427+
$Dao = new GFDibsDao();
428+
429+
$form = GFAPI::get_form( $_POST['dibs_form_id'] );
430+
$feed_id = $Dao->isDibsForm($form['id']);
431+
$feed = $Dao->getDibsMeta($feed_id);
432+
433+
$block = false;
434+
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')) ){
435+
$block = true;
436+
}
366437

367-
$feed_id = $Dao->isDibsForm($form['id']);
368438

369-
$block = false;
370-
// $Dao->log($_SERVER);
371-
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')) ){
372-
$block = true;
439+
if ( $feed_id && $order_id && !$block ){
440+
441+
// update Transaction
442+
$Dao->updateTransaction($_POST);
443+
444+
// send confirmation mails
445+
_log('confm');
446+
_log($order_id);
447+
if ( $Transaction = $Dao->getTransactionByOrderId($order_id) ){
448+
_log($Transaction);
449+
/* prod */
450+
if ( isset($feed->meta['gf_dibs_no_confirmations']) && $feed->meta['gf_dibs_no_confirmations'] == '1' && !$Dao->getDateCompleted($Transaction->lead_id) ){
451+
GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id);
452+
$Dao->setDateCompleted($Transaction->lead_id);
453+
}
454+
/* test */
455+
// GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id);
456+
//
457+
}
458+
459+
460+
if ( $location = self::hasRedirect($form) ){
461+
wp_redirect( $location );
462+
die();
463+
}
464+
}
373465
}
466+
}
467+
468+
469+
374470

375-
if ( $feed_id && isset($_POST['orderId']) & !$block ){
376-
// update Transaction
377-
$Dao->updateTransaction($_POST);
471+
472+
public static function formTag($form_tag, $form){
473+
474+
if ( isset($_POST['orderId']) ){
475+
$Dao = new GFDibsDao();
476+
$feed_id = $Dao->isDibsForm($form['id']);
378477
// get feed settings
478+
379479
$feed = $Dao->getDibsMeta($feed_id);
380480

381481
$placeholders = array();
@@ -392,33 +492,24 @@ public static function formTag($form_tag, $form){
392492
}
393493
}
394494

395-
/* prod */
396-
if ( isset($feed->meta['gf_dibs_no_confirmations']) && $feed->meta['gf_dibs_no_confirmations'] == '1' && !$Dao->getDateCompleted($Transaction->lead_id) ){
397-
GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id);
398-
$Dao->setDateCompleted($Transaction->lead_id);
399-
}
400-
/* test */
401-
// GFDibsAddOn::sendNotification('form_submission', $form, $Transaction->lead_id);
402-
403495
// get placeholders
404496
$placeholders = self::getPlaceholders($Transaction, $feed );
405497

406498
// replace placeholders
407499
$message = self::replacePlaceholders($message, $placeholders);
408-
}
409500

410-
// sanitize form tag
411-
$form_tag = preg_replace("|action='(.*?)'|", "style='display:none;'", $form_tag);
412-
?>
413-
<div class="thank-you" style="background:#e5e4e1;padding:20px 20px;border-radius:10px;">
414-
<p><?php echo $message; ?></p>
415-
</div>
416-
<?php
501+
// sanitize form tag
502+
$form_tag = preg_replace("|action='(.*?)'|", "style='display:none;'", $form_tag);
503+
504+
printf('<div class="thank-you"><p>%s</p></div>', $message);
505+
}
417506
}
418507

508+
419509
return $form_tag;
420510
}
421511

512+
422513
public static function addPaymentDetails($form, $lead){
423514
$Dao = new GFDibsDao();
424515

gravityformsdibs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Gravity Forms DIBS
44
Plugin URI: http://nettbutikk.mediebruket.no
55
Description: DIBS add-on for Gravity Forms. Supports D2 and DX platform.
6-
Version: 1.2.0
6+
Version: 1.2.1
77
Author: Mediebruket
88
Author URI: http://mediebruket.no
99
*/

0 commit comments

Comments
 (0)