From bef740c8eb15f2ac57f20f62d06c31c5bf4c7d6b Mon Sep 17 00:00:00 2001 From: Andy Pieters Date: Wed, 1 Aug 2018 17:49:41 +0200 Subject: [PATCH] Added transaction status api Transaction Get result will call the transaction status api automaticly when fields from this api are accessed --- src/Api/Transaction/Status.php | 48 +++++++++++ src/Result/Transaction/Status.php | 113 +++++++++++++++++++++++++ src/Result/Transaction/Transaction.php | 46 ++++++++-- src/Transaction.php | 15 ++++ 4 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 src/Api/Transaction/Status.php create mode 100644 src/Result/Transaction/Status.php diff --git a/src/Api/Transaction/Status.php b/src/Api/Transaction/Status.php new file mode 100644 index 00000000..940fb962 --- /dev/null +++ b/src/Api/Transaction/Status.php @@ -0,0 +1,48 @@ +transactionId = $transactionId; + } + + /** + * @inheritdoc + */ + public function doRequest($endpoint = null, $version = null) + { + return parent::doRequest('transaction/status'); + } + + /** + * @inheritdoc + * @throws Error\Required TransactionId is required + */ + protected function getData() + { + if (empty($this->transactionId)) { + throw new Error\Required('TransactionId required'); + } + + $this->data['transactionId'] = $this->transactionId; + + return parent::getData(); + } +} \ No newline at end of file diff --git a/src/Result/Transaction/Status.php b/src/Result/Transaction/Status.php new file mode 100644 index 00000000..9a683d69 --- /dev/null +++ b/src/Result/Transaction/Status.php @@ -0,0 +1,113 @@ +data['paymentDetails']['transactionId']; + } + + /** + * @return string + */ + public function getOrderId() + { + return $this->data['paymentDetails']['orderId']; + } + + /** + * @return int + */ + public function getPaymentProfileId() + { + return $this->data['paymentDetails']['paymentProfileId']; + } + + /** + * @return int the status id + */ + public function getState() + { + return $this->data['paymentDetails']['state']; + } + + /** + * @return string The name of the status + */ + public function getStateName() + { + return $this->data['paymentDetails']['stateName']; + } + + /** + * @return string + */ + public function getCurrency() + { + return $this->data['paymentDetails']['currency']; + } + + /** + * @return float|int The amount in euro + */ + public function getAmount() + { + return $this->data['paymentDetails']['amount'] / 100; + } + + /** + * @return float|int The amount in the used currency + */ + public function getCurrencyAmount() + { + return $this->data['paymentDetails']['currenyAmount'] / 100; + } + + /** + * @return float|int The paid amount + */ + public function getPaidAmount() + { + return $this->data['paymentDetails']['paidAmount'] / 100; + } + + /** + * @return float|int The paid amount in the used currency + */ + public function getPaidCurrencyAmount() + { + return $this->data['paymentDetails']['paidCurrenyAmount'] / 100; + } + + /** + * @return float|int The amount that has been refunded + */ + public function getRefundedAmount() + { + return $this->data['paymentDetails']['refundAmount'] / 100; + } + + /** + * @return float|int The amount that has been refunded in the used currency + */ + public function getRefundedCurrencyAmount() + { + return $this->data['paymentDetails']['refundCurrenyAmount'] / 100; + } + + +} \ No newline at end of file diff --git a/src/Result/Transaction/Transaction.php b/src/Result/Transaction/Transaction.php index 9d417ee3..20f1473e 100644 --- a/src/Result/Transaction/Transaction.php +++ b/src/Result/Transaction/Transaction.php @@ -28,6 +28,8 @@ */ class Transaction extends Result { + private $_cachedStatusResult = null; + /** * @return bool Transaction is paid */ @@ -64,7 +66,7 @@ public function isCanceled() public function void() { - if ( ! $this->isAuthorized()) { + if (!$this->isAuthorized()) { throw new Error('Cannod void transaction, status is not authorized'); } @@ -86,7 +88,7 @@ public function getId() public function capture() { - if ( ! $this->isAuthorized()) { + if (!$this->isAuthorized()) { throw new Error('Cannod capture transaction, status is not authorized'); } @@ -223,9 +225,29 @@ public function getExtra3() return $this->data['statsDetails']['extra3']; } + /** + * @return float|int The refunded amount in euro + * @throws Error + * @throws \Paynl\Error\Api + */ + public function getRefundedAmount() + { + return $this->getStatus()->getRefundedAmount(); + } + + /** + * @return float|int The refunded amount in the used currency + * @throws Error + * @throws \Paynl\Error\Api + */ + public function getRefundedCurrencyAmount() + { + return $this->getStatus()->getRefundedCurrencyAmount(); + } + public function approve() { - if ( ! $this->isBeingVerified()) { + if (!$this->isBeingVerified()) { throw new Error("Cannot approve transaction because it does not have the status 'verify'"); } @@ -235,6 +257,19 @@ public function approve() return $result; } + /** + * @return Status + * @throws Error + * @throws \Paynl\Error\Api + */ + public function getStatus() + { + if (is_null($this->_cachedStatusResult)) { + $this->_cachedStatusResult = \Paynl\Transaction::status($this->getId()); + } + return $this->_cachedStatusResult; + } + /** * @return bool */ @@ -245,13 +280,14 @@ public function isBeingVerified() private function _reload() { - $result = \Paynl\Transaction::get($this->getId()); + $this->_cachedStatusResult = null; + $result = \Paynl\Transaction::get($this->getId()); $this->data = $result->getData(); } public function decline() { - if ( ! $this->isBeingVerified()) { + if (!$this->isBeingVerified()) { throw new Error("Cannot decline transaction because it does not have the status 'verify'"); } diff --git a/src/Transaction.php b/src/Transaction.php index 2690b81e..044a1f88 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -252,6 +252,21 @@ public static function get($transactionId) return new Result\Transaction($result); } + /** + * @param $transactionId + * @return Result\Status + * @throws Error\Api + * @throws Error\Error + */ + public static function status($transactionId) + { + $api = new Api\Status(); + $api->setTransactionId($transactionId); + $result = $api->doRequest(); + + return new Result\Status($result); + } + /** * Get the transaction in an exchange script. * This will work for all kinds of exchange calls (GET, POST AND POST_XML)