-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommercegate.module
485 lines (439 loc) · 15.1 KB
/
commercegate.module
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function commercegate_help($path, $arg) {
$output = ''; //declare your output variable
switch ($path) {
case "admin/help#commercegate":
$output = '<p>'. t("Tools that work with the CommerceGate payment system") .'</p>';
break;
case "admin/settings#commercegate":
$output = '<p>'. t("Your callback server is located in the module_folder/callback/server.php") .'</p>';
break;
}
return $output;
} // function onthisdate_help
/**
* Admin page for the module
*/
function commercegate_admin() {
$form = array();
$form['commercegate_cid'] = array(
'#type' => 'textfield',
'#title' => t('Account ID'),
'#default_value' => variable_get('commercegate_cid', ''),
'#size' => 10,
'#maxlength' => 10,
'#description' => t("CommerceGate account ID."),
'#required' => TRUE,
);
$form['commercegate_wid'] = array(
'#type' => 'textfield',
'#title' => t('Website ID'),
'#default_value' => variable_get('commercegate_wid', ''),
'#size' => 10,
'#maxlength' => 10,
'#description' => t("Website ID in CommerceGate configuration."),
'#required' => TRUE,
);
$form['commercegate_packid'] = array(
'#type' => 'textfield',
'#title' => t('Package ID'),
'#default_value' => variable_get('commercegate_packid', ''),
'#size' => 10,
'#maxlength' => 10,
'#description' => t("Package ID in CommerceGate configuration."),
'#required' => FALSE,
);
$form['commercegate_email'] = array(
'#type' => 'textfield',
'#title' => t('Notification email'),
'#default_value' => variable_get('commercegate_email', ''),
'#size' => 30,
'#maxlength' => 64,
'#description' => t("Email to which send the notifications."),
'#required' => TRUE,
);
$form['commercegate_linktext'] = array(
'#type' => 'textfield',
'#title' => t('Text in link'),
'#default_value' => variable_get('commercegate_linktext', ''),
'#size' => 30,
'#maxlength' => 64,
'#description' => t("Link text for the CommerceGate block."),
'#required' => TRUE,
);
$form['commercegate_sendemail'] = array(
'#type' => 'checkbox',
'#title' => t('Send user email'),
'#return_value' => 1,
'#default_value' => variable_get('commercegate_sendemail', 0),
'#description' => t("Send logged user's email to Payment Page?"),
'#required' => TRUE,
);
$form['commercegate_sendname'] = array(
'#type' => 'checkbox',
'#title' => t('Send username'),
'#return_value' => 1,
'#default_value' => variable_get('commercegate_sendname', 0),
'#description' => t("Send logged user's username to Payment Page?"),
'#required' => TRUE,
);
$form['commercegate_sendpassword'] = array(
'#type' => 'checkbox',
'#title' => t('Send user password'),
'#return_value' => 1,
'#default_value' => variable_get('commercegate_sendpassword', 0),
'#description' => t("Send logged user's password (hashed) to Payment Page?"),
'#required' => TRUE,
);
$form['commercegate_sendlanguage'] = array(
'#type' => 'checkbox',
'#title' => t('Send user language setting'),
'#return_value' => 1,
'#default_value' => variable_get('commercegate_sendlanguage', 0),
'#description' => t("Send logged user's language setting to Payment Page?"),
'#required' => TRUE,
);
$form['commercegate_checkip'] = array(
'#type' => 'checkbox',
'#title' => t('Check remote IP'),
'#return_value' => 1,
'#default_value' => variable_get('commercegate_checkip', 0),
'#description' => t("Only accept callbacks from CommerceGate's IPs? (recommended)"),
'#required' => TRUE,
);
return system_settings_form($form);
}// function commercegate_admin()
/**
* Validate de input data from admin page
*/
function commercegate_admin_validate($form, &$form_state) {
$cid = $form_state['values']['commercegate_cid'];
if (!is_numeric($cid)) {
form_set_error('commercegate_cid', t('Account ID must be a number.'));
}
}// function commercegate_admin_validate()
/**
* Implementation of hook_menu()
*/
function commercegate_menu() {
$items = array();
// añadir al menú de administración la página de settings del modulo
$items['admin/settings/commercegate'] = array(
'title' => t('CommerceGate'),
'description' => t('Configuration settings for module CommerceGate'),
'page callback' => 'drupal_get_form',
'page arguments' => array('commercegate_admin'),
'access' => user_access('administer commercegate'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
// crear una url para acceder al callback server
$items['commercegate/callbackserver'] = array(
'title' => t('Callback server'),
'description' => t('URL that triggers the callback server, to be called from CommerceGate servers'),
'type' => MENU_CALLBACK,
'page callback' => 'callback_server',
//'access' => user_access('commercegate server access'),
'access arguments' => array('access content'),
);
return $items;
}// function commercegate_menu()
function callback_server () {
module_invoke_all('exit');
//comprovamos que la llamada viene de una IP de CommerceGate
$checkip = variable_get('commercegate_checkip', 0);
$ips = array(
'195.10.21.3',
'195.10.21.4',
'195.10.21.5',
'195.10.21.12',
'195.10.38.130',
'195.10.38.131',
'195.10.38.132',
'195.10.38.133',
'195.10.38.134',
'195.10.38.135',
'195.10.38.136',
'195.10.38.137',
'195.10.38.138',
'195.10.38.139',
'195.10.38.140'
);
$ip=$_SERVER['REMOTE_ADDR'];
if($checkip && !in_array($ip,$ips)){
watchdog('commercegate', 'Unauthorized IP address %ip tried to access /commercegate/callbackserver.', array('%ip'=>$ip), WATCHDOG_ALERT);
drupal_set_message(t('You are not authorized to acces this address.'),'error', false);
drupal_not_found();
exit();
}
// Parse $_REQUEST parameters
$array=split("#",$_REQUEST['message']);
$cb= new stdClass;
$cb->transactiontype=$array[0];
$cb->transactionid=$array[1];
$cb->transactionreferenceid=$array[2];
$cb->offername=$array[3];
$cb->offerid=$array[4];
$cb->amount=$array[5];
$cb->currency=$array[6];
$cb->userid=$array[7];
$cb->userpw=$array[8];
$cb->email=$array[9];
$cb->ip=$array[10];
$cb->countryiso=$array[11];
$cb->cardholder=$array[12];
$cb->customerid=$array[13];
$cb->websiteid=$array[14];
$cb->op1=$array[15];
$cb->op2=$array[16];
$cb->op3=$array[17];
$cb->nmu=$array[18];
$cb->date= date( 'Y-m-d H:i:s');
//check transaction type
switch ($cb->transactiontype) {
case "SALE": // insert the user.
_commercegate_invoke_callback_trigger('callback_sale', $cb);
break;
case "REBILL": // Rebill user
_commercegate_invoke_callback_trigger('callback_rebill', $cb);
break;
case "UPSELL": // Upsell user
_commercegate_invoke_callback_trigger('callback_upsell', $cb);
break;
case "REFUND": // refund user
_commercegate_invoke_callback_trigger('callback_refund', $cb);
break;
case "CHARGEBACK": // chargeback user
_commercegate_invoke_callback_trigger('callback_chargeback', $cb);
break;
default:
echo "unsupported callback type";
watchdog('commercegate', 'Unsupported callback type', null , WATCHDOG_ERROR);
exit();
break;
}
$log = "User ".$cb->userid." did a ".$cb->transactiontype." transaction of ".$cb->amount." ".$cb->currency.". Offer: ".$cb->offername;
watchdog('commercegate', $log, null , WATCHDOG_INFO);
echo 'success';
exit();
}
/**
* Callback for trigger that fires actions assigned to him on every page request
*/
function _commercegate_invoke_callback_trigger($callback = '', $cb) {
if( $callback == '' || empty($cb) ) exit();
// Invoke all actions for this trigger
module_invoke_all('commercegate', $callback, $cb);
}
/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/
function commercegate_perm() {
return array('access commercegate content','administer commercegate');
} // function onthisdate_perm()
/**
* Implementation of hook_mail()
*/
function commercegate_mail ($key, &$message, $params) {
$transactiontype = $params['transactiontype'];
$transactionid = $params['transactionid'];
$transactionreferenceid = $params['transactionreferenceid'];
$offername = $params['offername'];
$offerid = $params['offerid'];
$amount = $params['amount'];
$currency = $params['currency'];
$userid = $params['userid'];
$userpw = $params['userpw'];
$email = $params['email'];
$ip = $params['ip'];
$countryiso = $params['countryiso'];
$cardholder = $params['cardholder'];
$customerid = $params['customerid'];
$websiteid = $params['websiteid'];
$op1 = $params['op1'];
$op2 = $params['op2'];
$op3 = $params['op3'];
$nmu = $params['nmu'];
$date = $params['date'];
$body = "Notification of CommerceGate transaction from site id $websiteid:\n\n";
$body .= "Transaction type: $transactiontype\n";
$body .= "Transaction reference ID: $transactionreferenceid\n";
$body .= "Offer name: $offername\n";
$body .= "Amount: $amount $currency\n";
$body .= "User ID: $userid\n";
$body .= "User email: $email\n";
$body .= "Country: $countryiso\n";
$body .= "Cardholder: $cardholder\n";
$body .= "Customer ID: $customerid\n";
$body .= "Date: $date\n";
switch ($key) {
case 'notification':
// Set headers etc
$message['subject'] = "Notification of CommerceGate transaction (user: $userid)";
$message['body'] = $body;
break;
}
}
/**
* Generate HTML for the CommerceGate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function commercegate_block($op='list', $delta=0) {
// set up the block
$block = array();
if ($op == "list") {
// Generate listing of blocks from this module, for the admin/block page
$block[0]["info"] = t('CommerceGate link block');
}
else if ($op == 'view') {
// Generate our block content
// Variables from admin page
$cid = variable_get('commercegate_cid', '');
$wid = variable_get('commercegate_wid', '');
$packid = variable_get('commercegate_packid', '');
$sendemail = variable_get('commercegate_sendemail', 0);
$sendname = variable_get('commercegate_sendname', 0);
$sendpassword = variable_get('commercegate_sendpassword', 0);
$sendlanguage = variable_get('commercegate_sendlanguage', 0);
// User variables
global $user;
$mail = $user->mail;
$name = $user->name;
$pass = $user->pass;
$lang = $user->language;
// content variable that will be returned for display
$block_content = '<a href="https://secure.commercegate.com/payment/ccform.php?cid='.$cid.'&wid='.$wid;
if($packid!='') $block_content .= '&packid='.$packid;
if($sendemail) $block_content .= '&email='.$mail;
if($sendname) $block_content .= '&username='.$name;
if($sendpassword) $block_content .= '&password='.$pass;
if($sendlanguage) $block_content .= '&lang='.$lang;
$block_content .= '" title="Link" target="_blank">'.t(variable_get('commercegate_linktext', 'Link')).'</a>';
// check to see if there was any content before returning
// the block view
if ($block_content == '') {
// no content from a week ago
$block['content'] = 'Sorry No Content';
}
else {
// set up the block
$block['content'] = $block_content;
}
}
return $block;
} // end commercegate_block
/** TRIGGERS (from http://drupal.org/node/375833) */
// Transaction types: sale, rebill, upsell, refund, cargeback.
/**
* Implementation of hook_hook_info().
*/
function commercegate_hook_info() {
return array(
'commercegate' => array(
'commercegate' => array(
'callback_sale' => array(
'runs when' => t('The CommerceGate server calls our callback server with SALE transaction type'),
),
'callback_rebill' => array(
'runs when' => t('The CommerceGate server calls our callback server with REBILL transaction type'),
),
'callback_upsell' => array(
'runs when' => t('The CommerceGate server calls our callback server with UPSELL transaction type'),
),
'callback_refund' => array(
'runs when' => t('The CommerceGate server calls our callback server with REFUND transaction type'),
),
'callback_cargeback' => array(
'runs when' => t('The CommerceGate server calls our callback server with CARGEBACK transaction type'),
),
),
),
);
} // end commercegate_hook_info
/**
* Implementation of hook_trigger_name().
*/
function commercegate_commercegate($op, $cb) {
// We support a subset of operations.
if (!in_array($op, array('callback_sale','callback_rebill','callback_upsell','callback_refund','callback_cargeback'))) {
return;
}
$aids = _trigger_get_hook_aids('commercegate', $op);
$context = array(
'hook' => 'commercegate',
'op' => $op,
'transactiontype' => $cb->transactiontype,
'transactionid' => $cb->transactionid,
'transactionreferenceid' => $cb->transactionreferenceid,
'offername' => $cb->offername,
'offerid' => $cb->offerid,
'amount' => $cb->amount,
'currency' => $cb->currency,
'userid' => $cb->userid,
'userpw' => $cb->userpw,
'email' => $cb->email,
'ip' => $cb->ip,
'countryiso' => $cb->countryiso,
'cardholder' => $cb->cardholder,
'customerid' => $cb->customerid,
'websiteid' => $cb->websiteid,
'op1' => $cb->op1,
'op2' => $cb->op2,
'op3' => $cb->op3,
'nmu' => $cb->nmu,
'date' => $cb->date,
);
actions_do(array_keys($aids), $user, $context);
} // end commercegate_commercegate()
/**
* Implementation of hook_menu_alter().
*
*/
function commercegate_menu_alter(&$callbacks) {
if (module_exists('trigger') & isset($callbacks['admin/build/trigger/commercegate'])) {
$callbacks['admin/build/trigger/commercegate']['access callback'] = 'trigger_access_check';
}
}
/** ACTIONS http://drupal.org/node/172152 http://drupal.org/node/438732 */
/**
* Implementation of hook_action_info().
*/
function commercegate_action_info() {
return array(
'commercegate_custom_action' => array(
'description' => t('CommerceGate module custom action.'),
'type' => 'commercegate',
'configurable' => false,
'hooks' => array(
'commercegate' => array('callback_sale','callback_rebill','callback_upsell','callback_refund','callback_cargeback'),
)
),
);
}
/**
* Action callback function
*/
function commercegate_custom_action(&$object,$context) {
//aqui iria el código para un custom action para este modulo
//por ejemplo publicar el anuncio del usuario que ha pagado
//por ahora lo que hace es enviar un email de notificación
$to = variable_get('commercegate_email', '');
if($to == '') {
watchdog('commercegate', 'Notification email not sent because no email address was given (go to <a href="admin/settings/commercegate">settings</a>).', array(), WATCHDOG_ALERT);
exit();
}
global $language;
$lang = $language->language;
drupal_mail('commercegate', 'notification', $to, $lang, $context);
watchdog('commercegate', 'Notification email sent to %to.', array('%to'=>$to), WATCHDOG_NOTICE);
}
?>