-
Notifications
You must be signed in to change notification settings - Fork 3
/
gpCredomatic.php
308 lines (249 loc) · 10.9 KB
/
gpCredomatic.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
<?php
/*
* Credomatic Payment Gateway for WP-e-Commerce
* Copyright, 2010 - Sebastian Oliva
* http://sebastianoliva.com
*
* Made at the request of http://royalestudios.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*/
$nzshpcrt_gateways[$num]['name'] = 'Pago Credomatic';
$nzshpcrt_gateways[$num]['admin_name'] = 'Pago Credomatic / GatewayCredomatic';
$nzshpcrt_gateways[$num]['internalname'] = 'pagoCredomatic';
$nzshpcrt_gateways[$num]['function'] = 'gateway_pagoCredomatic';
$nzshpcrt_gateways[$num]['form'] = "form_pagoCredomatic";
$nzshpcrt_gateways[$num]['submit_function'] = "submit_pagoCredomatic";
if(in_array('pagoCredomatic',(array)get_option('custom_gateway_options'))) {
//Generating the possible expire years for the card
$curryear = date('y');
$years = "";
for($i=0; $i < 10; $i++){
$years .= "<option value='".$curryear."'>20".$curryear."</option>\r\n";
$curryear++;
}
$gateway_checkout_form_fields[$nzshpcrt_gateways[$num]['internalname']] = "
<tr id='wpsc_gpc_cc_number'>
<td class='wpsc_gpc_cc_number1'>Numero de Tarjeta: *</td>
<td class='wpsc_gpc_cc_number2'>
<input type='text' value='' name='card_number' />
</td>
</tr>
<tr id='wpsc_gpc_cc_expiry'>
<td class='wpsc_gpc_cc_expiry1'>Fecha de Expiracion: *</td>
<td class='wpsc_gpc_cc_expiry2'>
<select class='wpsc_ccBox' name='expiry[month]'>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
</select>
<select class='wpsc_ccBox' name='expiry[year]'>
".$years."
</select>
</td>
</tr>
<tr id='wpsc_gpc_cc_cvv'>
<td class='wpsc_gpc_cc_cvv1'>Numero de Verificación: *</td>
<td class='wpsc_gpc_cc_cvv2'>
<input type='text' value='' name='cvv' />
</td>
</tr>
";
}
function gateway_pagoCredomatic($seperator, $sessionid){
global $wpdb, $wpsc_cart;
$_POST['card_number'] = trim($_POST['card_number']);
//TODO: Void purchases with 999 and other well known numbers
//Adding extra protection against SQL Injection and killing invalid numbers
if( !is_numeric($_POST['card_number']) ||
!is_numeric($_POST['expiry']['year']) ||
!is_numeric($_POST['expiry']['month'])||
!is_numeric($_POST['cvv']) ||
$_POST['card_number'] == 1234567890 ||
$_POST['card_number'] == 0987654321 ||
$_POST['card_number'] == "" ||
$_POST['cvv'] == "" ||
$_POST['expiry']['month'] < date('m')
)
{
$sql = "DELETE from `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid` = ".$sessionid." LIMIT 1"; //Deleting the invalid transaction
$wpdb->query($sql);
$transact_url = get_option('checkout_url');
$_SESSION['wpsc_checkout_misc_error_messages'][] = __('La operación fue fallida, entrada Inválida ');
$_SESSION['gpc'] = 'fail';
header("Location: ".get_option('transact_url').$seperator."sessionid=".$sessionid);
exit();
}
$purchase_log = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid`= ".$sessionid." LIMIT 1",ARRAY_A) ;
$usersql = "SELECT `".WPSC_TABLE_SUBMITED_FORM_DATA."`.value, `".WPSC_TABLE_CHECKOUT_FORMS."`.`name`, `".WPSC_TABLE_CHECKOUT_FORMS."`.`unique_name` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` LEFT JOIN `".WPSC_TABLE_SUBMITED_FORM_DATA."` ON `".WPSC_TABLE_CHECKOUT_FORMS."`.id = `".WPSC_TABLE_SUBMITED_FORM_DATA."`.`form_id` WHERE `".WPSC_TABLE_SUBMITED_FORM_DATA."`.`log_id`=".$purchase_log['id']." ORDER BY `".WPSC_TABLE_CHECKOUT_FORMS."`.`order`";
//exit($usersql);
$userinfo = $wpdb->get_results($usersql, ARRAY_A);
//exit('<pre>'.print_r($userinfo, true).'</pre>');
//
$POSTURL = "https://credomatic.compassmerchantsolutions.com/api/transact.php";
//$POSTURL = "http://localhost:4567/api/transact.php"; //Testing Gateway included as dummyServerTesting.rb
$MODE_FOPEN = false;
//Preparing the data to be sent
$GC_purchaseData = array();
$mT_keyID = get_option("mT_keyID"); //the key ID of the merchant
$mT_key = stripslashes(get_option('mT_key')); //key of the merchant
$t_time = time();
foreach((array)$userinfo as $key => $value){
if(($value['unique_name']=='billingfirstname') && $value['value'] != ''){
$GC_purchaseData["firstname"] = $value['value'];
}
if(($value['unique_name']=='billinglastname') && $value['value'] != ''){
$GC_purchaseData['lastname'] = $value['value'];
}
if(($value['unique_name']=='billingemail') && $value['value'] != ''){
$GC_purchaseData["email"] = $value['value'];
}
}
$t_item = "";
$t_amount = 0;
//calculating the total to be charged
foreach($wpsc_cart->cart_items as $i => $Item) {
$t_item .= $Item->product_name." ";
$t_amount = $t_amount + number_format($Item->unit_price,2) * $Item->quantity;
}
$t_item = htmlspecialchars($t_item);
$t_ccNumber = $_POST['card_number'];
$t_ccExp = $_POST['expiry']['month'].$_POST['expiry']['year'];
$t_ccCvv =$_POST['cvv'];
$t_hash = md5("$t_item|$t_amount|$t_time|$mT_key");
//Filling the arguments array
$GC_purchaseData["type"] = "sale";
$GC_purchaseData["key_id"] = $mT_keyID;
$GC_purchaseData["time"] = $t_time;
$GC_purchaseData["redirect"] = "http://localhost"; //get_option('mT_REDIRECTURL');
$GC_purchaseData["orderid"] = $t_item;
$GC_purchaseData["amount"] = $t_amount;
//TODO: add more fields
$GC_purchaseData["ccnumber"] = $t_ccNumber;
$GC_purchaseData["ccexp"] = $t_ccExp;
$GC_purchaseData["cvv"] = $t_ccCvv;
$GC_purchaseData["hash"] = $t_hash;
//Creating the query string with the data
$t_constructedArgs = http_build_query($GC_purchaseData);
$GC_resultVal = false;
if ($MODE_FOPEN){ //FOPEN causes problems sometimes
//http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl
//print_r($t_constructedArgs);
$params = array('http' => array(
'method' => 'POST',
'content' => $t_constructedArgs
));
$context = stream_context_create($params);
$stream = fopen($POSTURL, 'r', false, $context); //making the actual request
$resultMetaData = stream_get_meta_data($stream);
fclose($stream);
$results = $resultMetaData["wrapper_data"];
$GC_resultVal = ""; //here we store the results
foreach ($results as $element){
$splitArrays = explode (": ", $element); //we separate the HTTP Header in sections
//if the selected header is Location we store the results
if ($splitArrays[0] == "Location"){
$GC_resultVal = $splitArrays[1];
break;
}
}
//end of MODE_FOPEN
} else { // In case MODE_FOPEN is false, we use curl instead
$ch = curl_init ($POSTURL);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $t_constructedArgs);
curl_setopt($ch, CURLOPT_USERAGENT, "WP e-Commerce");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$returndata = curl_exec ($ch);
$returnLines = explode("\n", $returndata);
foreach ($returnLines as $element){
$splitArrays = explode (": ", $element); //we separate the HTTP Header in sections
//if the selected header is Location we store the results
if ($splitArrays[0] == "Location"){
$GC_resultVal = $splitArrays[1];
break;
}
}
}
if ($GC_resultVal){ //protection in case of a failed request
parse_str($GC_resultVal, $GC_ResultData);
$serverResponse = $GC_ResultData["http://localhost?response"]; //$GC_ResultData[get_option('mT_REDIRECTURL')."?response"]; //didn't actually work
if($serverResponse == 1 && $GC_resultVal != ""){
//redirect to transaction page and store in DB as a order with accepted payment
$sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` set `processed` = 2 WHERE `sessionid` = ".$sessionid." LIMIT 1";
$wpdb->query($sql);
if(function_exists('wpsc_member_activate_subscriptions')){ //This bit here enables the auto aproval of the transaction
wpsc_member_activate_subscriptions($sessionid);
}
$transact_url = get_option('transact_url');
unset($_SESSION['WpscGatewayErrorMessage']);
$_SESSION['gpc'] = 'success';
header("Location: ".get_option('transact_url').$seperator."sessionid=".$sessionid);
exit();
}
else {
//redirect back to checkout page with errors
//$sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` set `processed` = '5' WHERE `sessionid` = ".$sessionid." LIMIT 1";
$sql = "DELETE from `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid` = ".$sessionid." LIMIT 1"; //Deleting the invalid transaction
$wpdb->query($sql);
$transact_url = get_option('checkout_url');
//if ($GC_ResultData["responsecode"] > 199 && $GC_ResultData["responsecode"] < 300)
$_SESSION['wpsc_checkout_misc_error_messages'][] = __('La operacion fue fallida, ningun cargo ha sido efectuado a su tarjeta. ') . $GC_ResultData["responsetext"];
$_SESSION['gpc'] = 'fail';
}
} //end of ($stream != false)
else{
$_SESSION['wpsc_checkout_misc_error_messages'][] = __(' Error en Conexion, no se efectuo la transaccion ') . $GC_ResultData["responsetext"];
$_SESSION['gpc'] = 'fail';
}
}
//Validating the POST info data from form_pagoCredomatic
function submit_pagoCredomatic(){
//exit('<pre>'.print_r($_POST, true).'</pre>');
if($_POST['gpc']['kID'] != null) {
update_option('mT_keyID', $_POST['gpc']['kID']);
}
if($_POST['gpc']['key'] != null) {
update_option('mT_key', $_POST['gpc']['key']);
}
return true;
}
function form_pagoCredomatic(){
$output = '
<tr>
<td>
<label for="mT_keyID">'.__('ID de la llave:').'</label>
</td>
<td>
<input type="text" name="gpc[kID]" id="mT_keyID" value="'.get_option("mT_keyID").'" size="10" />
</td>
</tr>
<tr>
<td>
<label for="mT_key">'.__('Llave del Mercante:').'</label>
</td>
<td>
<input type="text" name="gpc[key]" id="mT_key" value="'.stripslashes(get_option('mT_key')).'" size="30" />
</td>
</tr>';
return $output;
}
?>