Skip to content

Commit

Permalink
Merge pull request #103 from onpayio/task/capture_adjusted_amount
Browse files Browse the repository at this point in the history
Task/capture adjusted amount
  • Loading branch information
ebbesmoeller authored Jan 11, 2024
2 parents dc34580 + 32536b6 commit 3675d1b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Take adjusted total amounts into account on available amounts for capture
- Added general platform that identifies the pluin with the API.
- Shifted declineURL order reference to wc_order key instead of onpay reference.

Expand Down
13 changes: 10 additions & 3 deletions woocommerce-onpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ public function orderStatusCompleteEvent($orderId) {
$availableAmount = $this->getAvailableAmount($order, $transaction);
// If transaction has status active, and charged amount is less than the full amount, we'll capture the remaining amount on transaction
if ($transaction->status === 'active' && $availableAmount > 0) {
$this->get_onpay_client()->transaction()->captureTransaction($transaction->uuid);
$this->get_onpay_client()->transaction()->captureTransaction($transaction->uuid, $availableAmount);
$order->add_order_note( __( 'Status changed to completed. Amount was automatically captured on transaction in OnPay.', 'wc-onpay' ));
}
} catch (OnPay\API\Exception\ConnectionException $exception) { // No connection to OnPay API
Expand Down Expand Up @@ -1238,10 +1238,17 @@ private function getAvailableAmount($order, $transaction) {
$orderCurrency = $currencyHelper->fromAlpha3($order->get_currency());
$orderRefunded = $order->get_total_refunded() * (10 ** $orderCurrency->exp);

// If order amount is lower than transaction amount, we'll roll with that instead
$orderTotal = $order->get_total() * (10 ** $orderCurrency->exp);
$total = $transaction->amount;
if($orderTotal < $total) {
$total = $orderTotal;
}

if ($this->get_option(WC_OnPay::SETTING_ONPAY_REFUND_INTEGRATION) === 'yes') {
$availableAmount = $transaction->amount - $transaction->charged - $orderRefunded;
$availableAmount = $total - $transaction->charged - $orderRefunded;
} else {
$availableAmount = $transaction->amount - $transaction->charged;
$availableAmount = $total - $transaction->charged;
}

if ($availableAmount < 0) {
Expand Down

0 comments on commit 3675d1b

Please sign in to comment.