Skip to content

Commit 045ee43

Browse files
authored
Merge pull request #56 from xendit/bugfix/xendit-sprint
add sprint logic in overviewpost
2 parents c4aeff1 + 0f32f1a commit 045ee43

17 files changed

+334
-259
lines changed

CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# CHANGELOG
22

3+
## 2.2.2 (2020-10-01)
4+
5+
Bugfix:
6+
7+
- Handle invoice paid with OVO
8+
- Remove auto cancellation feature for now
9+
10+
## 2.2.1 (2020-09-28)
11+
12+
Improvements:
13+
14+
- Add `Send Email Notification` setting
15+
- Fix invoice email format
16+
- Disable Xendit multishipping when plugin in not enabled
17+
- Support Sprint multishipping in Xendit's ecosystem
18+
319
## 2.2.0 (2020-09-15)
420

521
Features:

Xendit/M2Invoice/Controller/Checkout/CCCallback.m22.php

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function execute()
3636

3737
if (isset($hostedPayment['error_code'])) {
3838
$isError = true;
39-
$this->handlePaymentFailure($order, $hostedPayment['error_code'], $hostedPayment['error_code'] . ' - Error reconciliating', $shouldRedirect);
4039
}
4140
else {
4241
if ($hostedPayment['paid_amount'] != $hostedPayment['amount']) {

Xendit/M2Invoice/Controller/Checkout/Invoice.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private function getApiRequestData($order)
6666
'description' => $orderId,
6767
'payer_email' => $order->getCustomerEmail(),
6868
'preferred_method' => $preferredMethod,
69-
'should_send_email' => "true",
69+
'should_send_email' => $this->getDataHelper()->getSendInvoiceEmail() ? "true" : "false",
7070
'platform_callback_url' => $this->getXenditCallbackUrl(),
7171
'client_type' => 'INTEGRATION'
7272
];

Xendit/M2Invoice/Controller/Checkout/InvoiceMultishipping.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class InvoiceMultishipping extends AbstractAction
1212
public function execute()
1313
{
1414
try {
15-
$billingEmail = $this->getRequest()->getParam('billing_email');
1615
$rawOrderIds = $this->getRequest()->getParam('order_ids');
1716
$orderIds = explode("-", $rawOrderIds);
1817

@@ -32,6 +31,7 @@ public function execute()
3231
$order->save();
3332

3433
$transactionAmount += (int)$order->getTotalDue();
34+
$billingEmail = $order->getCustomerEmail();
3535
}
3636

3737
$preferredMethod = $this->getRequest()->getParam('preferred_method');
@@ -43,7 +43,7 @@ public function execute()
4343
'description' => $rawOrderIds,
4444
'payer_email' => $billingEmail,
4545
'preferred_method' => $preferredMethod,
46-
'should_send_email' => "true",
46+
'should_send_email' => $this->getDataHelper()->getSendInvoiceEmail() ? "true" : "false",
4747
'platform_callback_url' => $this->getXenditCallbackUrl(),
4848
'client_type' => 'INTEGRATION'
4949
];
@@ -59,6 +59,7 @@ public function execute()
5959
return $resultRedirect;
6060
} catch (\Exception $e) {
6161
$message = 'Exception caught on xendit/checkout/redirect: ' . $e->getMessage();
62+
$this->getLogger()->info($message);
6263
return $this->redirectToCart("There was an error in the Xendit payment. Failure reason: Unexpected Error");
6364
}
6465
}

Xendit/M2Invoice/Controller/Checkout/Notification.m22.php

+90-79
Original file line numberDiff line numberDiff line change
@@ -54,54 +54,7 @@ public function execute()
5454
try {
5555
$post = $this->getRequest()->getContent();
5656
$callbackToken = $this->getRequest()->getHeader('X-CALLBACK-TOKEN');
57-
$decodedPost = json_decode($post, true);
58-
$isEwallet = false;
59-
60-
if (!empty($decodedPost['ewallet_type'])) {
61-
$isEwallet = true;
62-
63-
if (!$decodedPost['external_id']) {
64-
$result = $this->jsonResultFactory->create();
65-
/** You may introduce your own constants for this custom REST API */
66-
$result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST);
67-
$result->setData([
68-
'status' => __('ERROR'),
69-
'message' => 'Callback external_id is invalid'
70-
]);
71-
72-
return $result;
73-
}
74-
} else if (!isset($decodedPost['description']) || !isset($decodedPost['id'])) {
75-
$result = $this->jsonResultFactory->create();
76-
/** You may introduce your own constants for this custom REST API */
77-
$result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST);
78-
$result->setData([
79-
'status' => __('ERROR'),
80-
'message' => 'Callback body is invalid'
81-
]);
82-
83-
return $result;
84-
}
85-
86-
if ($isEwallet) {
87-
// default code if API doesn't send failure_code
88-
$failureCode = 'UNKNOWN_ERROR';
89-
if (isset($decodedPost['failure_code'])) {
90-
$failureCode = $decodedPost['failure_code'];
91-
}
92-
93-
$extIdPrefix = $this->dataHelper->getExternalIdPrefix();
94-
$orderId = ltrim($decodedPost['external_id'], $extIdPrefix);
95-
96-
// standalone OVO can only be single checkout
97-
$orderIds = [$orderId];
98-
} else {
99-
$orderId = $decodedPost['description'];
100-
$orderIds = explode("-", $orderId);
101-
}
102-
103-
$transactionId = $decodedPost['id'];
104-
$isMultishipping = (count($orderIds) > 1) ? true : false;
57+
$callbackPayload = json_decode($post, true);
10558

10659
if (!empty($callbackToken)) {
10760
$result = $this->jsonResultFactory->create();
@@ -115,20 +68,14 @@ public function execute()
11568
return $result;
11669
}
11770

118-
$invoice = $this->getXenditInvoice($transactionId);
119-
120-
if( $isMultishipping ) {
121-
foreach ($orderIds as $key => $value) {
122-
$result = $this->checkOrder($value, $isEwallet, $decodedPost, $invoice, $orderId);
123-
}
124-
125-
return $result;
71+
if (!empty($callbackPayload['invoice_url'])) {
72+
return $this->handleInvoiceCallback($callbackPayload);
12673
} else {
127-
return $this->checkOrder($orderId, $isEwallet, $decodedPost, $invoice, $orderId);
74+
return $this->handleEwalletCallback($callbackPayload);
12875
}
12976
} catch (\Exception $e) {
130-
$message = "Error invoice callback" . $e->getMessage();
131-
$this->logDNA->log(LogDNALevel::ERROR, $message, $decodedPost);
77+
$message = "Error invoice callback: " . $e->getMessage();
78+
$this->logDNA->log(LogDNALevel::ERROR, $message, $callbackPayload);
13279

13380
$result = $this->jsonResultFactory->create();
13481
/** You may introduce your own constants for this custom REST API */
@@ -141,11 +88,79 @@ public function execute()
14188
return $result;
14289
}
14390
}
144-
145-
private function checkOrder($orderId, $isEwallet, $decodedPost, $invoice, $callbackDescription) {
146-
$order = $this->orderFactory->create();
147-
$order->load($orderId);
148-
$transactionId = $decodedPost['id'];
91+
92+
public function handleInvoiceCallback($callbackPayload) {
93+
if (!isset($callbackPayload['description']) || !isset($callbackPayload['id'])) {
94+
$result = $this->jsonResultFactory->create();
95+
/** You may introduce your own constants for this custom REST API */
96+
$result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST);
97+
$result->setData([
98+
'status' => __('ERROR'),
99+
'message' => 'Callback body is invalid'
100+
]);
101+
102+
return $result;
103+
}
104+
105+
// Invoice description is Magento's order ID
106+
$description = $callbackPayload['description'];
107+
// in case of multishipping, we separate order IDs with `-`
108+
$orderIds = explode("-", $description);
109+
110+
$transactionId = $callbackPayload['id'];
111+
$isMultishipping = (count($orderIds) > 1) ? true : false;
112+
113+
$invoice = $this->getXenditInvoice($transactionId);
114+
115+
if( $isMultishipping ) {
116+
foreach ($orderIds as $key => $value) {
117+
$order = $this->orderFactory->create();
118+
$order->load($value);
119+
120+
$result = $this->checkOrder($order, false, $callbackPayload, $invoice, $description);
121+
}
122+
123+
return $result;
124+
} else {
125+
$order = $this->getOrderById($description);
126+
127+
if (!$order) {
128+
$order = $this->orderFactory->create();
129+
$order->load($description);
130+
}
131+
132+
return $this->checkOrder($order, false, $callbackPayload, $invoice, $description);
133+
}
134+
}
135+
136+
public function handleEwalletCallback($callbackPayload) {
137+
if (!$callbackPayload['external_id']) {
138+
$result = $this->jsonResultFactory->create();
139+
/** You may introduce your own constants for this custom REST API */
140+
$result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST);
141+
$result->setData([
142+
'status' => __('ERROR'),
143+
'message' => 'Callback external_id is invalid'
144+
]);
145+
146+
return $result;
147+
}
148+
149+
$failureCode = 'UNKNOWN_ERROR';
150+
if (isset($callbackPayload['failure_code'])) {
151+
$failureCode = $callbackPayload['failure_code'];
152+
}
153+
154+
$extIdPrefix = $this->dataHelper->getExternalIdPrefix();
155+
// Trimmed external ID from prefix is Magento's order ID
156+
$orderId = ltrim($callbackPayload['external_id'], $extIdPrefix);
157+
$order = $this->getOrderById($orderId);
158+
159+
return $this->checkOrder($order, true, $callbackPayload, null, $orderId);
160+
}
161+
162+
private function checkOrder($order, $isEwallet, $callbackPayload, $invoice, $callbackDescription) {
163+
$transactionId = $callbackPayload['id'];
149164

150165
if (!$order) {
151166
$result = $this->jsonResultFactory->create();
@@ -159,26 +174,22 @@ private function checkOrder($orderId, $isEwallet, $decodedPost, $invoice, $callb
159174
return $result;
160175
}
161176

162-
if ($isEwallet) {
163-
$order = $this->getOrderById($orderId);
177+
if (!$order->canInvoice()) {
178+
$result = $this->jsonResultFactory->create();
179+
$result->setData([
180+
'status' => __('SUCCESS'),
181+
'message' => 'Order is already processed'
182+
]);
164183

165-
if ($order->getState() === Order::STATE_PENDING_PAYMENT || $order->getState() === Order::STATE_PAYMENT_REVIEW) {
166-
//get ewallet payment status
167-
$paymentStatus = $this->getEwalletStatus($decodedPost['ewallet_type'], $decodedPost['external_id']);
168-
} else {
169-
$result = $this->jsonResultFactory->create();
170-
$result->setData([
171-
'status' => __('SUCCESS'),
172-
'message' => 'eWallet transaction has been completed successfully'
173-
]);
184+
return $result;
185+
}
174186

175-
return $result;
176-
}
187+
if ($isEwallet) {
188+
$paymentStatus = $this->getEwalletStatus($callbackPayload['ewallet_type'], $callbackPayload['external_id']);
177189
} else {
178190
$paymentStatus = $invoice['status'];
179-
$invoiceOrderId = $invoice['description'];
180191

181-
if ($invoiceOrderId !== $callbackDescription) {
192+
if ($invoice['description'] !== $callbackDescription) {
182193
$result = $this->jsonResultFactory->create();
183194
/** You may introduce your own constants for this custom REST API */
184195
$result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST);

0 commit comments

Comments
 (0)