Merge pull request #23258 from civicrm/5.49
[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
9cff1ee2
CC
70 $paymentProcessorBillingFields = array_keys($form->_paymentProcessor['object']->getBillingAddressFields());
71
423b9af4 72 if (!empty($form->_values['custom_pre_id'])) {
be2fb01f 73 $profileAddressFields = [];
423b9af4 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) {
9cff1ee2 78 CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, ['uf_group_id' => $form->_values['custom_pre_id']], $paymentProcessorBillingFields);
423b9af4 79 }
80 if (count($profileAddressFields)) {
81 $form->set('profileAddressFields', $profileAddressFields);
82 }
83 }
84
6a488035
TO
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)
1d1fee72 88 CRM_Core_Payment_Form::setPaymentFieldsByProcessor(
89 $form,
90 $form->_paymentProcessor,
18135422 91 CRM_Utils_Request::retrieve('billing_profile_id', 'String'),
92 $form->isBackOffice,
93 $form->paymentInstrumentID
1d1fee72 94 );
6a488035 95
6a488035
TO
96 $form->assign_by_ref('paymentProcessor', $form->_paymentProcessor);
97
98 // check if this is a paypal auto return and redirect accordingly
dde5a0ef 99 //@todo - determine if this is legacy and remove
6a488035
TO
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
a7488080 108 if (!empty($form->_values['is_monetary']) &&
353ffa53
TO
109 !$form->_paymentProcessor['class_name'] && empty($form->_values['is_pay_later'])
110 ) {
2d296f18 111 CRM_Core_Error::statusBounce(ts('Payment processor is not set for this page'));
6a488035
TO
112 }
113
8cc574cf
CW
114 if (!empty($form->_membershipBlock) && !empty($form->_membershipBlock['is_separate_payment']) &&
115 (!empty($form->_paymentProcessor['class_name']) &&
75ead8de 116 !$form->_paymentObject->supports('MultipleConcurrentPayments')
6a488035
TO
117 )
118 ) {
119
2d296f18 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',
be2fb01f 121 [1 => $form->_paymentProcessor['payment_processor_type']]
c48c8f82
CW
122 )
123 );
6a488035 124 }
6a488035
TO
125 }
126
6c786a9b 127 /**
9d421118 128 * Build the payment processor form.
129 *
130 * @param CRM_Core_Form $form
6c786a9b 131 */
18135422 132 public static function buildQuickForm(&$form) {
dde5a0ef 133 //@todo document why this addHidden is here
01da20ea 134 //CRM-15743 - we should not set/create hidden element for pay later
135 // because payment processor is not selected
8bf11c13 136 $processorId = $form->getVar('_paymentProcessorID');
1d1fee72 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 }
8bf11c13 141 if (!empty($processorId)) {
d1fcde00 142 $form->addElement('hidden', 'hidden_processor', 1);
143 }
c8062900 144 CRM_Core_Payment_Form::buildPaymentForm($form, $form->_paymentProcessor, $billing_profile_id, $form->isBackOffice, $form->paymentInstrumentID ?? NULL);
6a488035 145 }
96025800 146
6a488035 147}