diff --git a/src/Receipt.php b/src/Receipt.php new file mode 100644 index 0000000..c649a86 --- /dev/null +++ b/src/Receipt.php @@ -0,0 +1,126 @@ +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, + ], + ]; + } +} \ No newline at end of file