Merge pull request #19273 from eileenmcnaughton/complete
[civicrm-core.git] / CRM / Core / Payment / PayJunction.php
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 $params
45 * Assoc array of input parameters for this transaction.
46 *
47 * @return array
48 * the result in an nice formatted array (or an error object)
49 * @throws \Civi\Payment\Exception\PaymentProcessorException
50 */
51 public function doDirectPayment(&$params) {
52 $logon = $this->_paymentProcessor['user_name'];
53 $password = $this->_paymentProcessor['password'];
54 $url_site = $this->_paymentProcessor['url_site'];
55
56 // create pjpgCustInfo object
57 $pjpgCustInfo = new pjpgCustInfo();
58
59 $pjpgCustInfo->setEmail($params['email']);
60
61 $billing = [
62 'logon' => $logon,
63 'password' => $password,
64 'url_site' => $url_site,
65 'first_name' => $params['first_name'],
66 'last_name' => $params['last_name'],
67 'address' => $params['street_address'],
68 'city' => $params['city'],
69 'province' => $params['state_province'],
70 'postal_code' => $params['postal_code'],
71 'country' => $params['country'],
72 ];
73 $pjpgCustInfo->setBilling($billing);
74
75 // create pjpgTransaction object
76 $my_orderid = $params['invoiceID'];
77
78 $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']);
79
80 $txnArray = [
81 'type' => 'purchase',
82 'order_id' => $my_orderid,
83 'amount' => sprintf('%01.2f', $params['amount']),
84 'pan' => $params['credit_card_number'],
85 'expdate' => $expiry_string,
86 'crypt_type' => '7',
87 'cavv' => $params['cvv2'],
88 'cust_id' => $params['contact_id'],
89 ];
90
91 // Allow further manipulation of params via custom hooks
92 CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $txnArray);
93
94 $pjpgTxn = new pjpgTransaction($txnArray);
95
96 // set customer info (level 3 data) for the transaction
97 $pjpgTxn->setCustInfo($pjpgCustInfo);
98
99 // empty installments convert to 999 because PayJunction do not allow open end donation
100 if ($params['installments'] === '') {
101 $params['installments'] = '999';
102 }
103
104 // create recurring object
105 if ($params['is_recur'] == TRUE && $params['installments'] > 1) {
106 // schedule start date as today
107 // format: YYYY-MM-DD
108 $params['dc_schedule_start'] = date("Y-m-d");
109
110 // Recur Variables
111 $dc_schedule_create = $params['is_recur'];
112 $recurUnit = $params['frequency_unit'];
113 $recurInterval = $params['frequency_interval'];
114 $dc_schedule_start = $params['dc_schedule_start'];
115
116 // next payment in moneris required format
117 $startDate = date("Y/m/d", $next);
118
119 $numRecurs = $params['installments'];
120
121 $recurArray = [
122 'dc_schedule_create' => $dc_schedule_create,
123 // (day | week | month)
124 'recur_unit' => $recurUnit,
125 // yyyy/mm/dd
126 'start_date' => $startDate,
127 'num_recurs' => $numRecurs,
128 'start_now' => 'false',
129 'period' => $recurInterval,
130 'dc_schedule_start' => $dc_schedule_start,
131 'amount' => sprintf('%01.2f', $params['amount']),
132 ];
133
134 $pjpgRecur = new pjpgRecur($recurArray);
135
136 $pjpgTxn->setRecur($pjpgRecur);
137 }
138
139 // create a pjpgRequest object passing the transaction object
140 $pjpgRequest = new pjpgRequest($pjpgTxn);
141
142 $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest);
143
144 // get an pjpgResponse object
145 $pjpgResponse = $pjpgHttpPost->getPJpgResponse();
146
147 if (self::isError($pjpgResponse)) {
148 throw new PaymentProcessorException($pjpgResponse);
149 }
150
151 // Success
152 $params['trxn_result_code'] = $pjpgResponse['dc_response_code'];
153 $params['trxn_id'] = $pjpgResponse['dc_transaction_id'];
154
155 return $params;
156 }
157
158 // end function doDirectPayment
159
160 /**
161 * This function checks the PayJunction response code.
162 *
163 * @param array $response
164 *
165 * @return bool
166 */
167 public function isError(&$response) {
168 $responseCode = $response['dc_response_code'];
169
170 if ($responseCode === "00" || $responseCode === "85") {
171 return FALSE;
172 }
173 return TRUE;
174
175 }
176
177 /**
178 * Get the value of a field if set.
179 *
180 * @param string $field
181 * The field.
182 *
183 * @return mixed
184 * value of the field, or empty string if the field is
185 * not set
186 */
187 public function _getParam($field) {
188 if (isset($this->_params[$field])) {
189 return $this->_params[$field];
190 }
191 return '';
192 }
193
194 /**
195 * Set a field to the specified value. Value must be a scalar (int,
196 * float, string, or boolean)
197 *
198 * @param string $field
199 * @param mixed $value
200 *
201 * @return bool
202 * false if value is not a scalar, true if successful
203 */
204 public function _setParam($field, $value) {
205 if (!is_scalar($value)) {
206 return FALSE;
207 }
208 else {
209 $this->_params[$field] = $value;
210 }
211 }
212
213 /**
214 * This function checks to see if we have the right config values.
215 *
216 * @return string
217 * the error message if any
218 */
219 public function checkConfig() {
220 $error = [];
221 if (empty($this->_paymentProcessor['user_name'])) {
222 $error[] = ts('Username is not set for this payment processor');
223 }
224
225 if (empty($this->_paymentProcessor['password'])) {
226 $error[] = ts('Password is not set for this payment processor');
227 }
228
229 if (empty($this->_paymentProcessor['url_site'])) {
230 $error[] = ts('Site URL is not set for this payment processor');
231 }
232
233 if (!empty($error)) {
234 return implode('<p>', $error);
235 }
236 return NULL;
237 }
238
239 }
240 // end class CRM_Core_Payment_PayJunction