diff --git a/src/Transaction.php b/src/Transaction.php index 1f9cc73..4f0aa40 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -12,6 +12,13 @@ class Transaction { use Gettable; + /** + * The errors for the transaction. + * + * @var array + */ + protected $errors; + /** * The Gateway instance. * @@ -39,6 +46,16 @@ public function __construct(Gateway $gateway, array $params = [], $prepare = tru $this->params = $prepare ? $this->prepare($params) : $params; } + /** + * Check that the required parameters have not been provided to the transaction. + * + * @return bool + */ + public function invalid() + { + return !$this->valid(); + } + /** * Prepare the transaction parameters. * @@ -64,4 +81,38 @@ protected function prepare(array $params) return $params; } + + /** + * Check that the required parameters have been provided to the transaction. + * + * @return bool + */ + public function valid() + { + $params = $this->params; + $errors = []; + + $errors[] = Validator::empty($params) ? 'No parameters provided.' : null; + + if (isset($params['type'])) { + switch ($params['type']) { + case 'purchase': + $errors[] = Validator::set($params, 'order_id') ? null : 'Order Id not provided.'; + $errors[] = Validator::set($params, 'pan') ? null : 'Credit card number not provided.'; + $errors[] = Validator::set($params, 'amount') ? null : 'Amount not provided.'; + $errors[] = Validator::set($params, 'expdate') ? null : 'Expiry date not provided.'; + + break; + default: + $errors[] = $params['type'].' is not a supported transaction type.'; + } + } else { + $errors[] = 'Transaction type not provided.'; + } + + $errors = array_filter($errors); + $this->errors = $errors; + + return empty($errors); + } } \ No newline at end of file diff --git a/src/Validator.php b/src/Validator.php new file mode 100644 index 0000000..ff9bcd1 --- /dev/null +++ b/src/Validator.php @@ -0,0 +1,27 @@ +