-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcivicrm_backer_autoimport.module
195 lines (185 loc) · 7.14 KB
/
civicrm_backer_autoimport.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
<?php
function civicrm_backer_autoimport_menu() {
$items = array();
$items['backer-founder/ipn/%'] = array(
'access callback' => true,
'page callback' => 'civicrm_backer_autoimport_ipn',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'weight' => 0,
'file' => 'civicrm_backer_autoimport.ipn.inc',
);
$items['civicrm/contribute/stat'] = array(
'access callback' => 'civicrm_backer_autoimport_check_contribution_page',
'page callback' => 'civicrm_backer_autoimport_get_contribution_page_info',
'type' => MENU_CALLBACK,
'weight' => 0,
);
return $items;
}
function civicrm_backer_autoimport_civicrm_buildForm($form_name, &$form) {
if ($form_name == 'CRM_Contribute_Form_Contribution_Main') {
$isTest = ($form->_mode == 'test') ? 1 : 0;
$params = array(
1 => array($isTest, 'String'),
2 => array($form->_id, 'String'),
);
$sql = "SELECT url_site, url_button FROM civicrm_payment_processor WHERE payment_processor_type = 'BACKER' AND is_active = 1 AND is_test = %1 AND user_name = %2 AND url_site IS NOT NULL AND url_api IS NOT NULL";
$dao = CRM_Core_DAO::executeQuery($sql, $params);
if ($dao->fetch()) {
$path = drupal_get_path('module', 'civicrm_backer_autoimport');
drupal_add_js($path . '/civicrm_backer_autoimport.js');
drupal_add_css($path . '/civicrm_backer_autoimport.css');
$settings = array(
'url' => $dao->url_site,
'label' => $dao->url_button,
);
drupal_add_js(array('backer' => $settings), 'setting');
}
}
}
function civicrm_backer_autoimport_civicrm_alterTemplateVars($resource, &$vars) {
if (preg_match('/^string:/', $resource)) {
if (strstr($resource, 'msg_tpl_workflow_contribution-contribution_online_receipt-html') ||
strstr($resource, 'msg_tpl_workflow_contribution-contribution_offline_receipt-html') ) {
if (!empty($vars['amount_level']) && strstr($vars['amount_level'], CRM_Core_BAO_CustomOption::VALUE_SEPERATOR)) {
$amount_level = trim($vars['amount_level'], CRM_Core_BAO_CustomOption::VALUE_SEPERATOR);
list($amount_level) = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $amount_level);
$vars['amount_level'] = $amount_level;
}
}
}
if (!empty($vars['tplFile']) && $vars['tplFile'] == 'CRM/Contribute/Form/Contribution/Main.tpl') {
$contribution_page = $vars['id'];
$is_test = ($_GET['action'] == 'preview') ? 1 : 0;
$params = array(
1 => array($is_test, 'String'),
2 => array($contribution_page, 'String'),
);
$sql = "SELECT * FROM civicrm_payment_processor WHERE payment_processor_type = 'BACKER' AND is_active = 1 AND is_test = %1 AND user_name = %2 AND url_site IS NOT NULL AND url_api IS NOT NULL";
$dao = CRM_Core_DAO::executeQuery($sql, $params);
while ($dao->fetch()) {
if (!empty($vars['achievement'])) {
$url = $dao->url_api;
$APIResult = _civicrm_backer_autoimport_retrieve_API($url);
if ($vars['achievement']['type'] == "recurring") {
if ($APIResult['backer_count']) {
$current = $vars['achievement']['current'] += $APIResult['backer_count'];
}
}
if ($vars['achievement']['type'] == "amount") {
if ($APIResult['money_pledged']) {
$current = $vars['achievement']['current'] += $APIResult['money_pledged'];
}
}
$goal = $vars['achievement']['goal'];
$percent = round(ceil(($current/$goal)*100));
if ($current > 0 && $percent < 1) {
$percent = 1; // when there is value, we have at least 1 percent
}
$vars['achievement']['percent'] = $percent;
}
}
}
}
function _civicrm_backer_autoimport_retrieve_API($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// Check Error
if (curl_errno($ch)) {
CRM_Core_Error::debug_log_message('cURL Error: ' . curl_error($ch));
}
else {
curl_close($ch);
// Transfer to PHP array
$data = json_decode($result, true);
// Valid JSON
if (json_last_error() === JSON_ERROR_NONE && !empty($data) ) {
return $data;
}
else {
CRM_Core_Error::debug_log_message("JSON Parse Error: " . json_last_error_msg());
}
}
}
/**
* This function is used to get the contribution page information
* excluding contributions made through the 'Backer' payment processor
*
* @return array
*/
function civicrm_backer_autoimport_get_contribution_page_info() {
civicrm_initialize();
if ($_SERVER['REQUEST_METHOD'] == "GET" && $_SERVER['HTTP_X_CIVICRM_ACCESS_FROM'] == "backer") {
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$params = array( 1 => array($_GET['id'], 'Integer')) ;
$donation_query = "SELECT COALESCE(SUM(total_amount),0) FROM civicrm_contribution
WHERE contribution_page_id = %1
AND contribution_status_id = '1'
AND (payment_processor_id IS NULL OR payment_processor_id NOT IN (SELECT id FROM civicrm_payment_processor
WHERE payment_processor_type = 'Backer'
AND is_test = '0'))
AND is_test = '0'";
$donationAmountSum = CRM_Core_DAO::singleValueQuery($donation_query, $params);
$people_query = "SELECT count(contact_id) FROM civicrm_contribution
WHERE contribution_page_id = %1
AND contribution_status_id = '1'
AND (payment_processor_id IS NULL OR payment_processor_id NOT IN (SELECT id FROM civicrm_payment_processor
WHERE payment_processor_type = 'Backer'
AND is_test = '0'))
AND is_test = '0'";
$donationPeopleSum = CRM_Core_DAO::singleValueQuery($people_query, $params);
$donationAmountSum = (int) ($donationAmountSum);
if (isset($donationPeopleSum) && isset($donationAmountSum)) {
$data = array(
'people' => $donationPeopleSum,
'amount' => $donationAmountSum,
);
echo json_encode($data);
CRM_Utils_System::civiExit();
}
else {
$error = array(
'message' => 'An error occurred during processing.',
);
http_response_code(400);
echo json_encode($error);
CRM_Utils_System::civiExit();
}
}
else
{
$error = array(
'message' => 'Contribution page id is empty',
);
http_response_code(400);
echo json_encode($error);
CRM_Utils_System::civiExit();
}
}
}
/**
* This function is used to check contribution page have 'Backer' payment or not
*
* @return boolean: true if success, else false
*/
function civicrm_backer_autoimport_check_contribution_page() {
civicrm_initialize();
//Get contribution page id that use backer payment
$pageIds = array();
$sql = "SELECT user_name FROM civicrm_payment_processor WHERE payment_processor_type = 'Backer' AND is_test = '0'";
$dao = CRM_Core_DAO::executeQuery($sql);
while ($dao->fetch()) {
$pageIds[] = $dao->user_name;
}
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
if (in_array($_GET['id'],$pageIds)) {
return TRUE;
}
else {
return FALSE;
}
}
}