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 +--------------------------------------------------------------------+
12 use Civi\Payment\System
;
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 * Base class for building payment block for online contribution / event pages.
24 class CRM_Core_Payment_ProcessorForm
{
27 * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register|CRM_Financial_Form_Payment $form
33 public static function preProcess(&$form, $type = NULL, $mode = NULL) {
38 $form->_type
= CRM_Utils_Request
::retrieve('type', 'String', $form);
42 // @todo not sure when this would be true. Never passed in.
43 $form->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($form->_type
, $form->_mode
);
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.
50 $form->set('paymentProcessor', $form->_paymentProcessor
);
51 $form->_paymentObject
= System
::singleton()->getByProcessor($form->_paymentProcessor
);
52 if ($form->paymentInstrumentID
) {
53 $form->_paymentObject
->setPaymentInstrumentID($form->paymentInstrumentID
);
55 $form->_paymentObject
->setBackOffice($form->isBackOffice
);
56 $form->assign('isBackOffice', $form->isBackOffice
);
58 $form->assign('suppressSubmitButton', $form->_paymentObject
->isSuppressSubmitButtons());
60 CRM_Financial_Form_Payment
::addCreditCardJs($form->getPaymentProcessorID());
61 $form->assign('paymentProcessorID', $form->getPaymentProcessorID());
63 $form->assign('currency', $form->getCurrency());
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');
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);
75 foreach ((array) $fields as $key => $value) {
76 CRM_Core_BAO_UFField
::assignAddressField($key, $profileAddressFields, ['uf_group_id' => $form->_values
['custom_pre_id']]);
78 if (count($profileAddressFields)) {
79 $form->set('profileAddressFields', $profileAddressFields);
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(
88 $form->_paymentProcessor
,
89 CRM_Utils_Request
::retrieve('billing_profile_id', 'String'),
91 $form->paymentInstrumentID
94 $form->assign_by_ref('paymentProcessor', $form->_paymentProcessor
);
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}"
102 CRM_Utils_System
::redirect($url);
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'])
109 CRM_Core_Error
::statusBounce(ts('Payment processor is not set for this page'));
112 if (!empty($form->_membershipBlock
) && !empty($form->_membershipBlock
['is_separate_payment']) &&
113 (!empty($form->_paymentProcessor
['class_name']) &&
114 !$form->_paymentObject
->supports('MultipleConcurrentPayments')
118 CRM_Core_Error
::statusBounce(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']]
126 * Build the payment processor form.
128 * @param CRM_Core_Form $form
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';
139 if (!empty($processorId)) {
140 $form->addElement('hidden', 'hidden_processor', 1);
142 CRM_Core_Payment_Form
::buildPaymentForm($form, $form->_paymentProcessor
, $billing_profile_id, $form->isBackOffice
, $form->paymentInstrumentID ??
NULL);