-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcivicrm_esunbank.module
150 lines (137 loc) · 5.49 KB
/
civicrm_esunbank.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
<?php
define('CIVICRM_ESUNBANK_EXPIRED_DAY', 7);
/**
* Implementation of hook_init()
*/
function civicrm_esunbank_init(){
if(strstr($_GET['q'], 'payment-esunbank')){
$GLOBALS['conf']['cache'] = FALSE;
}
}
/**
* Implementation of hook_menu()
*/
function civicrm_esunbank_menu(){
return array(
'payment-esunbank/ipn' => array(
'access callback' => true,
'page callback' => 'civicrm_esunbank_ipn',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'weight' => 0,
'file' => 'civicrm_esunbank.ipn.inc',
),
);
}
/**
* Implementation of hook_civicrm_buildForm()
*/
function civicrm_esunbank_civicrm_buildForm($form_name, &$form){
switch($form_name){
case 'CRM_Contribute_Form_Contribution_ThankYou':
break;
case 'CRM_Contribute_Form_Contribution_Main':
break;
}
}
/**
* Implementation of hook_cron()
*/
function civicrm_esunbank_cron(){
$now = $_SERVER['REQUEST_TIME'] ? $_SERVER['REQUEST_TIME'] : time();
// fetch ftp
// every one hour, we check a system path for esunbank payment
$last = variable_get("civicrm_esunbank_cron", 0);
if($now - $last >= 86399){
$dir = file_directory_path().'/esunbank';
$dir2 = file_directory_path().'/esunbank_processed';
if(file_check_directory($dir, FILE_CREATE_DIRECTORY) && file_check_directory($dir2, FILE_CREATE_DIRECTORY)){
module_load_include('inc', 'civicrm_esunbank', 'civicrm_esunbank.ipn');
// start trying process ftp data
file_scan_directory($dir, '.csv', array('.', '..', 'CVS'), 'civicrm_esunbank_process_file', FALSE);
}
variable_set("civicrm_esunbank_cron", $_SERVER['REQUEST_TIME']);
}
// expired
// every day, we check if payment should expired.
// we will expired payment in 10 day before. (only for esunbank instrument)
if(date('G') > 2 && date('G') < 5){
$status = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$instrument_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Esunbank', 'value', 'name');
$overdue = array_search('Overdue', $status);
$pending = array_search('Pending', $status);
$gid = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_option_group WHERE name LIKE 'payment_instrument'");
if(!empty($instrument_id) && !empty($overdue) && !($pending)){
$days_before = date('Y-m-d H:i:s', $now - 86400*40);
$days_after = date('Y-m-d H:i:s', $now - 86400*35);
CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution SET contribution_status_id = {$overdue} WHERE contribution_status_id = {$pending} AND created_date > '{$days_after}' AND created_date < {$days_before} AND payment_instrument_id = {$instrument_id}");
}
}
}
function civicrm_esunbank_process_file($path){
static $num_files;
$num_files++;
// not today file
$today = (date('Y')-1911).date('md');
if(file_exists($path) && !strstr($path, $today) && $num_files <= 5){
$dest = str_replace('/esunbank/', '/esunbank_processed/', $path);
$data = file_get_contents($path);
if (substr($data, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
$data = substr($data, 3);
}
$data = trim($data);
if(!empty($data)){
$post = array('Data' => $data);
$results = civicrm_esunbank_ipn($post, TRUE);
// file move, we can't use drupal file_move because it's only move to drupal
if (copy($path, $dest)) {
unlink($path);
}
if($results === FALSE){
// empty results
}
else{
watchdog('civicrm_esunbank', var_export($results, true));
}
}
}
}
function civicrm_esunbank_civicrm_searchColumns($context, &$headers, &$rows, &$selector){
if($context == 'contribution' && !empty($rows)){
$instrument_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'Esunbank', 'value', 'name');
foreach($rows as $k => $r){
if($r['contribution_status_id'] == 2){
$ids[$k] = $r['contribution_id'];
}
}
if(count($ids)){
$processor = $pids = array();
$least_day = date('Y-m-d H:i:s', strtotime('-'.CIVICRM_ESUNBANK_EXPIRED_DAY.' days'));
$query = CRM_Core_DAO::executeQuery("SELECT id, payment_processor_id as pid FROM civicrm_contribution WHERE id IN (".implode(',', $ids).") AND payment_instrument_id = %1 AND payment_processor_id IS NOT NULL AND created_date >= '{$least_day}'", array(1 => array($instrument_id, 'Integer')));
while($query->fetch()){
if(empty($processor[$query->pid]) && !empty($query->pid)){
$processor[$query->pid][0] = CRM_Core_BAO_PaymentProcessor::getPayment($query->pid, 'live');
$processor[$query->pid][1] = CRM_Core_BAO_PaymentProcessor::getPayment($query->pid, 'test');
}
$k = array_search($query->id, $ids);
if(!empty($rows[$k])){
$pp =& $processor[$query->pid][$rows[$k]['is_test']];
$url = array(
'code' => $pp['user_name'],
'number' => $query->id,
'length' => $pp['password'],
'price' => round($rows[$k]['total_amount']),
'timestamp' => strtotime($rows[$k]['created_date'])+86400*CIVICRM_ESUNBANK_EXPIRED_DAY,
'store' => $pp['subject'],
'type' => $pp['signature'],
'user' => $rows[$k]['sort_name'],
'item' => $rows[$k]['contribution_source'],
);
$link = esunbank_api_link($url);
$link = '<a href="'.$link.'" class="action-item" target="_blank">帳單</a>';
$rows[$k]['action'] = str_replace('</span>', $link.'</span>', $rows[$k]['action']);
}
}
}
}
}