diff --git a/Gateway/Http/TransactionBuilder/Refund.php b/Gateway/Http/TransactionBuilder/Refund.php index 32120f2b2..e10fd9b6f 100644 --- a/Gateway/Http/TransactionBuilder/Refund.php +++ b/Gateway/Http/TransactionBuilder/Refund.php @@ -21,24 +21,22 @@ namespace Buckaroo\Magento2\Gateway\Http\TransactionBuilder; -use Magento\Framework\App\Config\ScopeConfigInterface; -use Magento\Framework\Data\Form\FormKey; -use Magento\Framework\UrlInterface; -use Magento\Framework\Encryption\Encryptor; -use Buckaroo\Magento2\Gateway\Http\Transaction; -use Buckaroo\Magento2\Model\ConfigProvider\Account; -use Buckaroo\Magento2\Model\ConfigProvider\Method\Factory; -use Buckaroo\Magento2\Service\Software\Data as SoftwareData; +use Buckaroo\Magento2\Exception; +use Buckaroo\Magento2\Model\Method\AbstractMethod; +use Magento\Framework\Exception\LocalizedException; class Refund extends AbstractTransactionBuilder { + /** - * @throws \Buckaroo\Magento2\Exception + * @return void + * @throws Exception + * @throws LocalizedException */ protected function setRefundCurrencyAndAmount() { /** - * @var \Buckaroo\Magento2\Model\Method\AbstractMethod $methodInstance + * @var AbstractMethod $methodInstance */ $methodInstance = $this->order->getPayment()->getMethodInstance(); $method = $methodInstance->buckarooPaymentMethodCode; @@ -65,6 +63,8 @@ protected function setRefundCurrencyAndAmount() /** * @return array + * @throws Exception + * @throws LocalizedException */ public function getBody() { @@ -103,6 +103,12 @@ public function getBody() ], ]; + if ($this->order->getTotalRefunded() >= $this->order->getGrandTotal()) { + $this->order->setState(\Magento\Sales\Model\Order::STATE_CLOSED) + ->setStatus('closed'); + $this->order->save(); + } + return $body; } diff --git a/Gateway/Http/TransactionBuilder/RefundPartial.php b/Gateway/Http/TransactionBuilder/RefundPartial.php index 95491a4ae..50d7e33c8 100644 --- a/Gateway/Http/TransactionBuilder/RefundPartial.php +++ b/Gateway/Http/TransactionBuilder/RefundPartial.php @@ -21,31 +21,30 @@ namespace Buckaroo\Magento2\Gateway\Http\TransactionBuilder; +use Exception; use Magento\Store\Model\Store; use Magento\Framework\App\RequestInterface; use Buckaroo\Magento2\Model\GroupTransaction; use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress; - - class RefundPartial extends AbstractTransactionBuilder { /** - * @var \Magento\Framework\App\RequestInterface + * @var RequestInterface */ protected $httpRequest; /** - * @var \Magento\Store\Model\Store + * @var Store */ protected $store; /** * - * @var \Buckaroo\Magento2\Model\GroupTransaction + * @var GroupTransaction */ protected $groupTransaction; @@ -87,18 +86,19 @@ public function setStore(Store $store) $this->store = $store; return $this; } + /** * @return array + * @throws Exception */ public function getBody() { if (!$this->store instanceof Store) { - throw new \Exception("`store` must be instance of Magento\Store\Model\Store"); + throw new Exception("`store` must be instance of Magento\Store\Model\Store"); } - if (!$this->groupTransaction instanceof GroupTransaction) { - throw new \Exception("`groupTransaction` must be instance of Buckaroo\Magento2\Model\GroupTransaction"); + throw new Exception("`groupTransaction` must be instance of Buckaroo\Magento2\Model\GroupTransaction"); } $ip = $this->getUserIp($this->store); @@ -132,13 +132,20 @@ public function getBody() ], ]; + if ($this->groupTransaction->getRefundedAmount() >= $this->groupTransaction->getOrder()->getGrandTotal()) { + $this->groupTransaction->getOrder()->setState(\Magento\Sales\Model\Order::STATE_CLOSED) + ->setStatus('closed'); + $this->groupTransaction->getOrder()->save(); + } + return $body; } /** * Get merchant key for store * - * @return mixed + * @return string + * @throws Exception */ public function getMerchantKey() { @@ -146,6 +153,7 @@ public function getMerchantKey() $this->configProviderAccount->getMerchantKey($this->store) ); } + /** * @return array */ @@ -165,13 +173,12 @@ private function getAdditionalParameters() */ private function getParameterLine($name, $value) { - $line = [ + return [ '_' => $value, 'Name' => $name, ]; - - return $line; } + /** * {@inheritdoc} */ @@ -186,19 +193,20 @@ public function getReturnUrl() return $this->returnUrl; } + /** * Get user ip * * @param Store $store * * @return string - * @throws \Exception + * @throws Exception */ protected function getUserIp($store) { - + if (!$this->httpRequest instanceof RequestInterface) { - throw new \Exception("Required parameter `httpRequest` must be instance of Magento\Framework\App\RequestInterface"); + throw new Exception("Required parameter `httpRequest` must be instance of Magento\Framework\App\RequestInterface"); } $ipHeaders = $this->configProviderAccount->getIpHeader($store);