Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
Adds receipt class as a wrapper for the Moneris response
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Paul committed Oct 25, 2016
1 parent 1c5ca81 commit 313e6f7
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions src/Receipt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace CraigPaul\Moneris;

class Receipt
{
/**
* @var array
*/
protected $data;

/**
* Create a new Receipt instance.
*
* @param $data
*/
public function __construct($data)
{
$this->data = $this->prepare($data);
}

/**
* Prepare the receipt data.
*
* @param $data
*
* @return array
*/
protected function prepare($data)
{
$array = [];
$params = [
['property' => 'amount', 'key' => 'TransAmount', 'cast' => 'float'],
['property' => 'authorization', 'key' => 'AuthCode', 'cast' => 'string'],
['property' => 'card', 'key' => 'CardType', 'cast' => 'string'],
['property' => 'code', 'key' => 'ResponseCode', 'cast' => 'string'],
['property' => 'complete', 'key' => 'Complete', 'cast' => 'boolean'],
['property' => 'data', 'key' => 'ResolveData', 'cast' => 'array', 'callback' => 'setData'],
['property' => 'date', 'key' => 'TransDate', 'cast' => 'string'],
['property' => 'id', 'key' => 'ReceiptId', 'cast' => 'string'],
['property' => 'iso', 'key' => 'ISO', 'cast' => 'string'],
['property' => 'key', 'key' => 'DataKey', 'cast' => 'string'],
['property' => 'message', 'key' => 'Message', 'cast' => 'string'],
['property' => 'reference', 'key' => 'ReferenceNum', 'cast' => 'string'],
['property' => 'time', 'key' => 'TransTime', 'cast' => 'string'],
['property' => 'transaction', 'key' => 'TransID', 'cast' => 'string'],
['property' => 'type', 'key' => 'TransType', 'cast' => 'string'],
];

foreach ($params as $param) {
$key = $param['key'];
$property = $param['property'];

$array[$property] = isset($data->$key) && !is_null($data->$key) ? $data->$key : null;

if (isset($param['cast'])) {
switch ($param['cast']) {
case 'boolean':
$array[$property] = isset($array[$property]) ? $array[$property]->__toString() : null;
$array[$property] = isset($array[$property]) && !is_null($array[$property]) ? ($array[$property] === 'true' ? true : false) : false;

break;
case 'float':
$array[$property] = isset($array[$property]) ? floatval($array[$property]->__toString()) : null;

break;
case 'string':
$array[$property] = isset($array[$property]) ? $array[$property]->__toString() : null;

break;
case 'array':
$array[$property] = (array)$array[$property];
}
}

if (isset($param['callback'])) {
$callback = $param['callback'];

$array[$property] = $this->$callback($array[$property]);
}

}

return $array;
}

/**
* Read an item from the receipt.
*
* @param string $value
*
* @return mixed|null
*/
public function read(string $value)
{
if (isset($this->data[$value]) && !is_null($this->data[$value])) {
return $this->data[$value];
}

return null;
}

/**
* Format the resolved data from the Moneris API.
*
* @param array $data
*
* @return array
*/
private function setData(array $data)
{
return [
'customer_id' => isset($data['cust_id']) ? (is_string($data['cust_id']) ? $data['cust_id'] : $data['cust_id']->__toString()) : '',
'phone' => isset($data['phone']) ? $data['phone']->__toString() : '',
'email' => isset($data['email']) ? $data['email']->__toString() : '',
'note' => isset($data['note']) ? $data['note']->__toString() : '',
'crypt' => isset($data['crypt_type']) ? intval($data['crypt_type']) : null,
'masked_pan' => $data['masked_pan'] ?? null,
'pan' => $data['pan'] ?? null,
'expiry_date' => [
'month' => isset($data['expdate']) ? substr($data['expdate'], -2, 2) : null,
'year' => isset($data['expdate']) ? substr($data['expdate'], 0, 2) : null,
],
];
}
}

0 comments on commit 313e6f7

Please sign in to comment.