Skip to content

Commit

Permalink
Improve tracking metric logging
Browse files Browse the repository at this point in the history
  • Loading branch information
andykim committed Jan 5, 2023
1 parent 4b21b83 commit 6517bd3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
20 changes: 16 additions & 4 deletions modules/gateways/xendit.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ function xendit_link($params)
$xenditRequest = new XenditRequest();
try {
return $paymentLink->generatePaymentLink($params);
} catch (\Exception $e) {
$metricPayload = $xenditRequest->constructMetricPayload('whmcs_checkout', 'error', $payment_method);
} catch (\Throwable $e) {
$metricPayload = $xenditRequest->constructMetricPayload(
'whmcs_checkout',
array(
'type' => 'error',
'error_message' => $e->getMessage()
)
);
$xenditRequest->trackMetricCount($metricPayload);

return $paymentLink->errorMessage($e->getMessage());
Expand Down Expand Up @@ -429,8 +435,14 @@ function xendit_refund($params)

try {
$refundResponse = $xenditRequest->createRefund($chargeId, $body);
} catch (Exception $e) {
$metricPayload = $xenditRequest->constructMetricPayload('whmcs_refund', 'error', $payment_method);
} catch (\Throwable $e) {
$metricPayload = $xenditRequest->constructMetricPayload(
'whmcs_refund',
array(
'type' => 'error',
'error_message' => $e->getMessage()
)
);
$xenditRequest->trackMetricCount($metricPayload);

return array(
Expand Down
30 changes: 21 additions & 9 deletions modules/gateways/xendit/lib/XenditRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class XenditRequest
{
protected $tpi_server_domain = "https://tpi.xendit.co";
protected $tpi_server_domain = "https://tpi-service-live.us-west-2.stg.tidnex.dev";

/**
* @return mixed
Expand Down Expand Up @@ -265,14 +265,13 @@ public function getCardSettings()
}

/**
* @param array $param
* @return true|false|string
* @throws \Exception
* @param array $payload
* @return bool
*/
public function trackMetricCount(array $payload = [])
{
try {
$response = $this->request(
$this->request(
'/log/metrics/count',
[
'headers' => $this->defaultHeader(),
Expand All @@ -286,12 +285,25 @@ public function trackMetricCount(array $payload = [])
}
}

function constructMetricPayload($name, $type, $error_code = '')
{
/**
* @param string $name
* @param array $additional_tags
* @param string $error_code
* @return array
*/
public function constructMetricPayload(
string $name,
array $additional_tags,
string $error_code = ''
): array {
$metrics = array(
'name' => $name,
'additional_tags' => array(
'type' => $type
'additional_tags' => array_merge(
array(
'version' => XENDIT_PAYMENT_GATEWAY_VERSION,
'is_live' => $this->isTestMode()
),
$additional_tags
)
);

Expand Down

0 comments on commit 6517bd3

Please sign in to comment.