Merge pull request #13157 from eileenmcnaughton/currency
[civicrm-core.git] / CRM / Core / Payment / ProcessorForm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 use Civi\Payment\System;
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2018
33 */
34
35
36 /**
37 * Base class for building payment block for online contribution / event pages.
38 */
39 class CRM_Core_Payment_ProcessorForm {
40
41 /**
42 * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register|CRM_Financial_Form_Payment $form
43 * @param null $type
44 * @param null $mode
45 *
46 * @throws Exception
47 */
48 public static function preProcess(&$form, $type = NULL, $mode = NULL) {
49 if ($type) {
50 $form->_type = $type;
51 }
52 else {
53 $form->_type = CRM_Utils_Request::retrieve('type', 'String', $form);
54 }
55
56 if ($form->_type) {
57 // @todo not sure when this would be true. Never passed in.
58 $form->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($form->_type, $form->_mode);
59 }
60
61 if (empty($form->_paymentProcessor)) {
62 // This would happen when hitting the back-button on a multi-page form with a $0 selection in play.
63 return;
64 }
65 $form->set('paymentProcessor', $form->_paymentProcessor);
66 $form->_paymentObject = System::singleton()->getByProcessor($form->_paymentProcessor);
67 if ($form->paymentInstrumentID) {
68 $form->_paymentObject->setPaymentInstrumentID($form->paymentInstrumentID);
69 }
70 $form->_paymentObject->setBackOffice($form->isBackOffice);
71 $form->assign('isBackOffice', $form->isBackOffice);
72
73 $form->assign('suppressSubmitButton', $form->_paymentObject->isSuppressSubmitButtons());
74
75 $form->assign('currency', $form->getCurrency());
76
77 // also set cancel subscription url
78 if (!empty($form->_paymentProcessor['is_recur']) && !empty($form->_values['is_recur'])) {
79 $form->_values['cancelSubscriptionUrl'] = $form->_paymentObject->subscriptionURL(NULL, NULL, 'cancel');
80 }
81
82 if (!empty($form->_values['custom_pre_id'])) {
83 $profileAddressFields = array();
84 $fields = CRM_Core_BAO_UFGroup::getFields($form->_values['custom_pre_id'], FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE,
85 NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL);
86
87 foreach ((array) $fields as $key => $value) {
88 CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, array('uf_group_id' => $form->_values['custom_pre_id']));
89 }
90 if (count($profileAddressFields)) {
91 $form->set('profileAddressFields', $profileAddressFields);
92 }
93 }
94
95 //checks after setting $form->_paymentProcessor
96 // we do this outside of the above conditional to avoid
97 // saving the country/state list in the session (which could be huge)
98 CRM_Core_Payment_Form::setPaymentFieldsByProcessor(
99 $form,
100 $form->_paymentProcessor,
101 CRM_Utils_Request::retrieve('billing_profile_id', 'String'),
102 $form->isBackOffice,
103 $form->paymentInstrumentID
104 );
105
106 $form->assign_by_ref('paymentProcessor', $form->_paymentProcessor);
107
108 // check if this is a paypal auto return and redirect accordingly
109 //@todo - determine if this is legacy and remove
110 if (CRM_Core_Payment::paypalRedirect($form->_paymentProcessor)) {
111 $url = CRM_Utils_System::url('civicrm/contribute/transact',
112 "_qf_ThankYou_display=1&qfKey={$form->controller->_key}"
113 );
114 CRM_Utils_System::redirect($url);
115 }
116
117 // make sure we have a valid payment class, else abort
118 if (!empty($form->_values['is_monetary']) &&
119 !$form->_paymentProcessor['class_name'] && empty($form->_values['is_pay_later'])
120 ) {
121 CRM_Core_Error::fatal(ts('Payment processor is not set for this page'));
122 }
123
124 if (!empty($form->_membershipBlock) && !empty($form->_membershipBlock['is_separate_payment']) &&
125 (!empty($form->_paymentProcessor['class_name']) &&
126 !$form->_paymentObject->supports('MultipleConcurrentPayments')
127 )
128 ) {
129
130 CRM_Core_Error::fatal(ts('This contribution page is configured to support separate contribution and membership payments. This %1 plugin does not currently support multiple simultaneous payments, or the option to "Execute real-time monetary transactions" is disabled. Please contact the site administrator and notify them of this error',
131 array(1 => $form->_paymentProcessor['payment_processor_type'])
132 )
133 );
134 }
135 }
136
137 /**
138 * Build the payment processor form.
139 *
140 * @param CRM_Core_Form $form
141 */
142 public static function buildQuickForm(&$form) {
143 //@todo document why this addHidden is here
144 //CRM-15743 - we should not set/create hidden element for pay later
145 // because payment processor is not selected
146 $processorId = $form->getVar('_paymentProcessorID');
147 $billing_profile_id = CRM_Utils_Request::retrieve('billing_profile_id', 'String');
148 if (!empty($form->_values) && !empty($form->_values['is_billing_required'])) {
149 $billing_profile_id = 'billing';
150 }
151 if (!empty($processorId)) {
152 $form->addElement('hidden', 'hidden_processor', 1);
153 }
154 CRM_Core_Payment_Form::buildPaymentForm($form, $form->_paymentProcessor, $billing_profile_id, $form->isBackOffice, $form->paymentInstrumentID);
155 }
156
157 }