| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
| 32 | */ |
| 33 | class CRM_Core_Payment_Manual extends CRM_Core_Payment { |
| 34 | |
| 35 | protected $result; |
| 36 | |
| 37 | /** |
| 38 | * This function checks to see if we have the right config values. |
| 39 | */ |
| 40 | public function checkConfig() {} |
| 41 | |
| 42 | /** |
| 43 | * Get billing fields required for this processor. |
| 44 | * |
| 45 | * We apply the existing default of returning fields only for payment processor type 1. Processors can override to |
| 46 | * alter. |
| 47 | * |
| 48 | * @param int $billingLocationID |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | public function getBillingAddressFields($billingLocationID = NULL) { |
| 53 | if (!$billingLocationID) { |
| 54 | // Note that although the billing id is passed around the forms the idea that it would be anything other than |
| 55 | // the result of the function below doesn't seem to have eventuated. |
| 56 | // So taking this as a param is possibly something to be removed in favour of the standard default. |
| 57 | $billingLocationID = CRM_Core_BAO_LocationType::getBilling(); |
| 58 | } |
| 59 | |
| 60 | // Only handle pseudo-profile billing for now. |
| 61 | if ($this->billingProfile == 'billing') { |
| 62 | // @todo - use profile api to retrieve this - either as pseudo-profile or (better) set up billing |
| 63 | // as a reserved profile in the DB and (even better) allow the profile to be selected |
| 64 | // on the form instead of just 'billing for pay=later bool' |
| 65 | return [ |
| 66 | 'first_name' => 'billing_first_name', |
| 67 | 'middle_name' => 'billing_middle_name', |
| 68 | 'last_name' => 'billing_last_name', |
| 69 | 'street_address' => "billing_street_address-{$billingLocationID}", |
| 70 | 'city' => "billing_city-{$billingLocationID}", |
| 71 | 'country' => "billing_country_id-{$billingLocationID}", |
| 72 | 'state_province' => "billing_state_province_id-{$billingLocationID}", |
| 73 | 'postal_code' => "billing_postal_code-{$billingLocationID}", |
| 74 | ]; |
| 75 | } |
| 76 | else { |
| 77 | return []; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get array of fields that should be displayed on the payment form. |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | public function getPaymentFormFields() { |
| 87 | if (!$this->isBackOffice()) { |
| 88 | return []; |
| 89 | } |
| 90 | |
| 91 | $paymentInstrument = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID()); |
| 92 | if ($paymentInstrument === 'Credit Card') { |
| 93 | return ['credit_card_type', 'pan_truncation']; |
| 94 | } |
| 95 | elseif ($paymentInstrument === 'Check') { |
| 96 | return ['check_number']; |
| 97 | } |
| 98 | return []; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Process payment. |
| 103 | * |
| 104 | * The function ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms |
| 105 | * more agnostic. |
| 106 | * |
| 107 | * @param array $params |
| 108 | * |
| 109 | * @param string $component |
| 110 | * |
| 111 | * @return array |
| 112 | * Result array |
| 113 | * |
| 114 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
| 115 | */ |
| 116 | public function doPayment(&$params, $component = 'contribute') { |
| 117 | $params['payment_status_id'] = $this->getResult(); |
| 118 | return $params; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the result of the payment. |
| 123 | * |
| 124 | * Usually this will be pending but the calling layer has a chance to set the result. |
| 125 | * |
| 126 | * This would apply in particular when the form accepts status id. |
| 127 | * |
| 128 | * Note that currently this payment class is only being used to manage the 'billing block' aspect |
| 129 | * of pay later. However, a longer term idea is that by treating 'pay-later' as 'just another processor' |
| 130 | * will allow code simplification. |
| 131 | * |
| 132 | * @return int |
| 133 | */ |
| 134 | protected function getResult() { |
| 135 | if (!$this->result) { |
| 136 | $this->setResult(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'status_id', 'Pending')); |
| 137 | } |
| 138 | return $this->result; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Set the result to be returned. |
| 143 | * |
| 144 | * This would be set from outside the function where we want to pass on the status from the form. |
| 145 | * |
| 146 | * @param int $result |
| 147 | */ |
| 148 | public function setResult($result) { |
| 149 | $this->result = $result; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set payment instrument id. |
| 154 | * |
| 155 | * @param int $paymentInstrumentID |
| 156 | */ |
| 157 | public function setPaymentInstrumentID($paymentInstrumentID) { |
| 158 | $this->paymentInstrumentID = $paymentInstrumentID; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the name of the payment type. |
| 163 | * |
| 164 | * @return string |
| 165 | */ |
| 166 | public function getPaymentTypeName() { |
| 167 | return 'pay-later'; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get the name of the payment type. |
| 172 | * |
| 173 | * @return string |
| 174 | */ |
| 175 | public function getPaymentTypeLabel() { |
| 176 | return CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID()); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Declare that more than one payment can be processed at once. |
| 181 | * |
| 182 | * @return bool |
| 183 | */ |
| 184 | protected function supportsMultipleConcurrentPayments() { |
| 185 | return TRUE; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Checks if backoffice recurring edit is allowed |
| 190 | * |
| 191 | * @return bool |
| 192 | */ |
| 193 | public function supportsEditRecurringContribution() { |
| 194 | return TRUE; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Are back office payments supported. |
| 199 | * |
| 200 | * @return bool |
| 201 | */ |
| 202 | protected function supportsBackOffice() { |
| 203 | return TRUE; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Submit a manual payment. |
| 208 | * |
| 209 | * @param array $params |
| 210 | * Assoc array of input parameters for this transaction. |
| 211 | * |
| 212 | * @return array |
| 213 | */ |
| 214 | public function doDirectPayment(&$params) { |
| 215 | $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id'); |
| 216 | if ($params['is_pay_later']) { |
| 217 | $result['payment_status_id'] = array_search('Pending', $statuses); |
| 218 | } |
| 219 | else { |
| 220 | $result['payment_status_id'] = array_search('Completed', $statuses); |
| 221 | } |
| 222 | return $result; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Should a receipt be sent out for a pending payment. |
| 227 | * |
| 228 | * e.g for traditional pay later & ones with a delayed settlement a pending receipt makes sense. |
| 229 | */ |
| 230 | public function isSendReceiptForPending() { |
| 231 | return TRUE; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get help text information (help, description, etc.) about this payment, |
| 236 | * to display to the user. |
| 237 | * |
| 238 | * @param string $context |
| 239 | * Context of the text. |
| 240 | * Only explicitly supported contexts are handled without error. |
| 241 | * Currently supported: |
| 242 | * - contributionPageRecurringHelp (params: is_recur_installments, is_email_receipt) |
| 243 | * |
| 244 | * @param array $params |
| 245 | * Parameters for the field, context specific. |
| 246 | * |
| 247 | * @return string |
| 248 | */ |
| 249 | public function getText($context, $params) { |
| 250 | switch ($context) { |
| 251 | case 'contributionPageContinueText': |
| 252 | if ($params['amount'] <= 0) { |
| 253 | return ts('To complete this transaction, click the <strong>Continue</strong> button below.'); |
| 254 | } |
| 255 | return ts('To complete your contribution, click the <strong>Continue</strong> button below.'); |
| 256 | |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | } |