Skip to content

Commit

Permalink
Merge pull request #154 from xendit/TPI-8701/implement-adjust-metric-…
Browse files Browse the repository at this point in the history
…in-magento

Implement TPI metrics
  • Loading branch information
andykim authored Jan 5, 2023
2 parents e57f570 + bb41f48 commit 74cdb22
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 80 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 9.0.1 (2022-12-16)
Features:
- Implement Xendit metric
- Fix get payment_method from URL query

## 9.0.0 (2022-10-26)
Features:
- Add new IDR payment: Atome
Expand Down
67 changes: 30 additions & 37 deletions Controller/Checkout/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Xendit\M2Invoice\Helper\Crypto;
use Xendit\M2Invoice\Helper\Data;
use Xendit\M2Invoice\Helper\ErrorHandler;
use Xendit\M2Invoice\Helper\Metric;

/**
* Class AbstractAction
Expand Down Expand Up @@ -146,8 +147,14 @@ abstract class AbstractAction extends Action
*/
private $multishipping;

/**
* @var Metric
*/
protected $metricHelper;

/**
* AbstractAction constructor.
*
* @param Session $checkoutSession
* @param Context $context
* @param CategoryFactory $categoryFactory
Expand All @@ -168,6 +175,7 @@ abstract class AbstractAction extends Action
* @param ErrorHandler $errorHandler
* @param State $state
* @param Multishipping $multishipping
* @param Metric $metricHelper
*/
public function __construct(
Session $checkoutSession,
Expand All @@ -189,7 +197,8 @@ public function __construct(
CustomerSession $customerSession,
ErrorHandler $errorHandler,
State $state,
Multishipping $multishipping
Multishipping $multishipping,
Metric $metricHelper
) {
parent::__construct($context);

Expand All @@ -215,6 +224,7 @@ public function __construct(
$this->errorHandler = $errorHandler;
$this->state = $state;
$this->multishipping = $multishipping;
$this->metricHelper = $metricHelper;
}

/**
Expand All @@ -233,22 +243,6 @@ protected function getCheckoutSession()
return $this->checkoutSession;
}

/**
* @return CategoryFactory
*/
protected function getCategoryFactory()
{
return $this->categoryFactory;
}

/**
* @return OrderFactory
*/
protected function getOrderFactory()
{
return $this->orderFactory;
}

/**
* @return LoggerInterface
*/
Expand Down Expand Up @@ -479,14 +473,6 @@ protected function getXenditCallbackUrl()
return $baseUrl . 'xendit/checkout/notification';
}

/**
* @return CookieManagerInterface
*/
protected function getCookieManager()
{
return $this->cookieManager;
}

protected function getStateMultishipping()
{
return $this->state;
Expand Down Expand Up @@ -518,19 +504,26 @@ protected function orderValidToCreateXenditInvoice(Order $order): bool
}

/**
* @return mixed|string
*/
protected function getPreferredMethod()
{
$preferredMethod = $this->getRequest()->getParam('preferred_method');
if ($preferredMethod == 'cc') {
$preferredMethod = 'CREDIT_CARD';
}

if ($preferredMethod == 'shopeepayph') {
$preferredMethod = 'SHOPEEPAY';
* Get preferred payment from order
*
* @param Order $order
* @return false|string
*/
protected function getPreferredMethod(Order $order)
{
$payment = $order->getPayment();
$preferredMethod = $this->getDataHelper()->xenditPaymentMethod(
$payment->getMethod()
);

switch ($preferredMethod) {
case 'cc':
return 'CREDIT_CARD';
case 'shopeepayph':
return 'SHOPEEPAY';
default:
return $preferredMethod;
}
return $preferredMethod;
}

/**
Expand Down
22 changes: 16 additions & 6 deletions Controller/Checkout/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Invoice extends AbstractAction
*/
public function execute()
{
$order = $this->getOrder();
try {
$order = $this->getOrder();
$apiData = $this->getApiRequestData($order);

if ($order->getState() === Order::STATE_NEW) {
Expand All @@ -39,13 +39,24 @@ public function execute()
$this->getLogger()->debug('Order in unrecognized state: ' . $order->getState());
$this->_redirect('checkout/cart');
}
} catch (\Exception $e) {
} catch (\Throwable $e) {
$message = 'Exception caught on xendit/checkout/invoice: ' . $e->getMessage();

$this->getLogger()->debug('Exception caught on xendit/checkout/invoice: ' . $message);
$this->getLogger()->debug($e->getTraceAsString());

$this->cancelOrder($order, $e->getMessage());

// log metric error
$this->metricHelper->sendMetric(
'magento2_checkout',
[
'type' => 'error',
'payment_method' => $this->getPreferredMethod($order),
'error_message' => $e->getMessage()
]
);

return $this->redirectToCart($message);
}
}
Expand All @@ -54,6 +65,7 @@ public function execute()
* @param $order
* @return array|void
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws LocalizedException
*/
private function getApiRequestData($order)
{
Expand All @@ -65,9 +77,7 @@ private function getApiRequestData($order)
}

$orderId = $order->getRealOrderId();
$preferredMethod = $this->getRequest()->getParam('preferred_method');

$shippingAddress = $order->getShippingAddress();
$preferredMethod = $this->getPreferredMethod($order);
$orderItems = $order->getAllItems();
$items = [];
foreach ($orderItems as $orderItem) {
Expand Down Expand Up @@ -115,7 +125,7 @@ private function getApiRequestData($order)
*/
private function createInvoice($requestData)
{
$invoiceUrl = $this->getDataHelper()->getCheckoutUrl() . "/payment/xendit/invoice";
$invoiceUrl = $this->getDataHelper()->getXenditApiUrl() . "/payment/xendit/invoice";
$invoiceMethod = Request::METHOD_POST;
$invoice = '';

Expand Down
43 changes: 29 additions & 14 deletions Controller/Checkout/InvoiceMultishipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@
class InvoiceMultishipping extends AbstractAction
{
/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Redirect|\Magento\Framework\Controller\ResultInterface
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Redirect|\Magento\Framework\Controller\ResultInterface|null
* @throws LocalizedException
*/
public function execute()
{
$transactionAmount = 0;
$orderProcessed = false;
$orders = [];
$items = [];
$currency = '';
$billingEmail = '';
$customerObject = [];

$orderIncrementIds = [];
$preferredMethod = '';

try {
$orderIds = $this->getMultiShippingOrderIds();
if (empty($orderIds)) {
Expand All @@ -27,17 +39,6 @@ public function execute()
return $this->redirectToCart($message);
}

$transactionAmount = 0;
$orderProcessed = false;
$orders = [];
$items = [];
$currency = '';
$billingEmail = '';
$customerObject = [];

$orderIncrementIds = [];
$preferredMethod = $this->getPreferredMethod();

foreach ($orderIds as $orderId) {
$order = $this->getOrderRepo()->get($orderId);
if (!$this->orderValidToCreateXenditInvoice($order)) {
Expand All @@ -46,6 +47,9 @@ public function execute()
return $this->redirectToCart($message);
}

if (empty($preferredMethod)) {
$preferredMethod = $this->getPreferredMethod($order);
}
$orderIncrementIds[] = $order->getRealOrderId();

$orderState = $order->getState();
Expand Down Expand Up @@ -122,9 +126,20 @@ public function execute()
$resultRedirect = $this->getRedirectFactory()->create();
$resultRedirect->setUrl($redirectUrl);
return $resultRedirect;
} catch (\Exception $e) {
} catch (\Throwable $e) {
$message = 'Exception caught on xendit/checkout/redirect: ' . $e->getMessage();
$this->getLogger()->info($message);

// log metric error
$this->metricHelper->sendMetric(
'magento2_checkout',
[
'type' => 'error',
'payment_method' => $preferredMethod,
'error_message' => $e->getMessage()
]
);

return $this->redirectToCart($message);
}
}
Expand All @@ -136,7 +151,7 @@ public function execute()
*/
private function createInvoice($requestData)
{
$invoiceUrl = $this->getDataHelper()->getCheckoutUrl() . "/payment/xendit/invoice";
$invoiceUrl = $this->getDataHelper()->getXenditApiUrl() . "/payment/xendit/invoice";
$invoiceMethod = Request::METHOD_POST;

try {
Expand Down
2 changes: 1 addition & 1 deletion Controller/Checkout/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private function checkOrder(Order $order, $callbackPayload, $invoice, $callbackD
*/
private function getXenditInvoice($invoiceId)
{
$invoiceUrl = $this->dataHelper->getCheckoutUrl() . "/payment/xendit/invoice/$invoiceId";
$invoiceUrl = $this->dataHelper->getXenditApiUrl() . "/payment/xendit/invoice/$invoiceId";

$this->logger->info("getXenditInvoice");
$this->logger->info($invoiceUrl);
Expand Down
2 changes: 1 addition & 1 deletion Controller/Payment/OverviewPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function execute()
);
$this->_redirect('*/*/billing');
}
$redirect = $baseUrl . '/xendit/checkout/invoicemultishipping?preferred_method=' . $xenditPaymentMethod;
$redirect = $baseUrl . '/xendit/checkout/invoicemultishipping';
$this->_redirect($redirect);
} else {
//OTHERS
Expand Down
2 changes: 1 addition & 1 deletion Helper/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function getHeaders($isPublicRequest, $preferredMethod = null, $customHe
'Content-Type' => 'application/json',
'x-plugin-name' => 'MAGENTO2',
'user-agent' => 'Magento 2 Module',
'x-plugin-version' => '9.0.0'
'x-plugin-version' => \Xendit\M2Invoice\Helper\Data::XENDIT_M2INVOICE_VERSION
];

if ($preferredMethod !== null) {
Expand Down
4 changes: 3 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
class Data extends AbstractHelper
{
const XENDIT_M2INVOICE_VERSION = '9.0.1';

/**
* @var StoreManagerInterface
*/
Expand Down Expand Up @@ -173,7 +175,7 @@ protected function getStoreManager()
/**
* @return mixed
*/
public function getCheckoutUrl()
public function getXenditApiUrl()
{
return $this->xendit->getConfigData('xendit_url');
}
Expand Down
Loading

0 comments on commit 74cdb22

Please sign in to comment.