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