This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds receipt class as a wrapper for the Moneris response
- Loading branch information
Craig Paul
committed
Oct 25, 2016
1 parent
1c5ca81
commit 313e6f7
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]; | ||
} | ||
} |