Skip to content

Commit 143c26f

Browse files
authored
Support Fee object for magento (#175)
* Support Fee object for magento * update version
1 parent 729bfe2 commit 143c26f

File tree

7 files changed

+415
-6
lines changed

7 files changed

+415
-6
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## 12.0.1 (2024-01-11)
4+
- Support Xendit invoice fees
5+
36
## 12.0.0 (2023-11-21)
47
- Update xendit url
58

Controller/Checkout/Invoice.php

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ private function getApiRequestData(Order $order)
120120
'items' => $items
121121
];
122122

123+
// Extract order fees and send it to Xendit invoice
124+
$orderFees = $this->getDataHelper()->extractOrderFees($order);
125+
if (!empty($orderFees)) {
126+
$payload['fees'] = $orderFees;
127+
}
128+
123129
if (!empty($customerObject)) {
124130
$payload['customer'] = $customerObject;
125131
}

Controller/Checkout/InvoiceMultishipping.php

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function execute()
2828
$currency = '';
2929
$billingEmail = '';
3030
$customerObject = [];
31+
$feesObject = [];
3132

3233
$orderIncrementIds = [];
3334
$preferredMethod = '';
@@ -95,6 +96,9 @@ public function execute()
9596
if (empty($customerObject)) {
9697
$customerObject = $this->getDataHelper()->extractXenditInvoiceCustomerFromOrder($order);
9798
}
99+
100+
// Extract order fees and send it to Xendit invoice
101+
$feesObject[] = $this->getDataHelper()->extractOrderFees($order);
98102
}
99103

100104
if ($orderProcessed) {
@@ -115,6 +119,11 @@ public function execute()
115119
'failure_redirect_url' => $this->getDataHelper()->getFailureUrl($orderIncrementIds),
116120
'items' => $items
117121
];
122+
123+
if (!empty($feesObject)) {
124+
$requestData['fees'] = $this->getDataHelper()->mergeFeesObject($feesObject);
125+
}
126+
118127
if (!empty($customerObject)) {
119128
$requestData['customer'] = $customerObject;
120129
}

Helper/Data.php

+100-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
class Data extends AbstractHelper
3232
{
33-
const XENDIT_M2INVOICE_VERSION = '12.0.0';
33+
const XENDIT_M2INVOICE_VERSION = '12.0.1';
3434

3535
/**
3636
* @var StoreManagerInterface
@@ -663,4 +663,103 @@ public function extractProductCategoryName(Product $product): string
663663
}
664664
return !empty($categoryNames) ? implode(', ', $categoryNames) : 'n/a';
665665
}
666+
667+
/**
668+
* Extract order fees
669+
* @param Order $order
670+
* @return array
671+
*/
672+
public function extractOrderFees(Order $order): array
673+
{
674+
$fees = [
675+
[
676+
'type' => __('Discount'),
677+
'value' => (float) $order->getDiscountAmount()
678+
],
679+
[
680+
'type' => __('Shipping fee'),
681+
'value' => (float) $order->getShippingAmount()
682+
],
683+
[
684+
'type' => __('Tax fee'),
685+
'value' => $order->getTaxAmount()
686+
]
687+
];
688+
689+
// Make sure it will cover the other fees
690+
$otherFees = $this->getOtherFees($order);
691+
if ($otherFees > 0) {
692+
$fees[] = [
693+
'type' => __('Other Fees'),
694+
'value' => $this->getOtherFees($order)
695+
];
696+
}
697+
698+
return array_values(
699+
array_filter($fees, function ($value) {
700+
return $value['value'] != 0;
701+
}, ARRAY_FILTER_USE_BOTH)
702+
);
703+
}
704+
705+
/**
706+
* Get other fees amount
707+
* In case order has the other fees not Magento standard
708+
*
709+
* @param Order $order
710+
* @return float
711+
*/
712+
public function getOtherFees(Order $order): float
713+
{
714+
return $order->getTotalDue() - (float)array_sum(
715+
[
716+
$order->getSubtotal(), // items total
717+
$order->getTaxAmount(),
718+
$order->getShippingAmount(),
719+
$order->getDiscountAmount()
720+
]
721+
);
722+
}
723+
724+
/**
725+
* Merge Fees object
726+
*
727+
* @param array $feesObject
728+
* @return array
729+
*/
730+
public function mergeFeesObject(array $feesObject = []): array
731+
{
732+
if (empty($feesObject)) {
733+
return [];
734+
}
735+
736+
$mergedFeesObject = [];
737+
foreach ($feesObject as $feeObject) {
738+
foreach ($feeObject as $fee) {
739+
/** @var \Magento\Framework\Phrase $type */
740+
$type = $fee['type'];
741+
$typeLabel = $type->getText();
742+
$value = $fee['value'];
743+
744+
if (isset($mergedFeesObject[$typeLabel])) {
745+
$mergedFeesObject[$typeLabel] = (float)$mergedFeesObject[$typeLabel] + $value;
746+
} else {
747+
$mergedFeesObject[$typeLabel] = $value;
748+
}
749+
}
750+
}
751+
752+
if (empty($mergedFeesObject)) {
753+
return [];
754+
}
755+
756+
$response = [];
757+
foreach ($mergedFeesObject as $typeLabel => $value) {
758+
$response[] = [
759+
'type' => $typeLabel,
760+
'value' => $value
761+
];
762+
}
763+
return $response;
764+
}
666765
}

0 commit comments

Comments
 (0)