-
Notifications
You must be signed in to change notification settings - Fork 5
/
gffd-gf-forms.php
386 lines (310 loc) · 8.88 KB
/
gffd-gf-forms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
// All the components to allow us
// to perform a purchase on a GFFD
// active form.
// Show the customer reference number on the entry
function gffd_gform_entry_detail_content_before( $form, $lead ) {
$customer_reference_number = get_option(
'gffd_fd_' . $lead['id']
);
if( $customer_reference_number ) {
?>
<table cellspacing="0" class="widefat fixed entry-detail-view">
<tbody>
<tr>
<td colspan="2" class="entry-view-field-name">
<?php _e( 'Reference Number'); ?>
</td>
</tr>
<tr>
<td class="entry-view-field-value">
<?php echo $customer_reference_number; ?>
</td>
</tr>
</tbody>
</table>
<?php
}
}
add_action(
'gform_entry_detail_content_before',
'gffd_gform_entry_detail_content_before',
10, 2
);
// Make sure, when the entry is entered,
function gffd_gform_post_submission( $entry, $form ) {
// Get the temporary stored reference number saved on FD
$customer_reference_number = get_option(
//gffd_fd_12_9.9.9.9
"gffd_fd_" . $form['id'] . "_" . $_SERVER['REMOTE_ADDR']
);
if( $customer_reference_number ) {
// Re-store this information for this lead (entry)
update_option(
'gffd_fd_' . $entry['id'],
$customer_reference_number
);
// Delete the temp store
delete_option( "gffd_fd_" . $form['id'] . "_" . $_SERVER['REMOTE_ADDR'] );
}
}
add_action(
'gform_post_submission',
'gffd_gform_post_submission',
10, 2
);
// Validate the data, reformat the data,
// try and perform the purchase and throw errors.
//
// Otherwise, do the purchase, save the reference number
// and continue to adding entry.
function gffd_validation_and_payment($validation_result){
$form_id = $validation_result['form']['id'];
$the_submitted_form = $_REQUEST; //for easier reading.
// Is this form supposed to be used by gffd?
if( gffd_feed_is_active($form_id) ){
// Validate all the fields.
foreach(
gffd_is_array(
gffd_get_purchase_field_requirements()
) as $required_field
){
$gffd_feeds_get_form_feed_settings
=gffd_feeds_get_form_feed_settings(
$form_id
);
// Get the feed index
$gf_form_feed_index
= $gffd_feeds_get_form_feed_settings
['feed_indexes'][
$required_field['gffd_index']
];
// Get the $_POST index by using value_to_post_input
// so we can pull input_3_4 in GF style
$gffd_convert_float_value_to_post_input=
gffd_convert_float_value_to_post_input($gf_form_feed_index);
// In the case when a required field is removed,
// we need to test for it's value in $_POST.
//
// Test $required_field for it's $_POST
// counterpart.
if(
isset(
$the_submitted_form[
$gffd_convert_float_value_to_post_input
]
)
){
$gffd_is_valid_pre_format_feed_data=
gffd_is_valid_pre_format_feed_data(
// Tell is_valid, where to find the function
$required_field['gffd_index'],
// Pull the value from $_POST
$the_submitted_form[
$gffd_convert_float_value_to_post_input
]
);
// If we have a good result, set the value to pass
// to the purchasing functions.
if($gffd_is_valid_pre_format_feed_data){
$gffd_fd_form_info[
$required_field[
'gffd_index'
]
] = $gffd_is_valid_pre_format_feed_data;
// If even one of the items fail, jump out,
// thrown an error and try again.
}else{
// Fail the form.
$validation_result["is_valid"]=false;
$validation_result["form"]=gffd_get_form(
$form_id
);
// Let's try and find out what failed by going through the
// form's fields.
foreach($validation_result['form']['fields'] as &$field){
if($field['id']==floor($gf_form_feed_index)){
$field["failed_validation"]=true;
$field["validation_message"]
=gffd_get_validation_message_feed_data(
$required_field['gffd_index']
);
}
}
// Let's not return the validation result here,
// instead let's test for it after it's added
// messages to all fields.
}
// Okay, one of the fields were missing.
}else{
// If a field is missing, let's just do nothing and submit
// the form. But, we will at least email the admin about it,
// because we did not do the purchase.
}
} //end foreach
// If something did not validate, throw the error
// back to GF.
if($validation_result["is_valid"]==false){
return $validation_result;
// If everything validated, let's try
// and perform the purchase.
}else{
// Set the customer reference number on FD
$gffd_fd_form_info['gffd_fd_customer_ref']
= gffd_fd_customer_ref( $form_id );
// Save the reference number to the DB temporarily so we can
// get it later.
update_option(
// Save gffd_fd_form_#_#.#.#
// form_id shoudn't change: gffd_fd_12
"gffd_fd_$form_id"
// Remote IP shouldn't change:
// When the entry is added, we want to look for
//
// gffd_fd_12_9.9.9.9
//
. "_" . $_SERVER['REMOTE_ADDR'],
// Save the reference number we saved in FD trasnsaction log
$gffd_fd_form_info['gffd_fd_customer_ref']
);
// Run a purchase by form
$result = gffd_fd_purchase_by_form(
$gffd_fd_form_info,
'as_original'
);
if(
// If we have results
gffd_is_array($result)
// If there isn't an error message
&& ! $result['error_message']
// And, we haven't set debug mode in wp-config.php
&& ! defined('GFFD_DEBUG_FORM_SUBMIT')
// And there was some sort of error
&& $result['error']
){
// Just show a simple error.
wp_die( __(
"There was an error, but FirstData didn't
send back an error message."
) );
// Want to see what the error is? Just set
// define('GFFD_DEBUG_FORM_SUBMIT', true);
// in wp-config.php and try again
} else {
// To debug just use define('GFFD_DEBUG_FORM_SUBMIT', true);
// in wp-config.php
if(defined('GFFD_DEBUG_FORM_SUBMIT')){
echo "<pre>";
var_dump(
//$result['gffd_fd_instance']->getBankResponseMessage()
$result
);
echo "</pre>";
exit;
// If the dev doesn't want to GFFD_DEBUG_FORM_SUBMIT
}else{
// If there was an error, and we've submitted
// the data for purchase, let's see what the
// bank or FirstData class said.
if($result['error']===true){
// Don't submit the entry.
$validation_result["is_valid"]=false;
// If there is a bank response, let's
// pass that to the CC field
if($result['gffd_fd_instance']->getBankResponseMessage()){
$validation_result
=gffd_wp_die_by_fd_response(
$result,
'getBankResponseMessage'
);
}elseif($result['gffd_fd_instance']->getErrorMessage()){
// Try and see if there is just a generic
// error message.
$validation_result
=gffd_wp_die_by_fd_response(
$result,
'getErrorMessage'
);
}
return $validation_result;
// If there was no bank response, but an error,
// we don't know what is wrong!
}else{
// Throw a general error message.
foreach($validation_result['form']['fields'] as &$field){
if($field['type']=='creditcard'){
// Tell GF what field failed. Here,
// always the CC.
$field["failed_validation"]=true;
// Pass the bank response back to
// GF validation_message.
$field["validation_message"]
= __(
"Sorry, but there was an error with the"
."information provided. Please correct and try again. "
."If this persists, contact the site owner."
);
}
}
return $validation_result;
}
}
}
}
}else{
// If the form is not a form with a feed, just ignore
// and continue.
return $validation_result;
}
}
add_filter('gform_validation','gffd_validation_and_payment');
function gffd_wp_die_by_fd_response($result, $kind){
if($kind=='getBankResponseMessage'){
$message=$result['gffd_fd_instance']
->getBankResponseMessage();
}elseif($kind="getErrorMessage"){
$message=$result['gffd_fd_instance']
->getErrorMessage();
}
wp_die(
"<h1>".$message."</h1>"
."<br><br>"
.__(
"Please go back and try again.<br><br>"
."If this persists, you can contact the site owner by "
."emailing "
)
."<a href='mailto:".get_option('admin_email')."'>"
.get_option('admin_email')
."</a>"
);
}
// Enqueue a script so we can watch for changes in
// #cctype and make it an input value.
function gffd_gf_forms_js(){
wp_enqueue_script(
'gffd-gf-forms-js',
plugins_url(
'gffd-gf-forms.js',
___GFFDFILE___
),
array(),
'',
false
);
}
add_action('gform_enqueue_scripts','gffd_gf_forms_js');
// Generate a reference number for FD
function gffd_fd_customer_ref( $form_id ) {
return substr(
// Nice hash based on form id and the user's IP.
md5(
$form_id
. $_SERVER['REMOTE_ADDR']
. time()
),
// Only 8 chars
0, 8
);
}
?>