forked from aubreypwd-old/gravityforms-firstdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gffd-fd.php
296 lines (249 loc) · 7.5 KB
/
gffd-fd.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
<?php
use VinceG\FirstDataApi\FirstData;
// Bypass having to send by reference
function gffd_end( $var ) {
return end( $var );
}
// Assign data no matter what is in the array.
function gffd_fd_form_info( $key, $gffd_fd_form_info ) {
if( isset( $gffd_fd_form_info[ $key ] ) ) {
return $gffd_fd_form_info[ $key ];
} else {
return false;
}
}
// Perform the purchase by sending date submitted
// by a form.
function gffd_fd_purchase_by_form(
$gffd_fd_form_info,
$echo_or_as_object=false
){
// Don't continue if the needed
// options are not setup.
if(!gffd_is_setup()){
return gffd_fd_format_error_without_instance(
__("Gateway ID or Gateway Password does not appear to be setup.")
);
}
// Get the options for the gateway id
// and password.
$gffd_fd_gateway_id=gffd_admin_get_setting('gffd_gateway_id');
$gffd_fd_gateway_pass=gffd_admin_get_setting('gffd_gateway_password');
$gffd_fd_test_mode=gffd_admin_get_setting('gffd_test_mode');
// Make sure we (bool) the DB store
// for test mode.
if($gffd_fd_test_mode=='on'){
$gffd_fd_test_mode = true;
}else{
$gffd_fd_test_mode = false;
}
// Setup the FirstData instance so we can use it
// to process the payment.
$fd_request=new FirstData(
// Gateway ID from Terminal > Settings.
$gffd_fd_gateway_id,
// Generated Password for Terminal.
$gffd_fd_gateway_pass,
// Test mode?.
$gffd_fd_test_mode
);
// Make sure there is <something> in the cc_type field.
if( ! isset( $gffd_fd_form_info['gffd_fd_cc_type'] ) ) {
$gffd_fd_form_info['gffd_fd_cc_type'] = '';
}
// Information on formatting: http://goo.gl/46V13c
$gffd_fd_info = array(
'gffd_fd_cc_type' => gffd_fd_form_info( 'gffd_fd_cc_type', $gffd_fd_form_info ),
'gffd_fd_cc_number' => gffd_fd_form_info( 'gffd_fd_cc_number', $gffd_fd_form_info ),
// Because GF credit card field only allows the name to be
// entered in "First and Last" in one input, we stored it
// in cc_firstname.
//
// Get the first name from the "John" in "John Doe".
'gffd_fd_cc_firstname' => (
current(
explode(
" ",
gffd_fd_form_info(
'gffd_fd_cc_firstname',
$gffd_fd_form_info
)
)
)
),
// Get the last name from the "Doe" in "John Doe".
'gffd_fd_cc_lastname' => (
gffd_end(
explode(
" ",
gffd_fd_form_info(
'gffd_fd_cc_firstname',
$gffd_fd_form_info
)
)
)
),
'gffd_fd_cc_exp' => gffd_fd_form_info( 'gffd_fd_cc_exp', $gffd_fd_form_info ), //mmyy
'gffd_fd_cc_amount' => gffd_fd_form_info( 'gffd_fd_cc_amount', $gffd_fd_form_info ),
'gffd_fd_cc_zip' => gffd_fd_form_info( 'gffd_fd_cc_zip', $gffd_fd_form_info ),
'gffd_fd_cc_cvv' => gffd_fd_form_info( 'gffd_fd_cc_cvv', $gffd_fd_form_info ),
// The $gffd_fd_form_info passed to this function has the
// address data segregated. Here, let's combine it the way
// First Data likes it.
'gffd_fd_cc_address'=>(
//Address
gffd_fd_form_info( 'gffd_fd_cc_address', $gffd_fd_form_info )
." ". gffd_fd_form_info( 'gffd_fd_cc_address2', $gffd_fd_form_info )
//Zip
."|".gffd_fd_form_info( 'gffd_fd_cc_zip', $gffd_fd_form_info )
//City
."|".gffd_fd_form_info( 'gffd_fd_cc_city', $gffd_fd_form_info )
//State
."|".gffd_fd_form_info( 'gffd_fd_cc_state', $gffd_fd_form_info )
//Country
."|".gffd_fd_form_info( 'gffd_fd_cc_country', $gffd_fd_form_info )
),
// Set to something at least
'gffd_fd_cc_address2' => '',
// Set the entry ID of the form
'gffd_fd_customer_ref' => gffd_fd_form_info(
'gffd_fd_customer_ref',
$gffd_fd_form_info
)
);
$purchase_action_result=
gffd_fd_perform_auth_purchase( $fd_request, $gffd_fd_info );
if($echo_or_as_object===true
|| $echo_or_as_object=='echo'
|| $echo_or_as_object=='json'
){
echo json_encode($purchase_action_result);
}elseif($echo_or_as_object=='as_original'){
return $purchase_action_result;
}else{
return json_encode($purchase_action_result);
}
}
// Perform a purchase by sending data
// via $_REQUEST.
function gffd_fd_purchase_by_request(){
$gffd_fd_purchase_by_request = new FirstData(
// Gateway ID from Terminal > Settings.
$_REQUEST['gffd_fd_gateway_id'],
// Generated Password for Terminal.
$_REQUEST['gffd_fd_gateway_pass'],
// Test mode?.
$_REQUEST['gffd_fd_gateway_test']
);
$gffd_fd_info = array(
'gffd_fd_cc_type' => $_REQUEST['gffd_fd_cc_type'],
'gffd_fd_cc_number' => $_REQUEST['gffd_fd_cc_number'],
'gffd_fd_cc_firstname' => $_REQUEST['gffd_fd_cc_firstname'],
'gffd_fd_cc_lastname' => $_REQUEST['gffd_fd_cc_lastname'],
'gffd_fd_cc_exp' => $_REQUEST['gffd_fd_cc_exp'], //mmyy
'gffd_fd_cc_amount' => $_REQUEST['gffd_fd_cc_amount'],
'gffd_fd_cc_zip' => $_REQUEST['gffd_fd_cc_zip'],
'gffd_fd_cc_cvv' => $_REQUEST['gffd_fd_cc_cvv'],
'gffd_fd_cc_address' => $_REQUEST['gffd_fd_cc_address'],
);
$result = gffd_fd_perform_auth_purchase(
$gffd_fd_purchase_by_request,
$gffd_fd_info
);
echo json_encode($result);
}
function gffd_fd_process($gffd_fd__,$trans_type,$gffd_fd_info){
$gffd_fd__->setTransactiontype(
$trans_type
)->setCreditCardType(
$gffd_fd_info['gffd_fd_cc_type']
)->setCreditCardNumber(
$gffd_fd_info['gffd_fd_cc_number']
)->setCreditCardName(
$gffd_fd_info['gffd_fd_cc_firstname']
. " "
. $gffd_fd_info['gffd_fd_cc_lastname']
)->setCreditCardExpiration(
$gffd_fd_info['gffd_fd_cc_exp']
)->setAmount(
$gffd_fd_info['gffd_fd_cc_amount']
)->setCreditCardZipCode(
$gffd_fd_info['gffd_fd_cc_zip']
)->setCreditCardVerification(
$gffd_fd_info['gffd_fd_cc_cvv']
)->setCreditCardAddress(
$gffd_fd_info['gffd_fd_cc_address']
. " "
.$gffd_fd_info['gffd_fd_cc_address2']
)->setReferenceNumber(
$gffd_fd_info['gffd_fd_customer_ref']
)->setCustomerReferenceNumber(
$gffd_fd_info['gffd_fd_customer_ref']
)->process();
return $gffd_fd__;
}
// Try both the pre-auth and the purchase.
// If the pre-auth doesn't work, just throw back the error
function gffd_fd_perform_auth_purchase($gffd_fd__, $gffd_fd_info){
// Store the transaction amount.
$transaction_amount
=$gffd_fd_info['gffd_fd_cc_amount'];
// Set the transaction amount for the pre_auth
// to 0 for the pre-auth.
$gffd_fd_info['gffd_fd_cc_amount']='0';
// Perform the pre-auth.
$gffd_fd__ = gffd_fd_process(
$gffd_fd__,
FirstData::TRAN_PREAUTHONLY,
$gffd_fd_info
);
// Set the amount back to the actual transaction amount
// for actual purchase.
$gffd_fd_info['gffd_fd_cc_amount']
=$transaction_amount;
if($gffd_fd__->isError()){
return gffd_fd_format_error($gffd_fd__, $gffd_fd_info);
}else{
$gffd_fd__ = gffd_fd_process(
$gffd_fd__,
FirstData::TRAN_PURCHASE,
$gffd_fd_info
);
if($gffd_fd__->isError()){
return gffd_fd_format_error($gffd_fd__, $gffd_fd_info);
}else{
return array(
'error'=>false,
'transaction_type'=>$gffd_fd__->getTransactionType(),
'gffd_fd_instance'=>$gffd_fd__
);
}
}
}
// DRY way of returning some useful information
// when there is an error.
function gffd_fd_format_error($gffd_fd__, $gffd_fd_info=false){
$result = array(
'error'=>true,
'transaction_type'=>$gffd_fd__->getTransactionType(),
'error_message'=>$gffd_fd__->getErrorMessage(),
'error_code'=>$gffd_fd__->getErrorCode(),
'gffd_fd_instance'=>$gffd_fd__,
'print_r'=>print_r($gffd_fd__, true),
);
if(
is_array($gffd_fd_info)
){
$result['gffd_fd_info']=$gffd_fd_info;
}
return $result;
}
// Return a formatted error without any _fd
// instances
function gffd_fd_format_error_without_instance($error_message){
return array(
'error'=>true,
'error_message'=>$error_message,
);
}
?>