3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for processing a contribution.
21 class CRM_Contribute_Form_UpdateBilling
extends CRM_Contribute_Form_ContributionRecur
{
22 protected $_mode = NULL;
24 protected $_subscriptionDetails = NULL;
26 public $_bltID = NULL;
29 * Set variables up before form is built.
31 * @throws \CRM_Core_Exception
33 public function preProcess() {
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
;
43 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'info');
44 $this->_paymentProcessor
['object'] = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
48 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'info');
49 $this->_paymentProcessor
['object'] = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_mid
, 'membership', 'obj');
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';
57 if ((!$this->_crid
&& !$this->_coid
&& !$this->_mid
) ||
(!$this->_subscriptionDetails
)) {
58 throw new CRM_Core_Exception('Required information missing.');
61 if (!$this->_paymentProcessor
['object']->supports('updateSubscriptionBillingInfo')) {
62 throw new CRM_Core_Exception(ts("%1 processor doesn't support updating subscription billing details.",
63 [1 => $this->_paymentProcessor
['title']]
66 $this->assign('paymentProcessor', $this->_paymentProcessor
);
68 $this->assignBillingType();
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
);
76 // handle context redirection
77 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
81 * Set the default values of various form elements.
86 public function setDefaultValues() {
87 $this->_defaults
= [];
89 if ($this->_subscriptionDetails
->contact_id
) {
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}",
101 foreach ($names as $name) {
104 $fields["state_province-{$this->_bltID}"] = 1;
105 $fields["country-{$this->_bltID}"] = 1;
106 $fields["email-{$this->_bltID}"] = 1;
107 $fields['email-Primary'] = 1;
109 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_subscriptionDetails
->contact_id
, $fields, $this->_defaults
);
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'])
115 $this->_defaults
["email-{$this->_bltID}"] = $this->_defaults
['email-Primary'];
118 foreach ($names as $name) {
119 if (!empty($this->_defaults
[$name])) {
120 $this->_defaults
['billing_' . $name] = $this->_defaults
[$name];
125 $config = CRM_Core_Config
::singleton();
126 // set default country from config if no country set
127 if (empty($this->_defaults
["billing_country_id-{$this->_bltID}"])) {
128 $this->_defaults
["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
131 return $this->_defaults
;
135 * Build the form object.
137 public function buildQuickForm() {
139 if ($this->isSelfService()) {
143 $this->addButtons(array(
146 'name' => ts('Save'),
151 'name' => ts('Cancel'),
155 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, TRUE, TRUE);
156 $this->addFormRule(array('CRM_Contribute_Form_UpdateBilling', 'formRule'), $this);
162 * @param array $fields
163 * The input form values.
164 * @param array $files
165 * The uploaded files if any.
166 * @param CRM_Core_Form $self
170 * true if no errors, else array of errors
172 public static function formRule($fields, $files, $self) {
174 CRM_Core_Form
::validateMandatoryFields($self->_fields
, $fields, $errors);
176 // validate the payment instrument values (e.g. credit card number)
177 CRM_Core_Payment_Form
::validatePaymentInstrument($self->_paymentProcessor
['id'], $fields, $errors, NULL);
179 return empty($errors) ?
TRUE : $errors;
185 public function postProcess() {
186 $params = $this->controller
->exportValues($this->_name
);
189 // now set the values for the billing location.
190 foreach ($this->_fields
as $name => $value) {
193 $fields["email-{$this->_bltID}"] = 1;
195 $processorParams = [];
196 foreach ($params as $key => $val) {
197 $key = str_replace('billing_', '', $key);
198 list($key) = explode('-', $key);
199 $processorParams[$key] = $val;
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'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($processorParams);
204 $processorParams['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($processorParams);
205 $processorParams['subscriptionId'] = $this->getSubscriptionDetails()->processor_id
;
206 $processorParams['amount'] = $this->_subscriptionDetails
->amount
;
208 $updateSubscription = $this->_paymentProcessor
['object']->updateSubscriptionBillingInfo($message, $processorParams);
209 if (is_a($updateSubscription, 'CRM_Core_Error')) {
210 CRM_Core_Error
::displaySessionError($updateSubscription);
212 elseif ($updateSubscription) {
213 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_subscriptionDetails
->contact_id
, 'contact_type');
214 $contact = &CRM_Contact_BAO_Contact
::createProfileContact($params,
216 $this->_subscriptionDetails
->contact_id
,
223 if ($this->_subscriptionDetails
->membership_id
) {
224 $inputParams = array('id' => $this->_subscriptionDetails
->membership_id
);
225 CRM_Member_BAO_Membership
::getValues($inputParams, $tplParams);
226 $tplParams = $tplParams[$this->_subscriptionDetails
->membership_id
];
227 $tplParams['membership_status'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', $tplParams['status_id']);
228 $tplParams['membershipType'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $tplParams['membership_type_id']);
229 $status = ts('Billing details for your automatically renewed %1 membership have been updated.',
230 array(1 => $tplParams['membershipType'])
232 $msgTitle = ts('Details Updated');
233 $msgType = 'success';
236 $status = ts('Billing details for the recurring contribution of %1, every %2 %3 have been updated.',
238 1 => $this->_subscriptionDetails
->amount
,
239 2 => $this->_subscriptionDetails
->frequency_interval
,
240 3 => $this->_subscriptionDetails
->frequency_unit
,
243 $msgTitle = ts('Details Updated');
244 $msgType = 'success';
247 'recur_frequency_interval' => $this->_subscriptionDetails
->frequency_interval
,
248 'recur_frequency_unit' => $this->_subscriptionDetails
->frequency_unit
,
249 'amount' => $this->_subscriptionDetails
->amount
,
253 // format new address for display
254 $addressParts = array("street_address", "city", "postal_code", "state_province", "country");
255 foreach ($addressParts as $part) {
256 $addressParts[$part] = $processorParams[$part] ??
NULL;
258 $tplParams['address'] = CRM_Utils_Address
::format($addressParts);
260 // format old address to store in activity details
261 $this->_defaults
["state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvince($this->_defaults
["state_province-{$this->_bltID}"], FALSE);
262 $this->_defaults
["country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::country($this->_defaults
["country-{$this->_bltID}"], FALSE);
263 $addressParts = array("street_address", "city", "postal_code", "state_province", "country");
264 foreach ($addressParts as $part) {
265 $key = "{$part}-{$this->_bltID}";
266 $addressParts[$part] = $this->_defaults
[$key] ??
NULL;
268 $this->_defaults
['address'] = CRM_Utils_Address
::format($addressParts);
270 // format new billing name
271 $name = $processorParams['first_name'];
272 if (!empty($processorParams['middle_name'])) {
273 $name .= " {$processorParams['middle_name']}";
275 $name .= ' ' . $processorParams['last_name'];
277 $tplParams['billingName'] = $name;
279 // format old billing name
280 $name = $this->_defaults
['first_name'];
281 if (!empty($this->_defaults
['middle_name'])) {
282 $name .= " {$this->_defaults['middle_name']}";
284 $name .= ' ' . $this->_defaults
['last_name'];
286 $this->_defaults
['billingName'] = $name;
289 <br/><br/>New Billing Name and Address
290 <br/>==============================
291 <br/>{$tplParams['billingName']}
292 <br/>{$tplParams['address']}
294 <br/><br/>Previous Billing Name and Address
295 <br/>==================================
296 <br/>{$this->_defaults['billingName']}
297 <br/>{$this->_defaults['address']}";
299 $activityParams = array(
300 'source_contact_id' => $this->_subscriptionDetails
->contact_id
,
301 'activity_type_id' => CRM_Core_PseudoConstant
::getKey(
302 'CRM_Activity_BAO_Activity',
304 'Update Recurring Contribution Billing Details'
306 'subject' => ts('Recurring Contribution Billing Details Updated'),
307 'details' => $message,
308 'activity_date_time' => date('YmdHis'),
309 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'),
311 $session = CRM_Core_Session
::singleton();
312 $cid = $session->get('userID');
314 $activityParams['target_contact_id'][] = $activityParams['source_contact_id'];
315 $activityParams['source_contact_id'] = $cid;
317 CRM_Activity_BAO_Activity
::create($activityParams);
320 if ($this->_subscriptionDetails
->contribution_page_id
) {
321 CRM_Core_DAO
::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
322 $this->_subscriptionDetails
->contribution_page_id
, $value, array(
325 'receipt_from_email',
328 $receiptFrom = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->_subscriptionDetails
->contribution_page_id
]) . '" <' . $value[$this->_subscriptionDetails
->contribution_page_id
]['receipt_from_email'] . '>';
331 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
332 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
334 list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact
::getContactDetails($this->_subscriptionDetails
->contact_id
);
335 $tplParams['contact'] = array('display_name' => $donorDisplayName);
337 $tplParams = array_merge($tplParams, CRM_Contribute_Form_AbstractEditPayment
::formatCreditCardDetails($processorParams));
339 $sendTemplateParams = array(
340 'groupName' => $this->_subscriptionDetails
->membership_id ?
'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution',
341 'valueName' => $this->_subscriptionDetails
->membership_id ?
'membership_autorenew_billing' : 'contribution_recurring_billing',
342 'contactId' => $this->_subscriptionDetails
->contact_id
,
343 'tplParams' => $tplParams,
344 'isTest' => $this->_subscriptionDetails
->is_test
,
345 'PDFFilename' => 'receipt.pdf',
346 'from' => $receiptFrom,
347 'toName' => $donorDisplayName,
348 'toEmail' => $donorEmail,
350 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
353 $status = ts('There was some problem updating the billing details.');
354 $msgTitle = ts('Update Error');
358 $session = CRM_Core_Session
::singleton();
359 $userID = $session->get('userID');
360 if ($userID && $status) {
361 $session->setStatus($status, $msgTitle, $msgType);
365 CRM_Utils_System
::setUFMessage($status);
367 $result = (int) ($updateSubscription && isset($ctype));
368 if (isset($tplParams)) {
369 $session->set('resultParams', $tplParams);
371 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/subscriptionstatus',
372 "reset=1&task=billing&result={$result}"));