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