Merge pull request #8328 from mlutfy/master-crm18524
[civicrm-core.git] / CRM / Core / Payment / PayJunction.php
CommitLineData
6a488035
TO
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/**
12 *
13 * @package CRM
14 * @author Michael Morris and Gene Chi @ Phase2 Technology <mmorris@phase2technology.com>
15 * $Id$
16 *
17 */
9f9eb906 18require_once 'PayJunction/pjClasses.php';
19
6a488035
TO
20class CRM_Core_Payment_PayJunction extends CRM_Core_Payment {
21 # (not used, implicit in the API, might need to convert?)
7da04cde 22 const CHARSET = 'UFT-8';
6a488035
TO
23
24 /**
25 * We only need one instance of this object. So we use the singleton
26 * pattern and cache the instance in this variable
27 *
28 * @var object
6a488035
TO
29 */
30 static private $_singleton = NULL;
31
32 /**
fe482240 33 * Constructor.
6a488035 34 *
6a0b768e
TO
35 * @param string $mode
36 * The mode of operation: live or test.
6a488035 37 *
77b97be7
EM
38 * @param $paymentProcessor
39 *
40 * @return \CRM_Core_Payment_PayJunction
6a488035 41 */
00be9182 42 public function __construct($mode, &$paymentProcessor) {
6a488035
TO
43 $this->_mode = $mode;
44 $this->_paymentProcessor = $paymentProcessor;
45 $this->_processorName = ts('PayJunction');
46 }
47
d424ffde 48 /**
6a488035
TO
49 * This function sends request and receives response from
50 * PayJunction payment process
b5c2afd0 51 *
6a0b768e
TO
52 * @param array $params
53 * Assoc array of input parameters for this transaction.
b5c2afd0 54 *
a6c01b45
CW
55 * @return array
56 * the result in an nice formatted array (or an error object)
b5c2afd0 57 */
00be9182 58 public function doDirectPayment(&$params) {
353ffa53 59 $logon = $this->_paymentProcessor['user_name'];
6a488035
TO
60 $password = $this->_paymentProcessor['password'];
61 $url_site = $this->_paymentProcessor['url_site'];
62
63 // create pjpgCustInfo object
64 $pjpgCustInfo = new pjpgCustInfo();
65
66 $pjpgCustInfo->setEmail($params['email']);
67
68 $billing = array(
69 "logon" => $logon,
70 "password" => $password,
71 "url_site" => $url_site,
72 "first_name" => $params['first_name'],
73 "last_name" => $params['last_name'],
74 "address" => $params['street_address'],
75 "city" => $params['city'],
76 "province" => $params['state_province'],
77 "postal_code" => $params['postal_code'],
78 "country" => $params['country'],
79 );
80 $pjpgCustInfo->setBilling($billing);
81
82 // create pjpgTransaction object
83 $my_orderid = $params['invoiceID'];
84
85 $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']);
86
87 $txnArray = array(
88 'type' => 'purchase',
89 'order_id' => $my_orderid,
90 'amount' => sprintf('%01.2f', $params['amount']),
91 'pan' => $params['credit_card_number'],
92 'expdate' => $expiry_string,
93 'crypt_type' => '7',
a175672f 94 'cavv' => $params['cvv2'],
6a488035
TO
95 'cust_id' => $params['contact_id'],
96 );
97
98 // Allow further manipulation of params via custom hooks
99 CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $txnArray);
100
101 $pjpgTxn = new pjpgTransaction($txnArray);
102
103 // set customer info (level 3 data) for the transaction
104 $pjpgTxn->setCustInfo($pjpgCustInfo);
105
6a488035
TO
106 // empty installments convert to 999 because PayJunction do not allow open end donation
107 if ($params['installments'] == "") {
108 $params['installments'] = "999";
109 }
110
111 // create recurring object
112 if ($params['is_recur'] == TRUE && $params['installments'] > 1) {
113 // schedule start date as today
114 // format: YYYY-MM-DD
115 $params['dc_schedule_start'] = date("Y-m-d");
116
117 // Recur Variables
118 $dc_schedule_create = $params['is_recur'];
353ffa53
TO
119 $recurUnit = $params['frequency_unit'];
120 $recurInterval = $params['frequency_interval'];
121 $dc_schedule_start = $params['dc_schedule_start'];
6a488035
TO
122
123 // next payment in moneris required format
124 $startDate = date("Y/m/d", $next);
125
126 $numRecurs = $params['installments'];
127
128 $recurArray = array(
129 'dc_schedule_create' => $dc_schedule_create,
130 // (day | week | month)
131 'recur_unit' => $recurUnit,
132 // yyyy/mm/dd
133 'start_date' => $startDate,
134 'num_recurs' => $numRecurs,
135 'start_now' => 'false',
136 'period' => $recurInterval,
137 'dc_schedule_start' => $dc_schedule_start,
138 'amount' => sprintf('%01.2f', $params['amount']),
139 );
140
141 $pjpgRecur = new pjpgRecur($recurArray);
142
143 $pjpgTxn->setRecur($pjpgRecur);
144 }
145
146 // create a pjpgRequest object passing the transaction object
147 $pjpgRequest = new pjpgRequest($pjpgTxn);
148
149 $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest);
150
151 // get an pjpgResponse object
152 $pjpgResponse = $pjpgHttpPost->getPJpgResponse();
153
154 if (self::isError($pjpgResponse)) {
155 return self::error($pjpgResponse);
156 }
157
158 /* Check for application errors */
159
160 $result = self::checkResult($pjpgResponse);
161 if (is_a($result, 'CRM_Core_Error')) {
162 return $result;
163 }
164
165 // Success
166 $params['trxn_result_code'] = $pjpgResponse['dc_response_code'];
167 $params['trxn_id'] = $pjpgResponse['dc_transaction_id'];
168 $params['gross_amount'] = $params['amount'];
169
170 return $params;
171 }
172 // end function doDirectPayment
173
6c786a9b 174 /**
fe482240 175 * This function checks the PayJunction response code.
6c786a9b 176 *
54957108 177 * @param array $response
178 *
6c786a9b
EM
179 * @return bool
180 */
00be9182 181 public function isError(&$response) {
6a488035
TO
182 $responseCode = $response['dc_response_code'];
183
184 if ($responseCode == "00" || $responseCode == "85") {
185 return FALSE;
186 }
187 else {
188 return TRUE;
189 }
190 }
191
192
6c786a9b 193 /**
4f1f1f2a 194 * ignore for now, more elaborate error handling later.
6c786a9b
EM
195 * @param $response
196 *
197 * @return mixed
198 */
00be9182 199 public function &checkResult(&$response) {
6a488035
TO
200 return $response;
201 }
202
203 /**
fe482240 204 * Get the value of a field if set.
6a488035 205 *
6a0b768e
TO
206 * @param string $field
207 * The field.
6a488035 208 *
72b3a70c
CW
209 * @return mixed
210 * value of the field, or empty string if the field is
16b10e64 211 * not set
6a488035 212 */
00be9182 213 public function _getParam($field) {
6a488035
TO
214 if (isset($this->_params[$field])) {
215 return $this->_params[$field];
216 }
217 else {
218 return '';
219 }
220 }
221
6c786a9b
EM
222 /**
223 * @param null $error
224 *
225 * @return object
226 */
00be9182 227 public function &error($error = NULL) {
6a488035
TO
228 $e = CRM_Core_Error::singleton();
229 if ($error) {
230 $e->push($error['dc_response_code'],
231 0, NULL,
232 $error['dc_response_message']
233 );
234 }
235 else {
236 $e->push(9001, 0, NULL, "Unknown System Error.");
237 }
238 return $e;
239 }
240
241 /**
242 * Set a field to the specified value. Value must be a scalar (int,
243 * float, string, or boolean)
244 *
245 * @param string $field
246 * @param mixed $value
247 *
a6c01b45
CW
248 * @return bool
249 * false if value is not a scalar, true if successful
6a488035 250 */
00be9182 251 public function _setParam($field, $value) {
6a488035
TO
252 if (!is_scalar($value)) {
253 return FALSE;
254 }
255 else {
256 $this->_params[$field] = $value;
257 }
258 }
259
260 /**
fe482240 261 * This function checks to see if we have the right config values.
6a488035 262 *
a6c01b45
CW
263 * @return string
264 * the error message if any
6a488035 265 */
00be9182 266 public function checkConfig() {
6a488035
TO
267 $error = array();
268 if (empty($this->_paymentProcessor['user_name'])) {
269 $error[] = ts('Username is not set for this payment processor');
270 }
271
272 if (empty($this->_paymentProcessor['password'])) {
273 $error[] = ts('Password is not set for this payment processor');
274 }
275
276 if (empty($this->_paymentProcessor['url_site'])) {
277 $error[] = ts('Site URL is not set for this payment processor');
278 }
279
280 if (!empty($error)) {
281 return implode('<p>', $error);
282 }
283 else {
284 return NULL;
285 }
286 }
96025800 287
6a488035
TO
288}
289// end class CRM_Core_Payment_PayJunction