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>
16 require_once 'PayJunction/pjClasses.php';
19 * Class CRM_Core_Payment_PayJunction.
21 class CRM_Core_Payment_PayJunction
extends CRM_Core_Payment
{
22 // (not used, implicit in the API, might need to convert?)
23 const CHARSET
= 'UFT-8';
29 * The mode of operation: live or test.
31 * @param $paymentProcessor
33 * @return \CRM_Core_Payment_PayJunction
35 public function __construct($mode, &$paymentProcessor) {
37 $this->_paymentProcessor
= $paymentProcessor;
38 $this->_processorName
= ts('PayJunction');
42 * This function sends request and receives response from
43 * PayJunction payment process
45 * @param array $params
46 * Assoc array of input parameters for this transaction.
49 * the result in an nice formatted array (or an error object)
51 public function doDirectPayment(&$params) {
52 $logon = $this->_paymentProcessor
['user_name'];
53 $password = $this->_paymentProcessor
['password'];
54 $url_site = $this->_paymentProcessor
['url_site'];
56 // create pjpgCustInfo object
57 $pjpgCustInfo = new pjpgCustInfo();
59 $pjpgCustInfo->setEmail($params['email']);
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'],
73 $pjpgCustInfo->setBilling($billing);
75 // create pjpgTransaction object
76 $my_orderid = $params['invoiceID'];
78 $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']);
82 'order_id' => $my_orderid,
83 'amount' => sprintf('%01.2f', $params['amount']),
84 'pan' => $params['credit_card_number'],
85 'expdate' => $expiry_string,
87 'cavv' => $params['cvv2'],
88 'cust_id' => $params['contact_id'],
91 // Allow further manipulation of params via custom hooks
92 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $txnArray);
94 $pjpgTxn = new pjpgTransaction($txnArray);
96 // set customer info (level 3 data) for the transaction
97 $pjpgTxn->setCustInfo($pjpgCustInfo);
99 // empty installments convert to 999 because PayJunction do not allow open end donation
100 if ($params['installments'] == "") {
101 $params['installments'] = "999";
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");
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'];
116 // next payment in moneris required format
117 $startDate = date("Y/m/d", $next);
119 $numRecurs = $params['installments'];
122 'dc_schedule_create' => $dc_schedule_create,
123 // (day | week | month)
124 'recur_unit' => $recurUnit,
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']),
134 $pjpgRecur = new pjpgRecur($recurArray);
136 $pjpgTxn->setRecur($pjpgRecur);
139 // create a pjpgRequest object passing the transaction object
140 $pjpgRequest = new pjpgRequest($pjpgTxn);
142 $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest);
144 // get an pjpgResponse object
145 $pjpgResponse = $pjpgHttpPost->getPJpgResponse();
147 if (self
::isError($pjpgResponse)) {
148 return self
::error($pjpgResponse);
151 /* Check for application errors */
153 $result = self
::checkResult($pjpgResponse);
154 if (is_a($result, 'CRM_Core_Error')) {
159 $params['trxn_result_code'] = $pjpgResponse['dc_response_code'];
160 $params['trxn_id'] = $pjpgResponse['dc_transaction_id'];
161 $params['gross_amount'] = $params['amount'];
166 // end function doDirectPayment
169 * This function checks the PayJunction response code.
171 * @param array $response
175 public function isError(&$response) {
176 $responseCode = $response['dc_response_code'];
178 if ($responseCode == "00" ||
$responseCode == "85") {
187 * ignore for now, more elaborate error handling later.
192 public function &checkResult(&$response) {
197 * Get the value of a field if set.
199 * @param string $field
203 * value of the field, or empty string if the field is
206 public function _getParam($field) {
207 if (isset($this->_params
[$field])) {
208 return $this->_params
[$field];
220 public function &error($error = NULL) {
221 $e = CRM_Core_Error
::singleton();
223 $e->push($error['dc_response_code'],
225 $error['dc_response_message']
229 $e->push(9001, 0, NULL, "Unknown System Error.");
235 * Set a field to the specified value. Value must be a scalar (int,
236 * float, string, or boolean)
238 * @param string $field
239 * @param mixed $value
242 * false if value is not a scalar, true if successful
244 public function _setParam($field, $value) {
245 if (!is_scalar($value)) {
249 $this->_params
[$field] = $value;
254 * This function checks to see if we have the right config values.
257 * the error message if any
259 public function checkConfig() {
261 if (empty($this->_paymentProcessor
['user_name'])) {
262 $error[] = ts('Username is not set for this payment processor');
265 if (empty($this->_paymentProcessor
['password'])) {
266 $error[] = ts('Password is not set for this payment processor');
269 if (empty($this->_paymentProcessor
['url_site'])) {
270 $error[] = ts('Site URL is not set for this payment processor');
273 if (!empty($error)) {
274 return implode('<p>', $error);
282 // end class CRM_Core_Payment_PayJunction