| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Copyright (C) 2007 |
| 5 | * Licensed to CiviCRM under the Academic Free License version 3.0. |
| 6 | * |
| 7 | * Written and contributed by Phase2 Technology, LLC (http://www.phase2technology.com) |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | use Civi\Payment\Exception\PaymentProcessorException; |
| 12 | |
| 13 | /** |
| 14 | * |
| 15 | * @package CRM |
| 16 | * @author Michael Morris and Gene Chi @ Phase2 Technology <mmorris@phase2technology.com> |
| 17 | */ |
| 18 | require_once 'PayJunction/pjClasses.php'; |
| 19 | |
| 20 | /** |
| 21 | * Class CRM_Core_Payment_PayJunction. |
| 22 | */ |
| 23 | class CRM_Core_Payment_PayJunction extends CRM_Core_Payment { |
| 24 | // (not used, implicit in the API, might need to convert?) |
| 25 | const CHARSET = 'UFT-8'; |
| 26 | |
| 27 | /** |
| 28 | * Constructor. |
| 29 | * |
| 30 | * @param string $mode |
| 31 | * The mode of operation: live or test. |
| 32 | * |
| 33 | * @param array $paymentProcessor |
| 34 | */ |
| 35 | public function __construct($mode, &$paymentProcessor) { |
| 36 | $this->_mode = $mode; |
| 37 | $this->_paymentProcessor = $paymentProcessor; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * This function sends request and receives response from |
| 42 | * PayJunction payment process |
| 43 | * |
| 44 | * @param array|PropertyBag $params |
| 45 | * |
| 46 | * @param string $component |
| 47 | * |
| 48 | * @return array |
| 49 | * Result array (containing at least the key payment_status_id) |
| 50 | * |
| 51 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
| 52 | */ |
| 53 | public function doPayment(&$params, $component = 'contribute') { |
| 54 | $propertyBag = \Civi\Payment\PropertyBag::cast($params); |
| 55 | $this->_component = $component; |
| 56 | $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate'); |
| 57 | |
| 58 | // If we have a $0 amount, skip call to processor and set payment_status to Completed. |
| 59 | // Conceivably a processor might override this - perhaps for setting up a token - but we don't |
| 60 | // have an example of that at the moment. |
| 61 | if ($propertyBag->getAmount() == 0) { |
| 62 | $result['payment_status_id'] = array_search('Completed', $statuses); |
| 63 | $result['payment_status'] = 'Completed'; |
| 64 | return $result; |
| 65 | } |
| 66 | |
| 67 | $logon = $this->_paymentProcessor['user_name']; |
| 68 | $password = $this->_paymentProcessor['password']; |
| 69 | $url_site = $this->_paymentProcessor['url_site']; |
| 70 | |
| 71 | // create pjpgCustInfo object |
| 72 | $pjpgCustInfo = new pjpgCustInfo(); |
| 73 | |
| 74 | $pjpgCustInfo->setEmail($params['email']); |
| 75 | |
| 76 | $billing = [ |
| 77 | 'logon' => $logon, |
| 78 | 'password' => $password, |
| 79 | 'url_site' => $url_site, |
| 80 | 'first_name' => $params['first_name'], |
| 81 | 'last_name' => $params['last_name'], |
| 82 | 'address' => $params['street_address'], |
| 83 | 'city' => $params['city'], |
| 84 | 'province' => $params['state_province'], |
| 85 | 'postal_code' => $params['postal_code'], |
| 86 | 'country' => $params['country'], |
| 87 | ]; |
| 88 | $pjpgCustInfo->setBilling($billing); |
| 89 | |
| 90 | // create pjpgTransaction object |
| 91 | $my_orderid = $params['invoiceID']; |
| 92 | |
| 93 | $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']); |
| 94 | |
| 95 | $txnArray = [ |
| 96 | 'type' => 'purchase', |
| 97 | 'order_id' => $my_orderid, |
| 98 | 'amount' => sprintf('%01.2f', $params['amount']), |
| 99 | 'pan' => $params['credit_card_number'], |
| 100 | 'expdate' => $expiry_string, |
| 101 | 'crypt_type' => '7', |
| 102 | 'cavv' => $params['cvv2'], |
| 103 | 'cust_id' => $params['contact_id'], |
| 104 | ]; |
| 105 | |
| 106 | // Allow further manipulation of params via custom hooks |
| 107 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $txnArray); |
| 108 | |
| 109 | $pjpgTxn = new pjpgTransaction($txnArray); |
| 110 | |
| 111 | // set customer info (level 3 data) for the transaction |
| 112 | $pjpgTxn->setCustInfo($pjpgCustInfo); |
| 113 | |
| 114 | // empty installments convert to 999 because PayJunction do not allow open end donation |
| 115 | if ($params['installments'] === '') { |
| 116 | $params['installments'] = '999'; |
| 117 | } |
| 118 | |
| 119 | // create recurring object |
| 120 | if ($params['is_recur'] == TRUE && $params['installments'] > 1) { |
| 121 | // schedule start date as today |
| 122 | // format: YYYY-MM-DD |
| 123 | $params['dc_schedule_start'] = date("Y-m-d"); |
| 124 | |
| 125 | // Recur Variables |
| 126 | $dc_schedule_create = $params['is_recur']; |
| 127 | $recurUnit = $params['frequency_unit']; |
| 128 | $recurInterval = $params['frequency_interval']; |
| 129 | $dc_schedule_start = $params['dc_schedule_start']; |
| 130 | |
| 131 | // next payment in moneris required format |
| 132 | $startDate = date("Y/m/d", $next); |
| 133 | |
| 134 | $numRecurs = $params['installments']; |
| 135 | |
| 136 | $recurArray = [ |
| 137 | 'dc_schedule_create' => $dc_schedule_create, |
| 138 | // (day | week | month) |
| 139 | 'recur_unit' => $recurUnit, |
| 140 | // yyyy/mm/dd |
| 141 | 'start_date' => $startDate, |
| 142 | 'num_recurs' => $numRecurs, |
| 143 | 'start_now' => 'false', |
| 144 | 'period' => $recurInterval, |
| 145 | 'dc_schedule_start' => $dc_schedule_start, |
| 146 | 'amount' => sprintf('%01.2f', $params['amount']), |
| 147 | ]; |
| 148 | |
| 149 | $pjpgRecur = new pjpgRecur($recurArray); |
| 150 | |
| 151 | $pjpgTxn->setRecur($pjpgRecur); |
| 152 | } |
| 153 | |
| 154 | // create a pjpgRequest object passing the transaction object |
| 155 | $pjpgRequest = new pjpgRequest($pjpgTxn); |
| 156 | |
| 157 | $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest); |
| 158 | |
| 159 | // get an pjpgResponse object |
| 160 | $pjpgResponse = $pjpgHttpPost->getPJpgResponse(); |
| 161 | |
| 162 | if (self::isError($pjpgResponse)) { |
| 163 | throw new PaymentProcessorException($pjpgResponse); |
| 164 | } |
| 165 | |
| 166 | // Success |
| 167 | $params['trxn_result_code'] = $pjpgResponse['dc_response_code']; |
| 168 | $params['trxn_id'] = $pjpgResponse['dc_transaction_id']; |
| 169 | $params['payment_status_id'] = array_search('Completed', $statuses); |
| 170 | $params['payment_status'] = 'Completed'; |
| 171 | |
| 172 | return $params; |
| 173 | } |
| 174 | |
| 175 | // end function doDirectPayment |
| 176 | |
| 177 | /** |
| 178 | * This function checks the PayJunction response code. |
| 179 | * |
| 180 | * @param array $response |
| 181 | * |
| 182 | * @return bool |
| 183 | */ |
| 184 | public function isError(&$response) { |
| 185 | $responseCode = $response['dc_response_code']; |
| 186 | |
| 187 | if ($responseCode === "00" || $responseCode === "85") { |
| 188 | return FALSE; |
| 189 | } |
| 190 | return TRUE; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * This function checks to see if we have the right config values. |
| 195 | * |
| 196 | * @return string |
| 197 | * the error message if any |
| 198 | */ |
| 199 | public function checkConfig() { |
| 200 | $error = []; |
| 201 | if (empty($this->_paymentProcessor['user_name'])) { |
| 202 | $error[] = ts('Username is not set for this payment processor'); |
| 203 | } |
| 204 | |
| 205 | if (empty($this->_paymentProcessor['password'])) { |
| 206 | $error[] = ts('Password is not set for this payment processor'); |
| 207 | } |
| 208 | |
| 209 | if (empty($this->_paymentProcessor['url_site'])) { |
| 210 | $error[] = ts('Site URL is not set for this payment processor'); |
| 211 | } |
| 212 | |
| 213 | if (!empty($error)) { |
| 214 | return implode('<p>', $error); |
| 215 | } |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | // end class CRM_Core_Payment_PayJunction |