Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/Flutterwave.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Flutterwave extends AbstractPayment
use PaymentFactory;

private SignozServiceLogger $signoz;
private ?array $traceContext = null;

/**
* Flutterwave Construct
Expand All @@ -49,6 +50,9 @@ public function __construct()
} else {
$this->signoz = self::$config->getSignoz();
}

$this->traceContext = $this->buildTraceContext();
$this->signoz->setDefaultTraceContext($this->traceContext);
}

private function checkPageIsSecure()
Expand All @@ -58,6 +62,40 @@ private function checkPageIsSecure()
}
}

private function buildTraceContext(?array $parentContext = null): array
{
$timestamp = gmdate('Y-m-d\TH:i:s.v\Z');
$traceId = $parentContext['trace_id'] ?? $this->generateTraceId();
$parentSpanId = $parentContext['span_id'] ?? null;

return [
'trace_id' => $traceId,
'span_id' => $this->generateSpanId(),
'parent_span_id' => $parentSpanId,
'span_start_time' => $timestamp,
'span_end_time' => $timestamp,
];
}

private function getTraceContextForEvent(): array
{
$nextContext = $this->buildTraceContext($this->traceContext);
$this->traceContext = $nextContext;
$this->signoz->setDefaultTraceContext($this->traceContext);

return $nextContext;
}

private function generateTraceId(): string
{
return bin2hex(random_bytes(16));
}

private function generateSpanId(): string
{
return bin2hex(random_bytes(8));
}

/**
* Sets the transaction amount
*
Expand Down Expand Up @@ -227,6 +265,16 @@ public function setMetaData(array $meta): object
return $this;
}

/**
* Enforce the same trace context for request, transaction, and error events.
*/
public function setTraceContext(array $traceContext): object
{
$this->traceContext = $this->buildTraceContext($traceContext);
$this->signoz->setDefaultTraceContext($this->traceContext);
return $this;
}

/**
* Sets the event hooks for all available triggers
*
Expand Down Expand Up @@ -257,6 +305,7 @@ public function requeryTransaction(string $referenceNumber): object

$appId = $this->signoz->getAppId();
$environment = $this->signoz->getCurrentEnvironment();
$traceContext = $this->getTraceContextForEvent();

$data = [
'id' => (int) $referenceNumber,
Expand All @@ -274,13 +323,13 @@ public function requeryTransaction(string $referenceNumber): object
// Handle successful.
if (isset($this->handler)) {
$final_tx_ref = $response->data->tx_ref;
$this->signoz->trackRequestSent($appId, $environment, 'GET', $referenceNumber, $url );
$this->signoz->trackRequestSent($appId, $environment, 'GET', $final_tx_ref, $url, $traceContext);
if( 'production' === $environment ) {
$final_currency = $response->data->currency;
$final_amount = $response->data->amount;
$payment_type = $response->data->payment_type;
$final_fee = $response->data->app_fee;
$this->signoz->trackTransaction($appId,$final_tx_ref, $final_currency, (float) $final_amount, $payment_type, (float) $final_fee);
$this->signoz->trackTransaction($appId,$final_tx_ref, $final_currency, (float) $final_amount, $payment_type, (float) $final_fee, $traceContext);
}
$this->handler->onSuccessful($response->data);
}
Expand All @@ -300,7 +349,7 @@ public function requeryTransaction(string $referenceNumber): object
if ($this->requeryCount > 4) {
// Now you have to setup a queue by force. We couldn't get a status in 5 requeries.
if (isset($this->handler)) {
$this->signoz->trackError($appId, 'TIMEOUT_ERROR', 'timedout while requerying transaction with id: ' . $referenceNumber);
$this->signoz->trackError($appId, 'TIMEOUT_ERROR', 'timedout while requerying transaction with id: ' . $referenceNumber, $traceContext);
$this->handler->onTimeout($this->txref, $response->data);
}
} else {
Expand All @@ -312,7 +361,7 @@ public function requeryTransaction(string $referenceNumber): object
}
} else {
// Handle Requery Error.
$this->signoz->trackError($appId, 'REQUERY_ERROR', 'Failed to requery transaction with id: ' . $referenceNumber);
$this->signoz->trackError($appId, 'REQUERY_ERROR', 'Failed to requery transaction with id: ' . $referenceNumber, $traceContext);
if (isset($this->handler)) {
$this->handler->onRequeryError($response->data);
}
Expand All @@ -326,6 +375,9 @@ public function requeryTransaction(string $referenceNumber): object
*/
public function initialize(): void
{
$this->traceContext = $this->buildTraceContext($this->traceContext);
$this->signoz->setDefaultTraceContext($this->traceContext);

@trigger_error(
'initialize() is deprecated and will be removed in a future version. Use render(\'inline\')->with([...])->getHtml() instead.',
E_USER_DEPRECATED
Expand Down
1 change: 1 addition & 0 deletions src/Library/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function getHtml()

$this->logger->info('Rendered Payment Modal Successfully..');
$signoz->trackRequestSent($appId, $environment, 'GET', $payload['tx_ref'], '/inline');

return $html;
}

Expand Down
Loading
Loading