-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_account.module
More file actions
646 lines (530 loc) · 20.1 KB
/
pg_account.module
File metadata and controls
646 lines (530 loc) · 20.1 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?php
/**
* Constants.
*/
define('PG_ACCOUNT_ADD_BALANCE','add');
define('PG_ACCOUNT_SUB_BALANCE','sub');
define('PG_ACCOUNT_ADD_BALANCE_ADMIN_TITLE', 'Balance for [pg_account:username] changed by [pg_account:operatorname]. Added [pg_account:amount]. New balance is [pg_account:balance]');
define('PG_ACCOUNT_ADD_BALANCE_ADMIN_MESSAGE', 'User: [pg_account:username]
Operator: [pg_account:operatorname]
Added: [pg_account:amount]
Balance: [pg_account:balance]
Provided description: [pg_account:description]
[site:url]');
define('PG_ACCOUNT_SUB_BALANCE_ADMIN_TITLE', 'Balance for [pg_account:username] changed by [pg_account:operatorname]. Withdraw [pg_account:amount]. New balance is [pg_account:balance]');
define('PG_ACCOUNT_SUB_BALANCE_ADMIN_MESSAGE', 'User: [pg_account:username]
Operator: [pg_account:operatorname]
Withdraw: [pg_account:amount]
Balance: [pg_account:balance]
Provided description: [pg_account:description]
[site:url]');
define('PG_ACCOUNT_ADD_BALANCE_USER_TITLE', 'Your Balance changed on [pg_account:amount]. New balance is [pg_account:balance]');
define('PG_ACCOUNT_ADD_BALANCE_USER_MESSAGE', 'Dear [pg_account:username]!
We added [pg_account:amount] on your balance.
Your new balance is [pg_account:balance].
This transaction related to: [pg_account:description]
[site:url]');
define('PG_ACCOUNT_SUB_BALANCE_USER_TITLE', 'Your Balance changed on [pg_account:amount]. New balance is [pg_account:balance]');
define('PG_ACCOUNT_SUB_BALANCE_USER_MESSAGE', 'Dear [pg_account:username]!
We withdraw [pg_account:amount] from your balance.
Your new balance is [pg_account:balance].
This transaction related to: [pg_account:description]
[site:url]');
define('PG_ACCOUNT_SEND_BALANCE_USER_TITLE', 'Your Balance changed on [pg_account:amount]. New balance is [pg_account:balance]');
define('PG_ACCOUNT_SEND_BALANCE_USER_MESSAGE', 'Dear [pg_account:username]!
We transfered [pg_account:amount] from your balance to [pg_account:username_received]
Your new balance is [pg_account:balance].
This transaction related to: [pg_account:description]
[site:url]');
define('PG_ACCOUNT_RECEIVE_BALANCE_USER_TITLE', 'Your Balance changed on [pg_account:amount]. New balance is [pg_account:balance]');
define('PG_ACCOUNT_RECEIVE_BALANCE_USER_MESSAGE', 'Dear [pg_account:username_received]!
We transfered [pg_account:amount] from [pg_account:username] to your balance.
Your new balance is [pg_account:balance].
This transaction related to: [pg_account:description]
[site:url]');
/**
* Implementation of hook_cron()
*/
function pg_account_cron(){
pg_account_process_oldtransaction();
}
/**
* Implements hook_permission().
*/
function pg_account_permission() {
// Add permission to admin pgapi.
return array(
'administer pg_account' => array(
'title' => t('Administer pg_account'),
'description' => t('NEED TO REPLACE'),
'restrict access' => TRUE,
),
'pg_account balance' => array(
'title' => t('Balance'),
'description' => t('User pg_account balance'),
'restrict access' => TRUE,
),
'pg_account operator' => array(
'title' => t('Operator'),
'description' => t('User can add or withdraw funds from user balance'),
'restrict access' => TRUE,
),
'pg_account send money' => array(
'title' => t('Send money'),
'description' => t('User can send money to another user'),
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_theme()
*/
function pg_account_theme() {
return array(
'pg_account_complete' => array(
'arguments' => array('t' => array()),
'file' => 'pg_account.theme.inc',
),
'pg_account_fail' => array(
'arguments' => array('t' => array()),
'file' => 'pg_account.theme.inc',
),
'pg_account_pending' => array(
'arguments' => array('t' => array()),
'file' => 'pg_account.theme.inc',
),
'user_balance' => array(
'arguments' => array('balance' => array()),
'file' => 'pg_account.theme.inc',
)
);
}
/**
* IMenu callback; loads a balance object
*/
function balance_load($uid) {
$uid=(int)$uid;
return pg_account_balance_load($uid);
}
function pg_account_profileAccess($account = NULL) {
global $user;
if(user_access('pg_account balance') && $account->uid == $user->uid){
return true;
}
if($user->uid == 1 or user_access('pg_account operator') or user_access('administer pg_account')){
return true;
}
return false;
}
/**
* Implements hook_user_delete().
*/
function pg_account_delete($account) {
db_delete('pg_account_balance')
->condition('uid', $account->uid)
->execute();
}
/**
* Implements menu_hook()
*/
function pg_account_menu() {
$items['user/%user/balance'] = array(
'title' => 'User balance',
'page callback' => 'pg_account_personal_history',
'page arguments' => array(1),
'access callback' => 'pg_account_profileAccess',
'access arguments' => array(1),
'file' => 'pg_account.user.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 31,
);
$items['user/%user/balance/history'] = array(
'title' => 'Transactions history',
'page callback' => 'pg_account_personal_history',
'page arguments' => array(1),
'access callback' => 'pg_account_profileAccess',
'access arguments' => array(1),
'file' => 'pg_account.user.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['user/%user/balance/add'] = array(
'title' => 'Add Funds',
'page callback' => 'pg_account_account_add',
'page arguments' => array(1),
'access callback' => 'pg_account_profileAccess',
'access arguments' => array(1),
'file' => 'pg_account.user.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
$items['user/%user/balance/withdraw'] = array(
'title' => 'Withdraw',
'page callback' => 'pg_account_account_withdraw',
'page arguments' => array(1),
'access arguments' => array('pg_account operator'),
'file' => 'pg_account.user.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['user/%user/balance/send'] = array(
'title' => 'Send money',
'page callback' => 'drupal_get_form',
'page arguments' => array('pg_account_account_send', 1),
'access arguments' => array('pg_account send money'),
'file' => 'pg_account.user.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['admin/config/pgapi/gateway_settings/pgaccount'] = array(
'title' => 'Account',
'description' => 'Account payment settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('pg_account_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer pgapi'),
'file' => 'pg_account.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);
$items['admin/reports/pgaccount_transactions'] = array(
'title' => 'Account transactions',
'description' => 'Manage account transaction.',
'page callback' => 'pg_account_history',
'access callback' => 'user_access',
'access arguments' => array('pg_account operator'),
'file' => 'pg_account.admin.inc',
);
$items['payment/pgaccount/%txnid'] = array(
'title' => 'Account Balance Payment',
'page callback' => 'drupal_get_form',
'page arguments' => array('pg_account_payform',2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'pg_account.admin.inc',
);
$items['pgaccount/complete/%txnid'] = array(
'title' => 'Transaction completed successfuly',
'page callback' => 'pg_account_complete',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'pg_account.admin.inc',
);
$items['pgaccount/pending/%txnid'] = array(
'title' => 'Transaction in progress',
'page callback' => 'pg_account_pending',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'pg_account.admin.inc',
);
$items['pgaccount/fail/%txnid'] = array(
'title' => 'Transaction was failed',
'page callback' => 'pg_account_fail',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'pg_account.admin.inc',
);
return $items;
}
/**
* Implements hook_formfilterapi().
*/
function pg_account_formfilterapi($op,$session_name,&$a1 = NULL){
if($op == 'filters'){
$filters = array();
switch($session_name){
case 'pg_account_personal_history';
$filters['price']= array(
'#type' => 'textfield',
'#title' => t('Amount'),
'#where' => "pgt.amount",
);
$filters['action']= array(
'#type' => 'select',
'#title' => t('Payments'),
'#where' => array('operation' => "pgt.amount"),
'#options' => array(
'> 0' => t('received'),
'< 0' => t('sent'),
),
);
$filters['status'] = array(
'#type' => 'select',
'#title' => t('Payment status'),
'#where' => 'pgt.status',
'#options' => pgapi_build_status(),
);
break;
case 'pg_account_operator_history';
$filters['name']= array(
'#type' => 'textfield',
'#title' => t('User'),
'#where' => "u.name",
);
$filters['price']= array(
'#type' => 'textfield',
'#title' => t('Amount'),
'#where' => "pgt.amount",
);
$filters['action']= array(
'#type' => 'select',
'#title' => t('Payments'),
'#where' => array('operation' => "pgt.amount"),
'#options' => array(
'> 0' => t('received'),
'< 0' => t('sent'),
),
);
$filters['status'] = array(
'#type' => 'select',
'#title' => t('Payment status'),
'#where' => 'pgt.status',
'#options' => pgapi_build_status(),
);
break;
}
return $filters;
}
}
/**
* Implements hook_provideNotice().
*/
function pg_account_provideNotice($op){
switch($op){
case 'settings':
return array(
'name' => 'pg_account',
'title' => t('Balance notifications'),
);
break;
}//switch
}
/*
* Implements hook_pgapi_gw
*/
function pg_account_pgapi_gw($op,$a3=NULL,$a4=NULL){
global $user;
switch($op){
case 'display name':
$ret = t('Balance');
break;
case 'payment page':
$ret = url('payment/pgaccount/'.$a3 ,array('absolute' => TRUE));
break;
case 'payment gateway info':
$balance=pg_account_balance_load($user->uid);
$ret = FALSE;
if($a4->service != 'pg_account' && $balance->balance >= $a3) {
$path = drupal_get_path('module','pg_account').'/pg_account.png';
$ret['name'] = '<div style="background: url(/'.$path.') no-repeat left top; padding-left:80px; height:45px; font-weight:bold; padding-top:25px;">'.t('Balance').'</div>';
$format = variable_get('pgapi_currency', array());
$ret['price']= array( $format['symbol'] => $a3 );
}
break;
case 'ajax_form':
$t=$a3;
return array(
'element' => array (
'message' => array(
'#markup' => t('We will charge !amount from your balance', array ('!amount' => pgapi_format_price_plain($t->amount))),
),
),
'submit' => 'pg_account_submit',
);
break;
}
return $ret;
}
function pg_account_submit($form,$form_state){
$values = $form_state['values'];
$transaction = pgapi_transaction_load($values['txnid']);
$balance = pg_account_balance_load($transaction->uid);
$data = new stdClass();
$data->amount = $transaction->amount;
$data->uid = $transaction->uid;
if(pg_account_balance_sub($data)){
$transaction->status = pgapi_get_status_id('completed');
$taccount = new stdClass();
$taccount->amount = (0 - $transaction->amount);
$taccount->operation = PG_ACCOUNT_SUB_BALANCE;
$taccount->uid = $transaction->uid;
$taccount->pgapi_txnid = $transaction->txnid;
pg_account_transaction_save($taccount);
$account = user_load($taccount->uid);
//create tokens data
$tokens['username'] = $account->name;
$tokens['description'] = $transaction->title."\n".$transaction->description."\n";
$tokens['amount'] = pgapi_format_price_plain($data->amount);
$balance=pg_account_balance_load($account->uid);
$tokens['balance'] = pgapi_format_price_plain($balance->balance);
$notice = new stdClass();
$notice->uid = $account->uid;
$notice->provider = 'pg_account';
$message = t(variable_get('pg_account_user_message_sub_body',PG_ACCOUNT_SUB_BALANCE_USER_MESSAGE));
$notice->message = token_replace($message, array('pg_account' => $tokens));
$subject = t(variable_get('pg_account_user_title_sub',PG_ACCOUNT_SUB_BALANCE_USER_TITLE));
$notice->subject = token_replace($subject, array('pg_account' => $tokens));
$notice->format = variable_get('pg_account_user_message_sub_format','filtered_html');
notices_save($notice);
}else{
$transaction->status = pgapi_get_status_id('failed');
}
pgapi_transaction_save($transaction);
}
/*
* Implemets hook_pgapi_transaction
*/
function pg_account_pgapi_transaction($op, $t, $target = NULL){
if($t->service == 'pg_account' && $target == 'service'){
global $user;
$account = user_load($t->uid);
$t_old = pgapi_transaction_load($t->txnid);
$balance = pg_account_balance_load($t->uid);
$tokens['username'] = $account->name;
$tokens['operatorname'] = $user->name;
$tokens['description'] = $t->description;
$tokens['amount'] = pgapi_format_price_plain($t->amount);
$balance=pg_account_balance_load($account->uid);
$tokens['balance'] = pgapi_format_price_plain($balance->balance);
switch($op){
case PG_COMPLETED:
if($t_old->status != PG_COMPLETED){
//send notice
$data = new stdClass();
$data->amount = $t->amount;
$data->uid = $account->uid;
if(pg_account_balance_add($data)){
$balance=pg_account_balance_load($account->uid);
$tokens['balance'] = pgapi_format_price_plain($balance->balance);
$notice = new stdClass();
$notice->uid = $account->uid;
$notice->provider = 'pg_account';
if($user->uid != 0 ){
$tokens['description'] = t('Transaction completed by operation !username.', array('!username' => $user->name)) . "\n" . $tokens['description'];
}
$message = t(variable_get('pg_account_user_message_add_body',PG_ACCOUNT_ADD_BALANCE_USER_MESSAGE));
$notice->message = token_replace($message, array('pg_account' => $tokens));
$subject = t(variable_get('pg_account_user_title_add',PG_ACCOUNT_ADD_BALANCE_USER_TITLE));
$notice->subject = token_replace($subject, array('pg_account' => $tokens));
$notice->format = variable_get('pg_account_user_message_add_format','filtered_html');
notices_save($notice);
}
}
break;
case PG_PENDING:
case PG_FAILED:
case PG_DENIED:
case PG_REFUNDED:
case PG_RECEIVED:
case PG_CANCELED:
if($t_old->status == PG_COMPLETED){
$data = (object)(NULL);
$data->amount = $t->amount;
$data->uid = $account->uid;
if(pg_account_balance_sub($data)){
$balance=pg_account_balance_load($account->uid);
$tokens['balance'] = pgapi_format_price_plain($balance->balance);
//send notice
$notice = new stdClass();
$notice->uid = $account->uid;
$notice->provider = 'pg_account';
if($user->uid != 0 ){
$tokens['description'] = t('Cancelling transaction by operation !username.', array('!username' => $user->name)) . "\n" . $tokens['description'];
}
$message = t(variable_get('pg_account_user_message_sub_body',PG_ACCOUNT_SUB_BALANCE_USER_MESSAGE));
$notice->message = token_replace($message, array('pg_account' => $tokens));
$subject = t(variable_get('pg_account_user_title_sub',PG_ACCOUNT_SUB_BALANCE_USER_TITLE));
$notice->subject = token_replace($subject, array('pg_account' => $tokens));
$notice->format = variable_get('pg_account_user_message_sub_format','filtered_html');
notices_save($notice);
}
}
break;
}
}//if
}
function pg_account_pgapi_callback($t){
switch($t->status){
case PG_COMPLETED:
return 'pgaccount/complete/'.$t->txnid;
case PG_PENDING:
return 'pgaccount/pending/'.$t->txnid;
default:
return 'pgaccount/fail/'.$t->txnid;
break;
}
}
/*
* API
*/
function pg_account_transaction_load($txnid) {
$transaction = db_query("SELECT * FROM {pg_account_transaction} WHERE txnid = :txnid", array ( ':txnid' => $txnid))->fetchObject;
return $transaction;
}
function pg_account_transaction_save(&$t) {
if(isset($t->txnid)){
drupal_write_record('pg_account_transaction', $t,'txnid');
}else{
drupal_write_record('pg_account_transaction', $t);
}
}
function pg_account_balance_add($data) {
if($data->amount < 0){
//amount needs to be positive
return false;
}
$b = pg_account_balance_load($data->uid);
if(isset($b->norecord)){
$b->balance = $data->amount;
$b->uid = $data->uid;
drupal_write_record('pg_account_balance', $b);
}else{
$b->balance = $b->balance + $data->amount;
drupal_write_record('pg_account_balance', $b, 'uid');
}
module_invoke_all('pgaccount_balance', 'add',$data);
return true;
}
function pg_account_balance_sub($values) {
$values=(object)$values;
$sub=abs($values->amount);
$b=pg_account_balance_load($values->uid);
if($b->balance >=$sub) {
$b->balance = $b->balance - $sub;
drupal_write_record('pg_account_balance', $b, 'uid');
module_invoke_all('pgaccount_balance', 'sub',$b);
return true;
}
return false;
}
function pg_account_balance_load($uid) {
$balance = db_query("SELECT * FROM {pg_account_balance} WHERE uid = :uid ", array(':uid' => $uid))->fetchObject();
if(empty($balance)){
$balance = new stdClass();
$balance->uid=$uid;
$balance->balance = 0;
$balance->norecord =true;
}
return $balance;
}
function pg_account_process_oldtransaction($uid=0){
if($uid==0){
$transactions = db_query('SELECT * FROM {pg_account_transaction} WHERE status = :status', array( ':status' => PG_PENDING))->fetchAll();
}else{
$transactions = db_query('SELECT * FROM {pg_account_transaction} WHERE status = %d AND uid = :uid', array( ':status' => PG_PENDING, ':uid' => $uid))->fetchAll();
}
foreach($transactions as $t){
//cancel orders with no payment more that 7 days
$remove=86400*7;
if( $t->status == PG_PENDING && $t->created < time()-$remove){
$t->status = PG_CANCELED;
$t->status = pgapi_get_status_id('canceled');
$t->workflow = pgapi_get_workflow_id('canceled');
$t->description = t('Transaction marked canceled due no payment for more than 7 days');
pg_account_transaction_save($t);
}
}
}