From 9a51ec56db529086e09f788d10854ab48ae26b6b Mon Sep 17 00:00:00 2001 From: Craig Paul Date: Wed, 19 Oct 2016 13:02:40 -0600 Subject: [PATCH] Adds stubbed out purchase functionality and needed classes --- src/Gateway.php | 75 +++++++++++++++++++++++++++++++++++++++++++++ src/Processor.php | 11 +++++++ src/Response.php | 8 +++++ src/Transaction.php | 8 +++++ 4 files changed, 102 insertions(+) create mode 100644 src/Processor.php create mode 100644 src/Response.php create mode 100644 src/Transaction.php diff --git a/src/Gateway.php b/src/Gateway.php index d5656e4..5683fa3 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -2,6 +2,9 @@ namespace CraigPaul\Moneris; +use CraigPaul\Moneris\Validators\Purchase; +use CraigPaul\Moneris\Validators\Validatable; + /** * CraigPaul\Moneris\Gateway * @@ -34,6 +37,13 @@ class Gateway */ protected $token; + /** + * The current transaction. + * + * @var \CraigPaul\Moneris\Transaction + */ + protected $transaction; + /** * Create a new Moneris instance. * @@ -49,4 +59,69 @@ public function __construct(string $id, string $token, string $environment) $this->token = $token; $this->environment = $environment; } + + /** + * Make a purchase. + * + * @param array $params + * + * @return \CraigPaul\Moneris\Response + */ + public function purchase(array $params = []) + { + $this->validate(Purchase::class, $params); + + array_merge($params, [ + 'type' => 'purchase', + 'crypt_type' => Crypt::SSL_ENABLED_MERCHANT, + ]); + + $transaction = $this->transaction($params); + + return $this->process($transaction); + } + + /** + * Process a transaction through the Moneris API. + * + * @param \CraigPaul\Moneris\Transaction $transaction + * + * @return \CraigPaul\Moneris\Response + */ + protected function process(Transaction $transaction) + { + return Processor::process($transaction); + } + + /** + * Get or create a new Transaction instance. + * + * @param array|null $params + * + * @return \CraigPaul\Moneris\Transaction + */ + protected function transaction(array $params = null) + { + if (is_null($this->transaction) || !is_null($params)) { + return $this->transaction = new Transaction($this, $params); + } + + return $this->transaction; + } + + /** + * Validate the supplied parameters against a provided class. + * + * @param string $class + * @param array $params + * + * @return void + */ + protected function validate(string $class, array $params = []) + { + /** @var Validatable $class */ + $class = new $class; + + $class->validate($params); + } } \ No newline at end of file diff --git a/src/Processor.php b/src/Processor.php new file mode 100644 index 0000000..04c6af5 --- /dev/null +++ b/src/Processor.php @@ -0,0 +1,11 @@ +