| 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 | $paymentProcessorBillingFields = array_keys($form->_paymentProcessor['object']->getBillingAddressFields()); |
| 71 | |
| 72 | if (!empty($form->_values['custom_pre_id'])) { |
| 73 | $profileAddressFields = []; |
| 74 | $fields = CRM_Core_BAO_UFGroup::getFields($form->_values['custom_pre_id'], FALSE, CRM_Core_Action::ADD, NULL, NULL, FALSE, |
| 75 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL); |
| 76 | |
| 77 | foreach ((array) $fields as $key => $value) { |
| 78 | CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, ['uf_group_id' => $form->_values['custom_pre_id']], $paymentProcessorBillingFields); |
| 79 | } |
| 80 | if (count($profileAddressFields)) { |
| 81 | $form->set('profileAddressFields', $profileAddressFields); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | //checks after setting $form->_paymentProcessor |
| 86 | // we do this outside of the above conditional to avoid |
| 87 | // saving the country/state list in the session (which could be huge) |
| 88 | CRM_Core_Payment_Form::setPaymentFieldsByProcessor( |
| 89 | $form, |
| 90 | $form->_paymentProcessor, |
| 91 | CRM_Utils_Request::retrieve('billing_profile_id', 'String'), |
| 92 | $form->isBackOffice, |
| 93 | $form->paymentInstrumentID |
| 94 | ); |
| 95 | |
| 96 | $form->assign_by_ref('paymentProcessor', $form->_paymentProcessor); |
| 97 | |
| 98 | // check if this is a paypal auto return and redirect accordingly |
| 99 | //@todo - determine if this is legacy and remove |
| 100 | if (CRM_Core_Payment::paypalRedirect($form->_paymentProcessor)) { |
| 101 | $url = CRM_Utils_System::url('civicrm/contribute/transact', |
| 102 | "_qf_ThankYou_display=1&qfKey={$form->controller->_key}" |
| 103 | ); |
| 104 | CRM_Utils_System::redirect($url); |
| 105 | } |
| 106 | |
| 107 | // make sure we have a valid payment class, else abort |
| 108 | if (!empty($form->_values['is_monetary']) && |
| 109 | !$form->_paymentProcessor['class_name'] && empty($form->_values['is_pay_later']) |
| 110 | ) { |
| 111 | CRM_Core_Error::statusBounce(ts('Payment processor is not set for this page')); |
| 112 | } |
| 113 | |
| 114 | if (!empty($form->_membershipBlock) && !empty($form->_membershipBlock['is_separate_payment']) && |
| 115 | (!empty($form->_paymentProcessor['class_name']) && |
| 116 | !$form->_paymentObject->supports('MultipleConcurrentPayments') |
| 117 | ) |
| 118 | ) { |
| 119 | |
| 120 | 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', |
| 121 | [1 => $form->_paymentProcessor['payment_processor_type']] |
| 122 | ) |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Build the payment processor form. |
| 129 | * |
| 130 | * @param CRM_Core_Form $form |
| 131 | */ |
| 132 | public static function buildQuickForm(&$form) { |
| 133 | //@todo document why this addHidden is here |
| 134 | //CRM-15743 - we should not set/create hidden element for pay later |
| 135 | // because payment processor is not selected |
| 136 | $processorId = $form->getVar('_paymentProcessorID'); |
| 137 | $billing_profile_id = CRM_Utils_Request::retrieve('billing_profile_id', 'String'); |
| 138 | if (!empty($form->_values) && !empty($form->_values['is_billing_required'])) { |
| 139 | $billing_profile_id = 'billing'; |
| 140 | } |
| 141 | if (!empty($processorId)) { |
| 142 | $form->addElement('hidden', 'hidden_processor', 1); |
| 143 | } |
| 144 | CRM_Core_Payment_Form::buildPaymentForm($form, $form->_paymentProcessor, $billing_profile_id, $form->isBackOffice, $form->paymentInstrumentID ?? NULL); |
| 145 | } |
| 146 | |
| 147 | } |