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;
41 * This function sends request and receives response from
42 * PayJunction payment process
44 * @param array $params
45 * Assoc array of input parameters for this transaction.
48 * the result in an nice formatted array (or an error object)
50 public function doDirectPayment(&$params) {
51 $logon = $this->_paymentProcessor
['user_name'];
52 $password = $this->_paymentProcessor
['password'];
53 $url_site = $this->_paymentProcessor
['url_site'];
55 // create pjpgCustInfo object
56 $pjpgCustInfo = new pjpgCustInfo();
58 $pjpgCustInfo->setEmail($params['email']);
62 "password" => $password,
63 "url_site" => $url_site,
64 "first_name" => $params['first_name'],
65 "last_name" => $params['last_name'],
66 "address" => $params['street_address'],
67 "city" => $params['city'],
68 "province" => $params['state_province'],
69 "postal_code" => $params['postal_code'],
70 "country" => $params['country'],
72 $pjpgCustInfo->setBilling($billing);
74 // create pjpgTransaction object
75 $my_orderid = $params['invoiceID'];
77 $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']);
81 'order_id' => $my_orderid,
82 'amount' => sprintf('%01.2f', $params['amount']),
83 'pan' => $params['credit_card_number'],
84 'expdate' => $expiry_string,
86 'cavv' => $params['cvv2'],
87 'cust_id' => $params['contact_id'],
90 // Allow further manipulation of params via custom hooks
91 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $txnArray);
93 $pjpgTxn = new pjpgTransaction($txnArray);
95 // set customer info (level 3 data) for the transaction
96 $pjpgTxn->setCustInfo($pjpgCustInfo);
98 // empty installments convert to 999 because PayJunction do not allow open end donation
99 if ($params['installments'] == "") {
100 $params['installments'] = "999";
103 // create recurring object
104 if ($params['is_recur'] == TRUE && $params['installments'] > 1) {
105 // schedule start date as today
106 // format: YYYY-MM-DD
107 $params['dc_schedule_start'] = date("Y-m-d");
110 $dc_schedule_create = $params['is_recur'];
111 $recurUnit = $params['frequency_unit'];
112 $recurInterval = $params['frequency_interval'];
113 $dc_schedule_start = $params['dc_schedule_start'];
115 // next payment in moneris required format
116 $startDate = date("Y/m/d", $next);
118 $numRecurs = $params['installments'];
121 'dc_schedule_create' => $dc_schedule_create,
122 // (day | week | month)
123 'recur_unit' => $recurUnit,
125 'start_date' => $startDate,
126 'num_recurs' => $numRecurs,
127 'start_now' => 'false',
128 'period' => $recurInterval,
129 'dc_schedule_start' => $dc_schedule_start,
130 'amount' => sprintf('%01.2f', $params['amount']),
133 $pjpgRecur = new pjpgRecur($recurArray);
135 $pjpgTxn->setRecur($pjpgRecur);
138 // create a pjpgRequest object passing the transaction object
139 $pjpgRequest = new pjpgRequest($pjpgTxn);
141 $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest);
143 // get an pjpgResponse object
144 $pjpgResponse = $pjpgHttpPost->getPJpgResponse();
146 if (self
::isError($pjpgResponse)) {
147 return self
::error($pjpgResponse);
150 /* Check for application errors */
152 $result = self
::checkResult($pjpgResponse);
153 if (is_a($result, 'CRM_Core_Error')) {
158 $params['trxn_result_code'] = $pjpgResponse['dc_response_code'];
159 $params['trxn_id'] = $pjpgResponse['dc_transaction_id'];
160 $params['gross_amount'] = $params['amount'];
165 // end function doDirectPayment
168 * This function checks the PayJunction response code.
170 * @param array $response
174 public function isError(&$response) {
175 $responseCode = $response['dc_response_code'];
177 if ($responseCode == "00" ||
$responseCode == "85") {
186 * ignore for now, more elaborate error handling later.
191 public function &checkResult(&$response) {
196 * Get the value of a field if set.
198 * @param string $field
202 * value of the field, or empty string if the field is
205 public function _getParam($field) {
206 if (isset($this->_params
[$field])) {
207 return $this->_params
[$field];
219 public function &error($error = NULL) {
220 $e = CRM_Core_Error
::singleton();
222 $e->push($error['dc_response_code'],
224 $error['dc_response_message']
228 $e->push(9001, 0, NULL, "Unknown System Error.");
234 * Set a field to the specified value. Value must be a scalar (int,
235 * float, string, or boolean)
237 * @param string $field
238 * @param mixed $value
241 * false if value is not a scalar, true if successful
243 public function _setParam($field, $value) {
244 if (!is_scalar($value)) {
248 $this->_params
[$field] = $value;
253 * This function checks to see if we have the right config values.
256 * the error message if any
258 public function checkConfig() {
260 if (empty($this->_paymentProcessor
['user_name'])) {
261 $error[] = ts('Username is not set for this payment processor');
264 if (empty($this->_paymentProcessor
['password'])) {
265 $error[] = ts('Password is not set for this payment processor');
268 if (empty($this->_paymentProcessor
['url_site'])) {
269 $error[] = ts('Site URL is not set for this payment processor');
272 if (!empty($error)) {
273 return implode('<p>', $error);
281 // end class CRM_Core_Payment_PayJunction