Skip to content

Commit

Permalink
Added transaction status api
Browse files Browse the repository at this point in the history
Transaction Get result will call the transaction status api automaticly when fields from this api are accessed
  • Loading branch information
Andy Pieters committed Aug 1, 2018
1 parent 84c4f61 commit bef740c
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 5 deletions.
48 changes: 48 additions & 0 deletions src/Api/Transaction/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Paynl\Api\Transaction;

use Paynl\Error;

class Status extends Transaction
{
protected $apiTokenRequired = true;

/**
* @var string
*/
private $transactionId;

/**
* Set the transactionId
*
* @param string $transactionId
*/
public function setTransactionId($transactionId)
{
$this->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();
}
}
113 changes: 113 additions & 0 deletions src/Result/Transaction/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Created by PhpStorm.
* User: andy
* Date: 1-8-18
* Time: 15:56
*/

namespace Paynl\Result\Transaction;


use Paynl\Result\Result;

class Status extends Result
{
/**
* @return string The EX-code of the transaction
*/
public function getTransactionId()
{
return $this->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;
}


}
46 changes: 41 additions & 5 deletions src/Result/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
class Transaction extends Result
{
private $_cachedStatusResult = null;

/**
* @return bool Transaction is paid
*/
Expand Down Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand Down Expand Up @@ -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'");
}

Expand All @@ -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
*/
Expand All @@ -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'");
}

Expand Down
15 changes: 15 additions & 0 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bef740c

Please sign in to comment.