Skip to content

Commit a9128db

Browse files
fix: Transaction Service Event Tracker
The Transaction Service no longer requires a separate event handler as it not has the trait of a event handler.
1 parent a3dfa35 commit a9128db

File tree

2 files changed

+77
-22
lines changed

2 files changed

+77
-22
lines changed

src/Service/Transactions.php

+21-22
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace Flutterwave\Service;
66

77
use Flutterwave\Contract\ConfigInterface;
8-
use Flutterwave\EventHandlers\TransactionVerificationEventHandler;
8+
use Flutterwave\EventHandlers\EventTracker;
99
use Flutterwave\Traits\ApiOperations\Post;
1010
use Psr\Http\Client\ClientExceptionInterface;
1111

1212
class Transactions extends Service
1313
{
14+
use EventTracker;
1415
use Post;
1516

1617
public const ENDPOINT = 'transactions';
@@ -26,14 +27,12 @@ class Transactions extends Service
2627
private array $payment_type = [
2728
'card','debit_ng_account','mobilemoney','bank_transfer', 'ach_payment',
2829
];
29-
private TransactionVerificationEventHandler $eventHandler;
3030

3131
public function __construct(?ConfigInterface $config = null)
3232
{
3333
parent::__construct($config);
34-
$this->baseUrl = $this->config::BASE_URL;
3534
$this->end_point = Transactions::ENDPOINT;
36-
$this->eventHandler = new TransactionVerificationEventHandler();
35+
3736
}
3837

3938
/**
@@ -43,13 +42,13 @@ public function verify(string $transactionId): \stdClass
4342
{
4443
$this->checkTransactionId($transactionId);
4544
$this->logger->notice('Transaction Service::Verifying Transaction...' . $transactionId);
46-
TransactionVerificationEventHandler::startRecording();
45+
self::startRecording();
4746
$response = $this->request(
4847
null,
4948
'GET',
5049
self::ENDPOINT . "/{$transactionId}/verify",
5150
);
52-
TransactionVerificationEventHandler::setResponseTime();
51+
self::setResponseTime();
5352

5453
return $response;
5554
}
@@ -60,13 +59,13 @@ public function verify(string $transactionId): \stdClass
6059
public function verifyWithTxref(string $tx_ref): \stdClass
6160
{
6261
$this->logger->notice('Transaction Service::Verifying Transaction...' . $tx_ref);
63-
TransactionVerificationEventHandler::startRecording();
62+
self::startRecording();
6463
$response = $this->request(
6564
null,
6665
'GET',
6766
self::ENDPOINT . '/verify_by_reference?tx_ref=' . $tx_ref,
6867
);
69-
TransactionVerificationEventHandler::setResponseTime();
68+
self::setResponseTime();
7069
return $response;
7170
}
7271

@@ -77,13 +76,13 @@ public function refund(string $trasanctionId): \stdClass
7776
{
7877
$this->checkTransactionId($trasanctionId);
7978
$this->logger->notice("Transaction Service::Refunding Transaction...{$trasanctionId}");
80-
TransactionVerificationEventHandler::startRecording();
79+
self::startRecording();
8180
$response = $this->request(
8281
null,
8382
'GET',
8483
self::ENDPOINT . "/{$trasanctionId}/refund",
8584
);
86-
TransactionVerificationEventHandler::setResponseTime();
85+
self::setResponseTime();
8786
return $response;
8887
}
8988

@@ -93,13 +92,13 @@ public function refund(string $trasanctionId): \stdClass
9392
public function getAllTransactions(): \stdClass
9493
{
9594
$this->logger->notice('Transaction Service::Retrieving all Transaction for Merchant');
96-
TransactionVerificationEventHandler::startRecording();
95+
self::startRecording();
9796
$response = $this->request(
9897
null,
9998
'GET',
10099
self::ENDPOINT,
101100
);
102-
TransactionVerificationEventHandler::setResponseTime();
101+
self::setResponseTime();
103102
return $response;
104103
}
105104

@@ -110,13 +109,13 @@ public function getRefundInfo(string $trasanctionId): \stdClass
110109
{
111110
$this->checkTransactionId($trasanctionId);
112111
$this->logger->notice("Transaction Service::Retrieving refund:Transactionid => {$trasanctionId}");
113-
TransactionVerificationEventHandler::startRecording();
112+
self::startRecording();
114113
$response = $this->request(
115114
null,
116115
'GET',
117116
"refunds/{$trasanctionId}",
118117
);
119-
TransactionVerificationEventHandler::setResponseTime();
118+
self::setResponseTime();
120119
return $response;
121120
}
122121

@@ -151,13 +150,13 @@ public function getTransactionFee(
151150

152151
$logData = json_encode($data);
153152
$this->logger->notice("Transaction Service::Retrieving Transaction Fee: Util => {$logData}");
154-
TransactionVerificationEventHandler::startRecording();
153+
self::startRecording();
155154
$response = $this->request(
156155
null,
157156
'GET',
158157
self::ENDPOINT . "/fee?{$query}",
159158
);
160-
TransactionVerificationEventHandler::setResponseTime();
159+
self::setResponseTime();
161160
return $response;
162161
}
163162

@@ -168,13 +167,13 @@ public function resendFailedHooks(string $transactionId): \stdClass
168167
{
169168
$this->checkTransactionId($transactionId);
170169
$this->logger->notice("Transaction Service::Resending Transaction Webhook: TransactionId => {$transactionId}");
171-
TransactionVerificationEventHandler::startRecording();
170+
self::startRecording();
172171
$response = $this->request(
173172
null,
174-
'GET',
173+
'POST',
175174
self::ENDPOINT . "/{$transactionId}/resend-hook",
176175
);
177-
TransactionVerificationEventHandler::setResponseTime();
176+
self::setResponseTime();
178177
return $response;
179178
}
180179

@@ -187,13 +186,13 @@ public function retrieveTimeline(string $transactionId): \stdClass
187186
$this->logger->notice(
188187
"Transaction Service::Retrieving Transaction Timeline: TransactionId => {$transactionId}"
189188
);
190-
TransactionVerificationEventHandler::startRecording();
189+
self::startRecording();
191190
$response = $this->request(
192191
null,
193192
'GET',
194-
self::ENDPOINT . "/{$transactionId}/timeline",
193+
self::ENDPOINT . "/{$transactionId}/events",
195194
);
196-
TransactionVerificationEventHandler::setResponseTime();
195+
self::setResponseTime();
197196
return $response;
198197
}
199198

tests/Unit/Service/TransactionTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,65 @@
22

33
namespace Unit\Service;
44

5+
use Flutterwave\Service\Transactions;
56
use PHPUnit\Framework\TestCase;
67

78
class TransactionTest extends TestCase
89
{
10+
public Transactions $service;
911

12+
protected function setUp(): void
13+
{
14+
$this->service = new Transactions();
15+
}
16+
17+
/**
18+
* @depends Unit\Service\MomoTest::testInitiateTanzaniaRedirect
19+
*/
20+
public function testVerifyingTransaction(string $tx_ref)
21+
{
22+
$result = $this->service->verifyWithTxref($tx_ref);
23+
$data = $result->data;
24+
$this->assertSame($data->customer->email, "[email protected]");
25+
return [ "id" => $data->id, "amount" => $data->amount, "currency" => $data->currency ];
26+
}
27+
28+
/**
29+
* @depends testVerifyingTransaction
30+
*/
31+
public function testVerifyingTransactionWithId(array $data)
32+
{
33+
$tx_id = $data['id'];
34+
35+
$result = $this->service->verify($tx_id);
36+
$data = $result->data;
37+
$this->assertSame($data->customer->email, "[email protected]");
38+
}
39+
40+
/**
41+
* @depends testVerifyingTransaction
42+
*/
43+
public function testResendingFailedHooks( array $data )
44+
{
45+
sleep(6);
46+
$tx_id = $data['id'];
47+
$result = $this->service->resendFailedHooks($tx_id);
48+
$this->assertTrue( $result->status === "success" && $result->data === "hook sent");
49+
}
50+
51+
/**
52+
* @depends testVerifyingTransaction
53+
*/
54+
public function testRetrievingTimeline( array $data )
55+
{
56+
$tx_id = $data['id'];
57+
$result = $this->service->retrieveTimeline($tx_id);
58+
$this->assertTrue( $result->status === "success" && $result->message === "Transaction events fetched");
59+
}
60+
61+
// public function testValidateCharge( string $flw_ref )
62+
// {
63+
// $result = $this->service->validate("3310", $flw_ref);
64+
// dd($result);
65+
// }
1066
}

0 commit comments

Comments
 (0)