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