Skip to content

Commit

Permalink
Support Fee object for magento (#175)
Browse files Browse the repository at this point in the history
* Support Fee object for magento

* update version
  • Loading branch information
andykim authored Jan 11, 2024
1 parent 729bfe2 commit 143c26f
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 12.0.1 (2024-01-11)
- Support Xendit invoice fees

## 12.0.0 (2023-11-21)
- Update xendit url

Expand Down
6 changes: 6 additions & 0 deletions Controller/Checkout/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ private function getApiRequestData(Order $order)
'items' => $items
];

// Extract order fees and send it to Xendit invoice
$orderFees = $this->getDataHelper()->extractOrderFees($order);
if (!empty($orderFees)) {
$payload['fees'] = $orderFees;
}

if (!empty($customerObject)) {
$payload['customer'] = $customerObject;
}
Expand Down
9 changes: 9 additions & 0 deletions Controller/Checkout/InvoiceMultishipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function execute()
$currency = '';
$billingEmail = '';
$customerObject = [];
$feesObject = [];

$orderIncrementIds = [];
$preferredMethod = '';
Expand Down Expand Up @@ -95,6 +96,9 @@ public function execute()
if (empty($customerObject)) {
$customerObject = $this->getDataHelper()->extractXenditInvoiceCustomerFromOrder($order);
}

// Extract order fees and send it to Xendit invoice
$feesObject[] = $this->getDataHelper()->extractOrderFees($order);
}

if ($orderProcessed) {
Expand All @@ -115,6 +119,11 @@ public function execute()
'failure_redirect_url' => $this->getDataHelper()->getFailureUrl($orderIncrementIds),
'items' => $items
];

if (!empty($feesObject)) {
$requestData['fees'] = $this->getDataHelper()->mergeFeesObject($feesObject);
}

if (!empty($customerObject)) {
$requestData['customer'] = $customerObject;
}
Expand Down
101 changes: 100 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
class Data extends AbstractHelper
{
const XENDIT_M2INVOICE_VERSION = '12.0.0';
const XENDIT_M2INVOICE_VERSION = '12.0.1';

/**
* @var StoreManagerInterface
Expand Down Expand Up @@ -663,4 +663,103 @@ public function extractProductCategoryName(Product $product): string
}
return !empty($categoryNames) ? implode(', ', $categoryNames) : 'n/a';
}

/**
* Extract order fees
* @param Order $order
* @return array
*/
public function extractOrderFees(Order $order): array
{
$fees = [
[
'type' => __('Discount'),
'value' => (float) $order->getDiscountAmount()
],
[
'type' => __('Shipping fee'),
'value' => (float) $order->getShippingAmount()
],
[
'type' => __('Tax fee'),
'value' => $order->getTaxAmount()
]
];

// Make sure it will cover the other fees
$otherFees = $this->getOtherFees($order);
if ($otherFees > 0) {
$fees[] = [
'type' => __('Other Fees'),
'value' => $this->getOtherFees($order)
];
}

return array_values(
array_filter($fees, function ($value) {
return $value['value'] != 0;
}, ARRAY_FILTER_USE_BOTH)
);
}

/**
* Get other fees amount
* In case order has the other fees not Magento standard
*
* @param Order $order
* @return float
*/
public function getOtherFees(Order $order): float
{
return $order->getTotalDue() - (float)array_sum(
[
$order->getSubtotal(), // items total
$order->getTaxAmount(),
$order->getShippingAmount(),
$order->getDiscountAmount()
]
);
}

/**
* Merge Fees object
*
* @param array $feesObject
* @return array
*/
public function mergeFeesObject(array $feesObject = []): array
{
if (empty($feesObject)) {
return [];
}

$mergedFeesObject = [];
foreach ($feesObject as $feeObject) {
foreach ($feeObject as $fee) {
/** @var \Magento\Framework\Phrase $type */
$type = $fee['type'];
$typeLabel = $type->getText();
$value = $fee['value'];

if (isset($mergedFeesObject[$typeLabel])) {
$mergedFeesObject[$typeLabel] = (float)$mergedFeesObject[$typeLabel] + $value;
} else {
$mergedFeesObject[$typeLabel] = $value;
}
}
}

if (empty($mergedFeesObject)) {
return [];
}

$response = [];
foreach ($mergedFeesObject as $typeLabel => $value) {
$response[] = [
'type' => $typeLabel,
'value' => $value
];
}
return $response;
}
}
Loading

0 comments on commit 143c26f

Please sign in to comment.