Skip to content

Commit

Permalink
Merge pull request #99 from SwedbankPay/hotfix/checkoutv3-update
Browse files Browse the repository at this point in the history
Upd
  • Loading branch information
aait committed Sep 30, 2023
2 parents d9ab56e + d63918d commit 4a595fc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 22 deletions.
46 changes: 36 additions & 10 deletions src/SwedbankPay/Core/Adapter/WC_Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace SwedbankPay\Core\Adapter;

use SwedbankPay\Checkout\WooCommerce\Swedbank_Pay_Admin;
use Automattic\WooCommerce\Utilities\OrderUtil;
use Automattic\WooCommerce\Admin\Overrides\OrderRefund;
use SwedbankPay\Core\Exception;
use SwedbankPay\Core\Log\LogLevel;
use SwedbankPay\Core\ConfigurationInterface;
Expand All @@ -23,6 +25,7 @@
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
* @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.MissingImport)
*/
// phpcs:ignore
class WC_Adapter implements PaymentAdapterInterface
Expand Down Expand Up @@ -189,8 +192,8 @@ public function getOrderData($orderId)
$order = wc_get_order($orderId);

// Load parent order if have `OrderRefund`
if ('order_refund' === $order->get_type()) {
$order = wc_get_order($order->get_parent_id());
if ('order_refund' === $order->get_type() || $order instanceof OrderRefund) {
throw new \LogicException('Unable to use OrderRefund objects.');
}

// Force a new DB read (and update cache) for meta data.
Expand Down Expand Up @@ -301,7 +304,11 @@ public function getOrderData($orderId)
'woocommerce'
),
OrderItemInterface::FIELD_TYPE => OrderItemInterface::TYPE_SHIPPING,
OrderItemInterface::FIELD_CLASS => apply_filters('swedbank_pay_product_class_shipping', 'ProductGroup1', $order),
OrderItemInterface::FIELD_CLASS => apply_filters(
'swedbank_pay_product_class_shipping',
'ProductGroup1',
$order
),
OrderItemInterface::FIELD_QTY => 1,
OrderItemInterface::FIELD_QTY_UNIT => 'pcs',
OrderItemInterface::FIELD_UNITPRICE => round($shippingWithTax * 100),
Expand All @@ -323,7 +330,11 @@ public function getOrderData($orderId)
OrderItemInterface::FIELD_REFERENCE => 'fee',
OrderItemInterface::FIELD_NAME => $orderFee->get_name(),
OrderItemInterface::FIELD_TYPE => OrderItemInterface::TYPE_OTHER,
OrderItemInterface::FIELD_CLASS => apply_filters('swedbank_pay_product_class_fee', 'ProductGroup1', $order),
OrderItemInterface::FIELD_CLASS => apply_filters(
'swedbank_pay_product_class_fee',
'ProductGroup1',
$order
),
OrderItemInterface::FIELD_QTY => 1,
OrderItemInterface::FIELD_QTY_UNIT => 'pcs',
OrderItemInterface::FIELD_UNITPRICE => round($feeWithTax * 100),
Expand All @@ -344,7 +355,11 @@ public function getOrderData($orderId)
OrderItemInterface::FIELD_REFERENCE => 'discount',
OrderItemInterface::FIELD_NAME => __('Discount', 'swedbank-pay-woocommerce-payments'),
OrderItemInterface::FIELD_TYPE => OrderItemInterface::TYPE_DISCOUNT,
OrderItemInterface::FIELD_CLASS => apply_filters('swedbank_pay_product_class_discount', 'ProductGroup1', $order),
OrderItemInterface::FIELD_CLASS => apply_filters(
'swedbank_pay_product_class_discount',
'ProductGroup1',
$order
),
OrderItemInterface::FIELD_QTY => 1,
OrderItemInterface::FIELD_QTY_UNIT => 'pcs',
OrderItemInterface::FIELD_UNITPRICE => round(-100 * $discountWithTax),
Expand Down Expand Up @@ -565,6 +580,12 @@ public function getOrderStatus($orderId)
*/
public function updateOrderStatus($orderId, $status, $message = null, $transactionNumber = null)
{
remove_action(
'woocommerce_order_status_changed',
[Swedbank_Pay_Admin::class, 'order_status_changed_transaction'],
0
);

$order = wc_get_order($orderId);

if ($transactionNumber) {
Expand All @@ -581,7 +602,15 @@ public function updateOrderStatus($orderId, $status, $message = null, $transacti
$order->save();
}

$this->log('info', sprintf('Update order status #%s to %s', $orderId, $status));
$this->log(
'info',
sprintf(
'Update order status #%s to %s. Transaction ID: %s',
$orderId,
$status,
$transactionNumber
)
);

switch ($status) {
case OrderInterface::STATUS_PENDING:
Expand All @@ -603,10 +632,7 @@ public function updateOrderStatus($orderId, $status, $message = null, $transacti
// Set on-hold
if (!$order->has_status('on-hold')) {
// Reduce stock
$orderStockReduced = $order->get_meta('_order_stock_reduced');
if (!$orderStockReduced) {
wc_reduce_stock_levels($order->get_id());
}
wc_maybe_reduce_stock_levels($order->get_id());

$order->update_status('on-hold', $message);
} elseif ($message) {
Expand Down
16 changes: 16 additions & 0 deletions src/SwedbankPay/Core/Api/FinancialTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ public function getReceiptReference()
return $this->getData(self::RECEIPT_REFERENCE);
}

/**
* @return bool
*/
public function isAuthorization()
{
return self::TYPE_AUTHORIZATION === $this->getType();
}

/**
* @return bool
*/
public function isVerification()
{
return self::TYPE_VERIFICATION === $this->getType();
}

/**
* @return bool
*/
Expand Down
58 changes: 46 additions & 12 deletions src/SwedbankPay/Core/Traits/OrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function processFinancialTransaction($orderId, $transaction)
{
$this->log(
LogLevel::DEBUG,
sprintf('Process transaction: %s', json_encode($transaction, JSON_PRETTY_PRINT))
sprintf('Process transaction: %s', var_export($transaction, true))
);

/** @var Order $order */
Expand Down Expand Up @@ -210,6 +210,21 @@ public function processFinancialTransaction($orderId, $transaction)
case FinancialTransactionInterface::TYPE_SALE:
// Check if the payment was captured fully
// `remainingCaptureAmount` is missing if the payment was captured fully
if (!isset($paymentBody['remainingCaptureAmount'])) {
$this->log(
LogLevel::DEBUG,
sprintf(
'Warning: Payment Order ID: %s. Transaction %s. Transaction amount: %s. Order amount: %s. Field remainingCaptureAmount is missing. Full action?', //phpcs:ignore
$order->getPaymentOrderId(),
$transaction->getNumber(),
$transaction->getAmount() / 100,
$order->getAmount()
)
);
}

$remainingAmount = isset($paymentBody['remainingCaptureAmount'])
? $paymentBody['remainingCaptureAmount'] / 100 : 0;
$isFullCapture = false;
if (!isset($paymentBody['remainingCaptureAmount'])) {
$isFullCapture = true;
Expand All @@ -231,9 +246,10 @@ public function processFinancialTransaction($orderId, $transaction)
$this->addOrderNote(
$orderId,
sprintf(
'Payment has been partially captured: Transaction: %s. Amount: %s',
'Payment has been partially captured: Transaction: %s. Amount: %s. Remaining amount: %s', //phpcs:ignore
$transaction->getNumber(),
$transaction->getAmount() / 100
$transaction->getAmount() / 100,
$remainingAmount
)
);
}
Expand All @@ -249,16 +265,33 @@ public function processFinancialTransaction($orderId, $transaction)

break;
case FinancialTransactionInterface::TYPE_REVERSAL:
// Create Credit Memo
$this->createCreditMemo(
$orderId,
$transaction->getAmount() / 100,
$transaction->getNumber(),
$transaction->getDescription()
);
// Create Credit Memo if not exists
if (!$this->isCreditMemoExist($transaction->getNumber())) {
$this->createCreditMemo(
$orderId,
$transaction->getAmount() / 100,
$transaction->getNumber(),
$transaction->getDescription()
);
}

// Check if the payment was refunded fully
// `remainingReversalAmount` is missing if the payment was refunded fully
if (!isset($paymentBody['remainingReversalAmount'])) {
$this->log(
LogLevel::DEBUG,
sprintf(
'Warning: Payment Order ID: %s. Transaction %s. Transaction amount: %s. Order amount: %s. Field remainingReversalAmount is missing. Full action?', //phpcs:ignore
$order->getPaymentOrderId(),
$transaction->getNumber(),
$transaction->getAmount() / 100,
$order->getAmount()
)
);
}

$remainingAmount = isset($paymentBody['remainingReversalAmount'])
? $paymentBody['remainingReversalAmount'] / 100 : 0;
$isFullRefund = false;
if (!isset($paymentBody['remainingReversalAmount'])) {
$isFullRefund = true;
Expand All @@ -280,9 +313,10 @@ public function processFinancialTransaction($orderId, $transaction)
$this->addOrderNote(
$orderId,
sprintf(
'Payment has been partially refunded: Transaction: %s. Amount: %s',
'Payment has been partially refunded: Transaction: %s. Amount: %s. Remaining amount: %s', //phpcs:ignore
$transaction->getNumber(),
$transaction->getAmount() / 100
$transaction->getAmount() / 100,
$remainingAmount
)
);
}
Expand Down

0 comments on commit 4a595fc

Please sign in to comment.