Skip to content

Commit

Permalink
Merge pull request #29 from xendit/TPI-8705/metrics
Browse files Browse the repository at this point in the history
Metrics
  • Loading branch information
candrasaputra authored Dec 22, 2022
2 parents b38dcfe + 6cc662a commit 4b21b83
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
9 changes: 8 additions & 1 deletion modules/gateways/xendit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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(),
Expand Down
38 changes: 38 additions & 0 deletions modules/gateways/xendit/lib/XenditRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 4b21b83

Please sign in to comment.