Skip to content

Commit

Permalink
Merge pull request #30 from xendit/TPI-8722/whmcs
Browse files Browse the repository at this point in the history
Improve tracking metric logging
  • Loading branch information
andykim authored Jan 5, 2023
2 parents 4b21b83 + 3bc9e2d commit a325214
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 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
28 changes: 20 additions & 8 deletions modules/gateways/xendit/lib/XenditRequest.php
Original file line number Diff line number Diff line change
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 a325214

Please sign in to comment.