Merge pull request #15818 from colemanw/fields
[civicrm-core.git] / CRM / Core / Payment / ProcessorForm.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
31d31a05 12use Civi\Payment\System;
518fa0ee 13
6a488035
TO
14/**
15 *
16 * @package CRM
ca5cec67 17 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
18 */
19
31d31a05 20
6a488035 21/**
31d31a05 22 * Base class for building payment block for online contribution / event pages.
6a488035
TO
23 */
24class CRM_Core_Payment_ProcessorForm {
25
6c786a9b 26 /**
a9768188 27 * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register|CRM_Financial_Form_Payment $form
6c786a9b
EM
28 * @param null $type
29 * @param null $mode
30 *
31 * @throws Exception
32 */
2aa397bc 33 public static function preProcess(&$form, $type = NULL, $mode = NULL) {
6a488035
TO
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) {
18135422 42 // @todo not sure when this would be true. Never passed in.
6a488035
TO
43 $form->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($form->_type, $form->_mode);
44 }
45
986905f5 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 }
6a488035 50 $form->set('paymentProcessor', $form->_paymentProcessor);
7a3b0ca3 51 $form->_paymentObject = System::singleton()->getByProcessor($form->_paymentProcessor);
18135422 52 if ($form->paymentInstrumentID) {
53 $form->_paymentObject->setPaymentInstrumentID($form->paymentInstrumentID);
54 }
55 $form->_paymentObject->setBackOffice($form->isBackOffice);
56 $form->assign('isBackOffice', $form->isBackOffice);
1ba4a3aa
EM
57
58 $form->assign('suppressSubmitButton', $form->_paymentObject->isSuppressSubmitButtons());
6a488035 59
2204d007
MWMC
60 CRM_Financial_Form_Payment::addCreditCardJs($form->getPaymentProcessorID());
61 $form->assign('paymentProcessorID', $form->getPaymentProcessorID());
62
d02c6418 63 $form->assign('currency', $form->getCurrency());
f61437d3 64
6a488035 65 // also set cancel subscription url
8cc574cf 66 if (!empty($form->_paymentProcessor['is_recur']) && !empty($form->_values['is_recur'])) {
3e473c0b 67 $form->_values['cancelSubscriptionUrl'] = $form->_paymentObject->subscriptionURL(NULL, NULL, 'cancel');
6a488035
TO
68 }
69
423b9af4 70 if (!empty($form->_values['custom_pre_id'])) {
be2fb01f 71 $profileAddressFields = [];
423b9af4 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) {
be2fb01f 76 CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, ['uf_group_id' => $form->_values['custom_pre_id']]);
423b9af4 77 }
78 if (count($profileAddressFields)) {
79 $form->set('profileAddressFields', $profileAddressFields);
80 }
81 }
82
6a488035
TO
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)
1d1fee72 86 CRM_Core_Payment_Form::setPaymentFieldsByProcessor(
87 $form,
88 $form->_paymentProcessor,
18135422 89 CRM_Utils_Request::retrieve('billing_profile_id', 'String'),
90 $form->isBackOffice,
91 $form->paymentInstrumentID
1d1fee72 92 );
6a488035 93
6a488035
TO
94 $form->assign_by_ref('paymentProcessor', $form->_paymentProcessor);
95
96 // check if this is a paypal auto return and redirect accordingly
dde5a0ef 97 //@todo - determine if this is legacy and remove
6a488035
TO
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
a7488080 106 if (!empty($form->_values['is_monetary']) &&
353ffa53
TO
107 !$form->_paymentProcessor['class_name'] && empty($form->_values['is_pay_later'])
108 ) {
6a488035
TO
109 CRM_Core_Error::fatal(ts('Payment processor is not set for this page'));
110 }
111
8cc574cf
CW
112 if (!empty($form->_membershipBlock) && !empty($form->_membershipBlock['is_separate_payment']) &&
113 (!empty($form->_paymentProcessor['class_name']) &&
75ead8de 114 !$form->_paymentObject->supports('MultipleConcurrentPayments')
6a488035
TO
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',
be2fb01f 119 [1 => $form->_paymentProcessor['payment_processor_type']]
c48c8f82
CW
120 )
121 );
6a488035 122 }
6a488035
TO
123 }
124
6c786a9b 125 /**
9d421118 126 * Build the payment processor form.
127 *
128 * @param CRM_Core_Form $form
6c786a9b 129 */
18135422 130 public static function buildQuickForm(&$form) {
dde5a0ef 131 //@todo document why this addHidden is here
01da20ea 132 //CRM-15743 - we should not set/create hidden element for pay later
133 // because payment processor is not selected
8bf11c13 134 $processorId = $form->getVar('_paymentProcessorID');
1d1fee72 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 }
8bf11c13 139 if (!empty($processorId)) {
d1fcde00 140 $form->addElement('hidden', 'hidden_processor', 1);
141 }
18135422 142 CRM_Core_Payment_Form::buildPaymentForm($form, $form->_paymentProcessor, $billing_profile_id, $form->isBackOffice, $form->paymentInstrumentID);
6a488035 143 }
96025800 144
6a488035 145}