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