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