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