@@ -29,6 +29,7 @@ class Flutterwave extends AbstractPayment
2929 use PaymentFactory;
3030
3131 private SignozServiceLogger $ signoz ;
32+ private ?array $ traceContext = null ;
3233
3334 /**
3435 * Flutterwave Construct
@@ -49,6 +50,9 @@ public function __construct()
4950 } else {
5051 $ this ->signoz = self ::$ config ->getSignoz ();
5152 }
53+
54+ $ this ->traceContext = $ this ->buildTraceContext ();
55+ $ this ->signoz ->setDefaultTraceContext ($ this ->traceContext );
5256 }
5357
5458 private function checkPageIsSecure ()
@@ -58,6 +62,40 @@ private function checkPageIsSecure()
5862 }
5963 }
6064
65+ private function buildTraceContext (?array $ parentContext = null ): array
66+ {
67+ $ timestamp = gmdate ('Y-m-d\TH:i:s.v\Z ' );
68+ $ traceId = $ parentContext ['trace_id ' ] ?? $ this ->generateTraceId ();
69+ $ parentSpanId = $ parentContext ['span_id ' ] ?? null ;
70+
71+ return [
72+ 'trace_id ' => $ traceId ,
73+ 'span_id ' => $ this ->generateSpanId (),
74+ 'parent_span_id ' => $ parentSpanId ,
75+ 'span_start_time ' => $ timestamp ,
76+ 'span_end_time ' => $ timestamp ,
77+ ];
78+ }
79+
80+ private function getTraceContextForEvent (): array
81+ {
82+ $ nextContext = $ this ->buildTraceContext ($ this ->traceContext );
83+ $ this ->traceContext = $ nextContext ;
84+ $ this ->signoz ->setDefaultTraceContext ($ this ->traceContext );
85+
86+ return $ nextContext ;
87+ }
88+
89+ private function generateTraceId (): string
90+ {
91+ return bin2hex (random_bytes (16 ));
92+ }
93+
94+ private function generateSpanId (): string
95+ {
96+ return bin2hex (random_bytes (8 ));
97+ }
98+
6199 /**
62100 * Sets the transaction amount
63101 *
@@ -227,6 +265,16 @@ public function setMetaData(array $meta): object
227265 return $ this ;
228266 }
229267
268+ /**
269+ * Enforce the same trace context for request, transaction, and error events.
270+ */
271+ public function setTraceContext (array $ traceContext ): object
272+ {
273+ $ this ->traceContext = $ this ->buildTraceContext ($ traceContext );
274+ $ this ->signoz ->setDefaultTraceContext ($ this ->traceContext );
275+ return $ this ;
276+ }
277+
230278 /**
231279 * Sets the event hooks for all available triggers
232280 *
@@ -257,6 +305,7 @@ public function requeryTransaction(string $referenceNumber): object
257305
258306 $ appId = $ this ->signoz ->getAppId ();
259307 $ environment = $ this ->signoz ->getCurrentEnvironment ();
308+ $ traceContext = $ this ->getTraceContextForEvent ();
260309
261310 $ data = [
262311 'id ' => (int ) $ referenceNumber ,
@@ -274,13 +323,13 @@ public function requeryTransaction(string $referenceNumber): object
274323 // Handle successful.
275324 if (isset ($ this ->handler )) {
276325 $ final_tx_ref = $ response ->data ->tx_ref ;
277- $ this ->signoz ->trackRequestSent ($ appId , $ environment , 'GET ' , $ referenceNumber , $ url );
326+ $ this ->signoz ->trackRequestSent ($ appId , $ environment , 'GET ' , $ final_tx_ref , $ url, $ traceContext );
278327 if ( 'production ' === $ environment ) {
279328 $ final_currency = $ response ->data ->currency ;
280329 $ final_amount = $ response ->data ->amount ;
281330 $ payment_type = $ response ->data ->payment_type ;
282331 $ final_fee = $ response ->data ->app_fee ;
283- $ this ->signoz ->trackTransaction ($ appId ,$ final_tx_ref , $ final_currency , (float ) $ final_amount , $ payment_type , (float ) $ final_fee );
332+ $ this ->signoz ->trackTransaction ($ appId ,$ final_tx_ref , $ final_currency , (float ) $ final_amount , $ payment_type , (float ) $ final_fee, $ traceContext );
284333 }
285334 $ this ->handler ->onSuccessful ($ response ->data );
286335 }
@@ -300,7 +349,7 @@ public function requeryTransaction(string $referenceNumber): object
300349 if ($ this ->requeryCount > 4 ) {
301350 // Now you have to setup a queue by force. We couldn't get a status in 5 requeries.
302351 if (isset ($ this ->handler )) {
303- $ this ->signoz ->trackError ($ appId , 'TIMEOUT_ERROR ' , 'timedout while requerying transaction with id: ' . $ referenceNumber );
352+ $ this ->signoz ->trackError ($ appId , 'TIMEOUT_ERROR ' , 'timedout while requerying transaction with id: ' . $ referenceNumber, $ traceContext );
304353 $ this ->handler ->onTimeout ($ this ->txref , $ response ->data );
305354 }
306355 } else {
@@ -312,7 +361,7 @@ public function requeryTransaction(string $referenceNumber): object
312361 }
313362 } else {
314363 // Handle Requery Error.
315- $ this ->signoz ->trackError ($ appId , 'REQUERY_ERROR ' , 'Failed to requery transaction with id: ' . $ referenceNumber );
364+ $ this ->signoz ->trackError ($ appId , 'REQUERY_ERROR ' , 'Failed to requery transaction with id: ' . $ referenceNumber, $ traceContext );
316365 if (isset ($ this ->handler )) {
317366 $ this ->handler ->onRequeryError ($ response ->data );
318367 }
@@ -321,60 +370,41 @@ public function requeryTransaction(string $referenceNumber): object
321370 }
322371
323372 /**
324- * Generates the final json to be used in configuring the payment call to the rave payment gateway
373+ * @deprecated Use render('inline')->getHtml() instead.
374+ * Will be removed in a future version.
325375 */
326376 public function initialize (): void
327377 {
378+ $ this ->traceContext = $ this ->buildTraceContext ($ this ->traceContext );
379+ $ this ->signoz ->setDefaultTraceContext ($ this ->traceContext );
380+
381+ @trigger_error (
382+ 'initialize() is deprecated and will be removed in a future version. Use render( \'inline \')->with([...])->getHtml() instead. ' ,
383+ E_USER_DEPRECATED
384+ );
385+
328386 $ this ->createCheckSum ();
329387
330- $ appId = $ this ->signoz ->getAppId ();
331- $ environment = $ this ->signoz ->getCurrentEnvironment ();
388+ $ checkoutConfig = [
389+ 'public_key ' => self ::$ config ->getPublicKey (),
390+ 'tx_ref ' => $ this ->txref ,
391+ 'amount ' => (float ) $ this ->amount ,
392+ 'currency ' => $ this ->currency ,
393+ 'country ' => $ this ->country ,
394+ 'redirect_url ' => $ this ->redirectUrl ,
395+ 'payment_method ' => $ this ->paymentOptions ,
396+ 'email ' => $ this ->customerEmail ,
397+ 'phone_number ' => $ this ->customerPhone ,
398+ 'first_name ' => $ this ->customerFirstname ,
399+ 'last_name ' => $ this ->customerLastname ,
400+ 'customizations ' => [
401+ 'title ' => $ this ->customTitle ,
402+ 'description ' => $ this ->customDescription ,
403+ 'logo ' => $ this ->customLogo ,
404+ ],
405+ ];
332406
333- $ this ->signoz ->trackRequestSent ($ appId , $ environment , 'GET ' , $ this ->txref , '/inline ' );
334-
335- $ this ->logger ->info ('Rendering Payment Modal.. ' );
336-
337- echo '<html lang="en"> ' ;
338- echo '<body> ' ;
339- // $loader_img_src = FLW_PHP_ASSET_DIR."js/v3.js";
340- echo '<div style="display: flex; flex-direction: row;justify-content: center; align-content: center ">
341- Proccessing...<img src="../assets/images/ajax-loader.gif" alt="loading-gif"/></div> ' ;
342- // $script_src = FLW_PHP_ASSET_DIR."js/v3.js";
343- echo '<script type="text/javascript" src="https://checkout.flutterwave.com/v3.js"></script> ' ;
344-
345- echo '<script> ' ;
346- echo 'document.addEventListener("DOMContentLoaded", function(event) { ' ;
347- echo 'FlutterwaveCheckout({
348- public_key: " ' . self ::$ config ->getPublicKey () . '",
349- tx_ref: " ' . $ this ->txref . '",
350- amount: ' . $ this ->amount . ',
351- currency: " ' . $ this ->currency . '",
352- country: " ' . $ this ->country . '",
353- payment_options: "card,ussd,mpesa,barter,mobilemoneyghana,
354- mobilemoneyrwanda,mobilemoneyzambia,mobilemoneyuganda,banktransfer,account",
355- redirect_url:" ' . $ this ->redirectUrl . '",
356- customer: {
357- email: " ' . $ this ->customerEmail . '",
358- phone_number: " ' . $ this ->customerPhone . '",
359- name: " ' . $ this ->customerFirstname . ' ' . $ this ->customerLastname . '",
360- },
361- callback: function (data) {
362- console.log(data);
363- },
364- onclose: function() {
365- window.location = "?cancelled=cancelled&cancel_ref= ' . $ this ->txref . '";
366- },
367- customizations: {
368- title: " ' . $ this ->customTitle . '",
369- description: " ' . $ this ->customDescription . '",
370- logo: " ' . $ this ->customLogo . '",
371- }
372- }); ' ;
373- echo '}); ' ;
374- echo '</script> ' ;
375- echo '</body> ' ;
376- echo '</html> ' ;
377- $ this ->logger ->info ('Rendered Payment Modal Successfully.. ' );
407+ echo $ this ->render (Modal::POPUP )->with ($ checkoutConfig )->getHtml ();
378408 }
379409
380410 /**
0 commit comments