Skip to content

Commit

Permalink
implement card refunds using CanCreditOrderAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
iesus committed Feb 12, 2018
1 parent ae9707e commit f55afef
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions app/code/community/Svea/Checkout/Model/Payment/Api/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class Svea_Checkout_Model_Payment_Api_Invoice
const SVEA_CAN_ADD_ORDER_ROW = 'CanAddOrderRow';
const SVEA_ROW_IS_IS_UPDATEABLE = 'CanUpdateOrderRow';
const SVEA_CURRENT_ROW_IS_IS_UPDATEABLE = 'CanUpdateRow';
const SVEA_CAN_CREDIT_ORDER_AMOUNT = 'CanCreditAmount';
const SVEA_CAN_CREDIT_ORDER_ROWS = 'CanCreditRow';
const SVEA_CAN_CANCEL_ORDER_AMOUNT = 'CanCancelAmount';
const SVEA_ORDER_DELIVERED = 'Delivered';

/**
* Create invoice.
Expand All @@ -42,8 +45,8 @@ public function processInvoice($payment, $invoice)
$shippingMethod = '';
$canPartiallyProcess = in_array($this::SVEA_IS_PARTIALLY_INVOICEABLE, $sveaOrder['Actions']);

if (!in_array($this::SVEA_IS_INVOICEABLE, $sveaOrder['Actions'])) {
if ($sveaOrder['orderStatus'] == 'Delivered') {
if (!in_array($this::SVEA_IS_INVOICEABLE, $sveaOrder['Actions']) && isset($sveaOrder['OrderStatus'])) {
if ($sveaOrder['OrderStatus'] == $this::SVEA_ORDER_DELIVERED) {
return new Varien_Object($sveaOrder);
}

Expand Down Expand Up @@ -275,6 +278,58 @@ public function refund($creditMemo)
}
}

if (
is_array($sveaOrder['Actions']) &&
in_array(self::SVEA_CAN_CANCEL_ORDER_AMOUNT, $sveaOrder['Actions'])
) {

Mage::throwException(
'Amount not yet settled (usually takes up to 24 hours), Use void to fully cancel the order.'
);
}

if (isset($deliveryKey)) {
$delivery = $sveaOrder['Deliveries'][$deliveryKey];
if (
is_array($delivery['Actions']) &&
in_array($this::SVEA_CAN_CREDIT_ORDER_AMOUNT, $delivery['Actions'])
) {

$refundAmount = $creditMemo->getGrandTotal();

WebPayAdmin::creditAmount($sveaConfig)
->setCheckoutOrderId($sveaOrderId)
->setDeliveryId($deliveryKey)
->setAmountIncVat((float)$refundAmount)
->creditCheckoutAmount()
->doRequest();
$sveaOrder = $this->_getCheckoutOrder($order);

return new Varien_Object($sveaOrder);
}
} else {
foreach ($sveaOrder['Deliveries'] as $deliveries) {
if (
is_array($deliveries['Actions']) &&
in_array($this::SVEA_CAN_CREDIT_ORDER_AMOUNT, $deliveries['Actions'])
) {
$refundAmount = $creditMemo->getGrandTotal();
$deliveryKey = $deliveries['Id'];

WebPayAdmin::creditAmount($sveaConfig)
->setCheckoutOrderId($sveaOrderId)
->setDeliveryId($deliveryKey)
->setAmountIncVat((float)$refundAmount)
->creditCheckoutAmount()
->doRequest();

}
}
$sveaOrder = $this->_getCheckoutOrder($order);

return new Varien_Object($sveaOrder);
}

if (isset($deliveryKey)) {
$tmpRefundItems[$sveaOrder['Deliveries'][$deliveryKey]['Id']] = $this->_getActionRows(
$creditMemoItems,
Expand Down

0 comments on commit f55afef

Please sign in to comment.