5 * Licensed to CiviCRM under the Academic Free License version 3.0.
7 * Written and contributed by Phase2 Technology, LLC (http://www.phase2technology.com)
14 * @author Michael Morris and Gene Chi @ Phase2 Technology <mmorris@phase2technology.com>
18 class CRM_Core_Payment_PayJunction
extends CRM_Core_Payment
{
19 # (not used, implicit in the API, might need to convert?)
20 const CHARSET
= 'UFT-8';
23 * We only need one instance of this object. So we use the singleton
24 * pattern and cache the instance in this variable
29 static private $_singleton = NULL;
35 * The mode of operation: live or test.
37 * @param $paymentProcessor
39 * @return \CRM_Core_Payment_PayJunction
41 public function __construct($mode, &$paymentProcessor) {
42 //require PayJunction API library
43 require_once 'PayJunction/pjClasses.php';
46 $this->_paymentProcessor
= $paymentProcessor;
47 $this->_processorName
= ts('PayJunction');
51 * This function sends request and receives response from
52 * PayJunction payment process
55 * This function collects all the information from a web/api form and invokes
56 * the relevant payment processor specific functions to perform the transaction
58 * @param array $params
59 * Assoc array of input parameters for this transaction.
61 * @return array the result in an nice formatted array (or an error object)
64 public function doDirectPayment(&$params) {
65 $logon = $this->_paymentProcessor
['user_name'];
66 $password = $this->_paymentProcessor
['password'];
67 $url_site = $this->_paymentProcessor
['url_site'];
69 // create pjpgCustInfo object
70 $pjpgCustInfo = new pjpgCustInfo();
72 $pjpgCustInfo->setEmail($params['email']);
76 "password" => $password,
77 "url_site" => $url_site,
78 "first_name" => $params['first_name'],
79 "last_name" => $params['last_name'],
80 "address" => $params['street_address'],
81 "city" => $params['city'],
82 "province" => $params['state_province'],
83 "postal_code" => $params['postal_code'],
84 "country" => $params['country'],
86 $pjpgCustInfo->setBilling($billing);
88 // create pjpgTransaction object
89 $my_orderid = $params['invoiceID'];
91 $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']);
95 'order_id' => $my_orderid,
96 'amount' => sprintf('%01.2f', $params['amount']),
97 'pan' => $params['credit_card_number'],
98 'expdate' => $expiry_string,
100 'cust_id' => $params['contact_id'],
103 // Allow further manipulation of params via custom hooks
104 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $txnArray);
106 $pjpgTxn = new pjpgTransaction($txnArray);
108 // set customer info (level 3 data) for the transaction
109 $pjpgTxn->setCustInfo($pjpgCustInfo);
111 // empty installments convert to 999 because PayJunction do not allow open end donation
112 if ($params['installments'] == "") {
113 $params['installments'] = "999";
116 // create recurring object
117 if ($params['is_recur'] == TRUE && $params['installments'] > 1) {
118 // schedule start date as today
119 // format: YYYY-MM-DD
120 $params['dc_schedule_start'] = date("Y-m-d");
123 $dc_schedule_create = $params['is_recur'];
124 $recurUnit = $params['frequency_unit'];
125 $recurInterval = $params['frequency_interval'];
126 $dc_schedule_start = $params['dc_schedule_start'];
128 // next payment in moneris required format
129 $startDate = date("Y/m/d", $next);
131 $numRecurs = $params['installments'];
134 'dc_schedule_create' => $dc_schedule_create,
135 // (day | week | month)
136 'recur_unit' => $recurUnit,
138 'start_date' => $startDate,
139 'num_recurs' => $numRecurs,
140 'start_now' => 'false',
141 'period' => $recurInterval,
142 'dc_schedule_start' => $dc_schedule_start,
143 'amount' => sprintf('%01.2f', $params['amount']),
146 $pjpgRecur = new pjpgRecur($recurArray);
148 $pjpgTxn->setRecur($pjpgRecur);
151 // create a pjpgRequest object passing the transaction object
152 $pjpgRequest = new pjpgRequest($pjpgTxn);
154 $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest);
156 // get an pjpgResponse object
157 $pjpgResponse = $pjpgHttpPost->getPJpgResponse();
159 if (self
::isError($pjpgResponse)) {
160 return self
::error($pjpgResponse);
163 /* Check for application errors */
165 $result = self
::checkResult($pjpgResponse);
166 if (is_a($result, 'CRM_Core_Error')) {
171 $params['trxn_result_code'] = $pjpgResponse['dc_response_code'];
172 $params['trxn_id'] = $pjpgResponse['dc_transaction_id'];
173 $params['gross_amount'] = $params['amount'];
177 // end function doDirectPayment
181 * This function checks the PayJunction response code
188 public function isError(&$response) {
189 $responseCode = $response['dc_response_code'];
191 if ($responseCode == "00" ||
$responseCode == "85") {
200 // ignore for now, more elaborate error handling later.
206 public function &checkResult(&$response) {
211 * Get the value of a field if set
213 * @param string $field
216 * @return mixed value of the field, or empty string if the field is
219 public function _getParam($field) {
220 if (isset($this->_params
[$field])) {
221 return $this->_params
[$field];
233 public function &error($error = NULL) {
234 $e = CRM_Core_Error
::singleton();
236 $e->push($error['dc_response_code'],
238 $error['dc_response_message']
242 $e->push(9001, 0, NULL, "Unknown System Error.");
248 * Set a field to the specified value. Value must be a scalar (int,
249 * float, string, or boolean)
251 * @param string $field
252 * @param mixed $value
254 * @return bool false if value is not a scalar, true if successful
256 public function _setParam($field, $value) {
257 if (!is_scalar($value)) {
261 $this->_params
[$field] = $value;
266 * This function checks to see if we have the right config values
268 * @return string the error message if any
270 public function checkConfig() {
272 if (empty($this->_paymentProcessor
['user_name'])) {
273 $error[] = ts('Username is not set for this payment processor');
276 if (empty($this->_paymentProcessor
['password'])) {
277 $error[] = ts('Password is not set for this payment processor');
280 if (empty($this->_paymentProcessor
['url_site'])) {
281 $error[] = ts('Site URL is not set for this payment processor');
284 if (!empty($error)) {
285 return implode('<p>', $error);
292 // end class CRM_Core_Payment_PayJunction