Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for processing a ontribution | |
38 | * | |
39 | */ | |
40 | class CRM_Contribute_Form_UpdateBilling extends CRM_Core_Form { | |
41 | protected $_crid = NULL; | |
42 | protected $_coid = NULL; | |
43 | protected $_mode = NULL; | |
44 | ||
45 | protected $_subscriptionDetails = NULL; | |
46 | ||
47 | protected $_selfService = FALSE; | |
48 | ||
49 | public $_bltID = NULL; | |
50 | public $_paymentProcessor = NULL; | |
51 | ||
52 | public $_paymentProcessorObj = NULL; | |
53 | ||
54 | /** | |
55 | * Function to set variables up before form is built | |
56 | * | |
57 | * @return void | |
58 | * @access public | |
59 | */ | |
60 | public function preProcess() { | |
61 | $this->_mid = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE); | |
62 | $this->_crid = CRM_Utils_Request::retrieve('crid', 'Integer', $this, FALSE); | |
63 | if ($this->_crid) { | |
64 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_crid, 'recur', 'info'); | |
65 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_crid, 'recur', 'obj'); | |
66 | $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_crid); | |
eea16664 | 67 | |
6a488035 TO |
68 | // Are we cancelling a recurring contribution that is linked to an auto-renew membership? |
69 | if ($this->_subscriptionDetails->membership_id) { | |
70 | $this->_mid = $this->_subscriptionDetails->membership_id; | |
71 | } | |
72 | } | |
73 | ||
74 | $this->_coid = CRM_Utils_Request::retrieve('coid', 'Integer', $this, FALSE); | |
75 | if ($this->_coid) { | |
76 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'info'); | |
77 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj'); | |
78 | $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_coid, 'contribution'); | |
79 | } | |
80 | ||
81 | if ($this->_mid) { | |
82 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'info'); | |
83 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_mid, 'membership', 'obj'); | |
84 | $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_mid, 'membership'); | |
85 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
86 | $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_mid, 'membership_type_id'); | |
87 | $this->assign('membershipType', CRM_Utils_Array::value($membershipTypeId, $membershipTypes)); | |
88 | $this->_mode = 'auto_renew'; | |
89 | } | |
90 | ||
91 | if ((!$this->_crid && !$this->_coid && !$this->_mid) || | |
92 | ($this->_subscriptionDetails == CRM_Core_DAO::$_nullObject) | |
93 | ) { | |
94 | CRM_Core_Error::fatal('Required information missing.'); | |
95 | } | |
96 | if (!CRM_Core_Permission::check('edit contributions')) { | |
97 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this, FALSE); | |
98 | if (!CRM_Contact_BAO_Contact_Utils::validChecksum($this->_subscriptionDetails->contact_id, $userChecksum)) { | |
99 | CRM_Core_Error::fatal(ts('You do not have permission to cancel subscription.')); | |
100 | } | |
101 | $this->_selfService = TRUE; | |
102 | } | |
103 | ||
104 | if (!$this->_paymentProcessorObj->isSupported('updateSubscriptionBillingInfo')) { | |
105 | CRM_Core_Error::fatal(ts("%1 processor doesn't support updating subscription billing details.", | |
106 | array(1 => $this->_paymentProcessorObj->_processorName) | |
107 | )); | |
108 | } | |
109 | $this->assign('paymentProcessor', $this->_paymentProcessor); | |
110 | ||
111 | // get the billing location type | |
180409a4 | 112 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
6a488035 TO |
113 | $this->_bltID = array_search('Billing', $locationTypes); |
114 | $this->assign('bltID', $this->_bltID); | |
115 | if (!$this->_bltID) { | |
116 | CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing'))); | |
117 | } | |
118 | ||
119 | $this->assign('frequency_unit', $this->_subscriptionDetails->frequency_unit); | |
120 | $this->assign('frequency_interval', $this->_subscriptionDetails->frequency_interval); | |
121 | $this->assign('amount', $this->_subscriptionDetails->amount); | |
122 | $this->assign('installments', $this->_subscriptionDetails->installments); | |
123 | $this->assign('mode', $this->_mode); | |
124 | ||
125 | // handle context redirection | |
126 | CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext(); | |
127 | } | |
128 | ||
186c9c17 EM |
129 | /** |
130 | * This virtual function is used to set the default values of | |
131 | * various form elements | |
132 | * | |
133 | * access public | |
134 | * | |
135 | * @return array reference to the array of default values | |
136 | * | |
137 | */ | |
138 | /** | |
139 | * @return array | |
140 | */ | |
6a488035 TO |
141 | function setDefaultValues() { |
142 | $this->_defaults = array(); | |
143 | ||
144 | if ($this->_subscriptionDetails->contact_id) { | |
6a488035 TO |
145 | $fields = array(); |
146 | $names = array( | |
147 | 'first_name', 'middle_name', 'last_name', "street_address-{$this->_bltID}", "city-{$this->_bltID}", | |
148 | "postal_code-{$this->_bltID}", "country_id-{$this->_bltID}", "state_province_id-{$this->_bltID}", | |
149 | ); | |
150 | foreach ($names as $name) { | |
151 | $fields[$name] = 1; | |
152 | } | |
153 | $fields["state_province-{$this->_bltID}"] = 1; | |
154 | $fields["country-{$this->_bltID}"] = 1; | |
155 | $fields["email-{$this->_bltID}"] = 1; | |
156 | $fields['email-Primary'] = 1; | |
157 | ||
158 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_subscriptionDetails->contact_id, $fields, $this->_defaults); | |
159 | ||
160 | // use primary email address if billing email address is empty | |
161 | if (empty($this->_defaults["email-{$this->_bltID}"]) && | |
162 | !empty($this->_defaults['email-Primary']) | |
163 | ) { | |
164 | $this->_defaults["email-{$this->_bltID}"] = $this->_defaults['email-Primary']; | |
165 | } | |
166 | ||
167 | foreach ($names as $name) { | |
168 | if (!empty($this->_defaults[$name])) { | |
169 | $this->_defaults['billing_' . $name] = $this->_defaults[$name]; | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
6a488035 TO |
174 | $config = CRM_Core_Config::singleton(); |
175 | // set default country from config if no country set | |
a7488080 | 176 | if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) { |
6a488035 TO |
177 | $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
178 | } | |
179 | ||
6a488035 TO |
180 | return $this->_defaults; |
181 | } | |
182 | ||
183 | /** | |
184 | * Function to build the form | |
185 | * | |
355ba699 | 186 | * @return void |
6a488035 TO |
187 | * @access public |
188 | */ | |
189 | public function buildQuickForm() { | |
190 | $type = 'next'; | |
191 | if ( $this->_selfService ) { | |
192 | $type = 'submit'; | |
193 | } | |
194 | ||
195 | $this->addButtons(array( | |
196 | array( | |
197 | 'type' => $type, | |
198 | 'name' => ts('Save'), | |
199 | 'isDefault' => TRUE, | |
200 | ), | |
201 | array( | |
202 | 'type' => 'cancel', | |
203 | 'name' => ts('Cancel'), | |
204 | ), | |
205 | ) | |
206 | ); | |
207 | ||
208 | CRM_Core_Payment_Form::buildCreditCard($this); | |
209 | $this->addFormRule(array('CRM_Contribute_Form_UpdateBilling', 'formRule'), $this); | |
210 | } | |
211 | ||
212 | /** | |
213 | * global form rule | |
214 | * | |
da6b46f4 EM |
215 | * @param array $fields the input form values |
216 | * @param array $files the uploaded files if any | |
217 | * @param $self | |
218 | * | |
219 | * @internal param array $options additional user data | |
6a488035 TO |
220 | * |
221 | * @return true if no errors, else array of errors | |
222 | * @access public | |
223 | * @static | |
224 | */ | |
225 | static function formRule($fields, $files, $self) { | |
7cb3d4f0 CW |
226 | $errors = array(); |
227 | CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors); | |
6a488035 TO |
228 | |
229 | // make sure that credit card number and cvv are valid | |
7cb3d4f0 | 230 | CRM_Core_Payment_Form::validateCreditCard($fields, $errors); |
6a488035 | 231 | |
6a488035 TO |
232 | return empty($errors) ? TRUE : $errors; |
233 | } | |
234 | ||
235 | /** | |
236 | * Process the form | |
237 | * | |
238 | * @return void | |
239 | * @access public | |
240 | */ | |
241 | public function postProcess() { | |
242 | $params = $this->controller->exportValues($this->_name); | |
243 | $status = NULL; | |
244 | ||
245 | // now set the values for the billing location. | |
246 | foreach ($this->_fields as $name => $value) { | |
247 | $fields[$name] = 1; | |
248 | } | |
249 | $fields["email-{$this->_bltID}"] = 1; | |
250 | ||
251 | $processorParams = array(); | |
252 | foreach ($params as $key => $val) { | |
253 | $key = str_replace('billing_', '', $key); | |
254 | list($key) = explode('-', $key); | |
255 | $processorParams[$key] = $val; | |
256 | } | |
257 | $processorParams['state_province'] = CRM_Core_PseudoConstant::stateProvince($params["billing_state_province_id-{$this->_bltID}"], FALSE); | |
258 | $processorParams['country'] = CRM_Core_PseudoConstant::country($params["billing_country_id-{$this->_bltID}"], FALSE); | |
259 | $processorParams['month'] = $processorParams['credit_card_exp_date']['M']; | |
260 | $processorParams['year'] = $processorParams['credit_card_exp_date']['Y']; | |
261 | $processorParams['subscriptionId'] = $this->_subscriptionDetails->subscription_id; | |
262 | $processorParams['amount'] = $this->_subscriptionDetails->amount; | |
263 | ||
264 | $updateSubscription = $this->_paymentProcessorObj->updateSubscriptionBillingInfo($message, $processorParams); | |
265 | ||
266 | if (is_a($updateSubscription, 'CRM_Core_Error')) { | |
267 | CRM_Core_Error::displaySessionError($updateSubscription); | |
268 | } | |
269 | elseif ($updateSubscription) { | |
270 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_subscriptionDetails->contact_id, 'contact_type'); | |
271 | $contact = &CRM_Contact_BAO_Contact::createProfileContact($params, | |
272 | $fields, | |
273 | $this->_subscriptionDetails->contact_id, | |
274 | NULL, | |
275 | NULL, | |
276 | $ctype | |
277 | ); | |
278 | ||
279 | // build tpl params | |
280 | if ($this->_subscriptionDetails->membership_id) { | |
281 | $inputParams = array('id' => $this->_subscriptionDetails->membership_id); | |
282 | CRM_Member_BAO_Membership::getValues($inputParams, $tplParams); | |
283 | $tplParams = $tplParams[$this->_subscriptionDetails->membership_id]; | |
284 | $tplParams['membership_status'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']); | |
285 | $tplParams['membershipType'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']); | |
286 | $status = ts('Billing details for your automatically renewed %1 membership have been updated.', | |
287 | array(1 => $tplParams['membershipType']) | |
288 | ); | |
289 | $msgTitle = ts('Details Updated'); | |
290 | $msgType = 'success'; | |
291 | } | |
292 | else { | |
293 | $status = ts('Billing details for the recurring contribution of %1, every %2 %3 have been updated.', | |
294 | array( | |
295 | 1 => $this->_subscriptionDetails->amount, | |
296 | 2 => $this->_subscriptionDetails->frequency_interval, | |
297 | 3 => $this->_subscriptionDetails->frequency_unit | |
298 | ) | |
299 | ); | |
300 | $msgTitle = ts('Details Updated'); | |
301 | $msgType = 'success'; | |
eea16664 | 302 | |
6a488035 TO |
303 | $tplParams = array( |
304 | 'recur_frequency_interval' => $this->_subscriptionDetails->frequency_interval, | |
305 | 'recur_frequency_unit' => $this->_subscriptionDetails->frequency_unit, | |
306 | 'amount' => $this->_subscriptionDetails->amount, | |
307 | ); | |
308 | } | |
309 | ||
310 | // format new address for display | |
311 | $addressParts = array("street_address", "city", "postal_code", "state_province", "country"); | |
312 | foreach ($addressParts as $part) { | |
313 | $addressParts[$part] = CRM_Utils_Array::value($part, $processorParams); | |
314 | } | |
315 | $tplParams['address'] = CRM_Utils_Address::format($addressParts); | |
316 | ||
317 | // format old address to store in activity details | |
318 | $this->_defaults["state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvince($this->_defaults["state_province-{$this->_bltID}"], FALSE); | |
319 | $this->_defaults["country-{$this->_bltID}"] = CRM_Core_PseudoConstant::country($this->_defaults["country-{$this->_bltID}"], FALSE); | |
320 | $addressParts = array("street_address", "city", "postal_code", "state_province", "country"); | |
321 | foreach ($addressParts as $part) { | |
322 | $key = "{$part}-{$this->_bltID}"; | |
323 | $addressParts[$part] = CRM_Utils_Array::value($key, $this->_defaults); | |
324 | } | |
325 | $this->_defaults['address'] = CRM_Utils_Address::format($addressParts); | |
326 | ||
327 | // format new billing name | |
328 | $name = $processorParams['first_name']; | |
a7488080 | 329 | if (!empty($processorParams['middle_name'])) { |
6a488035 TO |
330 | $name .= " {$processorParams['middle_name']}"; |
331 | } | |
332 | $name .= ' ' . $processorParams['last_name']; | |
333 | $name = trim($name); | |
334 | $tplParams['billingName'] = $name; | |
335 | ||
336 | // format old billing name | |
337 | $name = $this->_defaults['first_name']; | |
a7488080 | 338 | if (!empty($this->_defaults['middle_name'])) { |
6a488035 TO |
339 | $name .= " {$this->_defaults['middle_name']}"; |
340 | } | |
341 | $name .= ' ' . $this->_defaults['last_name']; | |
342 | $name = trim($name); | |
343 | $this->_defaults['billingName'] = $name; | |
344 | ||
345 | $message .= " | |
346 | <br/><br/>New Billing Name and Address | |
347 | <br/>============================== | |
348 | <br/>{$tplParams['billingName']} | |
349 | <br/>{$tplParams['address']} | |
350 | ||
351 | <br/><br/>Previous Billing Name and Address | |
352 | <br/>================================== | |
353 | <br/>{$this->_defaults['billingName']} | |
354 | <br/>{$this->_defaults['address']}"; | |
355 | ||
356 | $activityParams = array( | |
357 | 'source_contact_id' => $this->_subscriptionDetails->contact_id, | |
358 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
359 | 'Update Recurring Contribution Billing Details', | |
360 | 'name' | |
361 | ), | |
362 | 'subject' => ts('Recurring Contribution Billing Details Updated'), | |
363 | 'details' => $message, | |
364 | 'activity_date_time' => date('YmdHis'), | |
365 | 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', | |
366 | 'Completed', | |
367 | 'name' | |
368 | ), | |
369 | ); | |
370 | $session = CRM_Core_Session::singleton(); | |
371 | $cid = $session->get('userID'); | |
372 | if ($cid) { | |
373 | $activityParams['target_contact_id'][] = $activityParams['source_contact_id']; | |
374 | $activityParams['source_contact_id'] = $cid; | |
375 | } | |
376 | CRM_Activity_BAO_Activity::create($activityParams); | |
377 | ||
378 | // send notification | |
379 | if ($this->_subscriptionDetails->contribution_page_id) { | |
380 | CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', | |
381 | $this->_subscriptionDetails->contribution_page_id, $value, array( | |
382 | 'title', | |
383 | 'receipt_from_name', | |
384 | 'receipt_from_email', | |
385 | ) | |
386 | ); | |
387 | $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . '" <' . $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . '>'; | |
388 | } | |
389 | else { | |
390 | $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); | |
391 | $receiptFrom = "$domainValues[0] <$domainValues[1]>"; | |
392 | } | |
393 | list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id); | |
394 | $tplParams['contact'] = array('display_name' => $donorDisplayName); | |
395 | ||
396 | $date = CRM_Utils_Date::format($processorParams['credit_card_exp_date']); | |
397 | $tplParams['credit_card_exp_date'] = CRM_Utils_Date::mysqlToIso($date); | |
398 | $tplParams['credit_card_number'] = CRM_Utils_System::mungeCreditCard($processorParams['credit_card_number']); | |
399 | $tplParams['credit_card_type'] = $processorParams['credit_card_type']; | |
400 | ||
401 | $sendTemplateParams = array( | |
402 | 'groupName' => $this->_subscriptionDetails->membership_id ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution', | |
403 | 'valueName' => $this->_subscriptionDetails->membership_id ? 'membership_autorenew_billing' : 'contribution_recurring_billing', | |
404 | 'contactId' => $this->_subscriptionDetails->contact_id, | |
405 | 'tplParams' => $tplParams, | |
406 | 'isTest' => $this->_subscriptionDetails->is_test, | |
407 | 'PDFFilename' => 'receipt.pdf', | |
408 | 'from' => $receiptFrom, | |
409 | 'toName' => $donorDisplayName, | |
410 | 'toEmail' => $donorEmail, | |
411 | ); | |
c6327d7d | 412 | list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
6a488035 TO |
413 | } |
414 | else { | |
415 | $status = ts('There was some problem updating the billing details.'); | |
416 | $msgTitle = ts('Update Error'); | |
417 | $msgType = 'error'; | |
418 | } | |
419 | ||
420 | $session = CRM_Core_Session::singleton(); | |
421 | $userID = $session->get('userID'); | |
422 | if ( $userID && $status) { | |
423 | $session->setStatus($status, $msgTitle, $msgType); | |
424 | } else if (!$userID) { | |
eea16664 | 425 | if ($status) |
6a488035 TO |
426 | CRM_Utils_System::setUFMessage($status); |
427 | $result = (int) ($updateSubscription && isset($ctype)); | |
eea16664 | 428 | if (isset($tplParams)) |
6a488035 | 429 | $session->set('resultParams', $tplParams); |
eea16664 | 430 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus', |
6a488035 TO |
431 | "reset=1&task=billing&result={$result}")); |
432 | } | |
433 | } | |
434 | } | |
435 |