diff --git a/modules/gateways/xendit.php b/modules/gateways/xendit.php index 966a82b..f7b0017 100644 --- a/modules/gateways/xendit.php +++ b/modules/gateways/xendit.php @@ -10,7 +10,7 @@ require __DIR__ . '/xendit/autoload.php'; // Module version -const XENDIT_PAYMENT_GATEWAY_VERSION = '1.1.0'; +const XENDIT_PAYMENT_GATEWAY_VERSION = '1.2.0'; use WHMCS\Billing\Invoice; use Xendit\Lib\ActionBase; @@ -74,9 +74,13 @@ function xendit_deactivate() function xendit_link($params) { $paymentLink = new PaymentLink(); + $xenditRequest = new XenditRequest(); try { return $paymentLink->generatePaymentLink($params); } catch (\Exception $e) { + $metricPayload = $xenditRequest->constructMetricPayload('whmcs_checkout', 'error', $payment_method); + $xenditRequest->trackMetricCount($metricPayload); + return $paymentLink->errorMessage($e->getMessage()); } } @@ -426,6 +430,9 @@ function xendit_refund($params) try { $refundResponse = $xenditRequest->createRefund($chargeId, $body); } catch (Exception $e) { + $metricPayload = $xenditRequest->constructMetricPayload('whmcs_refund', 'error', $payment_method); + $xenditRequest->trackMetricCount($metricPayload); + return array( 'status' => 'declined', 'rawdata' => $e->getMessage(), diff --git a/modules/gateways/xendit/lib/XenditRequest.php b/modules/gateways/xendit/lib/XenditRequest.php index 17788fa..29129f9 100644 --- a/modules/gateways/xendit/lib/XenditRequest.php +++ b/modules/gateways/xendit/lib/XenditRequest.php @@ -263,4 +263,42 @@ public function getCardSettings() throw new \Exception($e->getMessage()); } } + + /** + * @param array $param + * @return true|false|string + * @throws \Exception + */ + public function trackMetricCount(array $payload = []) + { + try { + $response = $this->request( + '/log/metrics/count', + [ + 'headers' => $this->defaultHeader(), + 'body' => json_encode($payload) + ], + "POST" + ); + return true; + } catch (\Exception $e) { + return false; + } + } + + function constructMetricPayload($name, $type, $error_code = '') + { + $metrics = array( + 'name' => $name, + 'additional_tags' => array( + 'type' => $type + ) + ); + + if ($error_code) { + $metrics['additional_tags']['error_code'] = $error_code; + } + + return $metrics; + } }