Skip to content

Commit 7fc2367

Browse files
feat: auto-generate transaction IDs for account and demo account creation
1 parent d369df1 commit 7fc2367

6 files changed

Lines changed: 76 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,15 @@ $response = $replicas->createReplica(
218218
'magic' => 123456,
219219
'region' => 'london',
220220
],
221-
transactionId: bin2hex(random_bytes(16))
222221
);
223222

224223
if ($response->shouldRetry()) {
225224
echo "Replica creation accepted. Retry after: " . ($response->retryAfter() ?? 'not specified');
226225
}
227226
```
228227

228+
A transaction ID is auto-generated. Pass your own `transactionId` to retry an accepted request.
229+
229230
Available methods:
230231

231232
- `replicas(string $accountId)`
@@ -260,7 +261,6 @@ $response = $demoAccounts->createMT4DemoAccount(
260261
'serverName' => 'Example-Server',
261262
'keywords' => ['Example Broker Ltd'],
262263
],
263-
transactionId: bin2hex(random_bytes(16))
264264
);
265265

266266
if ($response->isCreated()) {
@@ -272,6 +272,16 @@ if ($response->shouldRetry()) {
272272
}
273273
```
274274

275+
A transaction ID is auto-generated for you. You can also pass your own if you need to retry an accepted request:
276+
277+
```php
278+
$response = $demoAccounts->createMT4DemoAccount(
279+
'provisioning-profile-id',
280+
[...],
281+
transactionId: 'my-custom-32-char-transaction-id'
282+
);
283+
```
284+
275285
Create a MetaTrader 5 demo account:
276286

277287
```php
@@ -287,7 +297,6 @@ $response = $demoAccounts->createMT5DemoAccount(
287297
'serverName' => 'Example-Server',
288298
'keywords' => ['Example Broker Ltd'],
289299
],
290-
transactionId: bin2hex(random_bytes(16))
291300
);
292301
```
293302

‎src/Resources/AccountManagement/Account.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function createConfigurationLink(string $accountId, ?int $ttlInDays = nul
8989

9090
private function transactionHeader(?string $transactionId): array
9191
{
92-
return $transactionId ? ['transaction-id' => $transactionId] : [];
92+
return ['transaction-id' => $transactionId ?? bin2hex(random_bytes(16))];
9393
}
9494

9595
private function apiVersionHeader(?int $apiVersion): array

‎src/Resources/AccountManagement/AccountReplica.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ public function createReplica(string $accountId, array $data, ?string $transacti
2424
return $this->http->postAction(
2525
"/users/current/accounts/{$accountId}/replicas",
2626
$data,
27-
$transactionId ? ['transaction-id' => $transactionId] : []
27+
$this->transactionHeader($transactionId)
2828
);
2929
}
3030

31+
private function transactionHeader(?string $transactionId): array
32+
{
33+
return ['transaction-id' => $transactionId ?? bin2hex(random_bytes(16))];
34+
}
35+
3136
public function updateReplica(string $accountId, string $replicaId, array $data): array|string|null
3237
{
3338
return $this->http->put("/users/current/accounts/{$accountId}/replicas/{$replicaId}", $data);

‎src/Resources/AccountManagement/DemoAccount.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,26 @@ class DemoAccount
99
{
1010
public function __construct(private readonly Http $http) {}
1111

12-
public function createMT4DemoAccount(string $profileId, array $data, string $transactionId): ActionResponse
12+
public function createMT4DemoAccount(string $profileId, array $data, ?string $transactionId = null): ActionResponse
1313
{
1414
return $this->http->postAction(
1515
"/users/current/provisioning-profiles/{$profileId}/mt4-demo-accounts",
1616
$data,
17-
['transaction-id' => $transactionId]
17+
$this->transactionHeader($transactionId)
1818
);
1919
}
2020

21-
public function createMT5DemoAccount(string $profileId, array $data, string $transactionId): ActionResponse
21+
public function createMT5DemoAccount(string $profileId, array $data, ?string $transactionId = null): ActionResponse
2222
{
2323
return $this->http->postAction(
2424
"/users/current/provisioning-profiles/{$profileId}/mt5-demo-accounts",
2525
$data,
26-
['transaction-id' => $transactionId]
26+
$this->transactionHeader($transactionId)
2727
);
2828
}
29+
30+
private function transactionHeader(?string $transactionId): array
31+
{
32+
return ['transaction-id' => $transactionId ?? bin2hex(random_bytes(16))];
33+
}
2934
}

‎tests/AccountManagementTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
expect($history[0]['request']->getHeaderLine('transaction-id'))->toBe('12345678901234567890123456789012');
3939
});
4040

41+
it('auto-generates transaction id for account create when not provided', function (): void {
42+
$history = [];
43+
$metaapi = metaApiClientWithHistory([new Response(201, [], '{}')], $history);
44+
45+
$metaapi->accounts()->create(['name' => 'Demo']);
46+
47+
$transactionId = $history[0]['request']->getHeaderLine('transaction-id');
48+
expect($transactionId)->not->toBeEmpty();
49+
expect(strlen($transactionId))->toBe(32);
50+
expect(ctype_xdigit($transactionId))->toBeTrue();
51+
});
52+
4153
it('exposes accepted account creation retry metadata', function (): void {
4254
$metaapi = metaApiClientWithHistory([
4355
new Response(202, ['Retry-After' => '10'], '{"id":"account-id","state":"DRAFT"}'),
@@ -86,6 +98,18 @@
8698
expect($history[0]['request']->getHeaderLine('transaction-id'))->toBe('transaction-id');
8799
});
88100

101+
it('auto-generates transaction id for replica create when not provided', function (): void {
102+
$history = [];
103+
$metaapi = metaApiClientWithHistory([new Response(201, [], '{}')], $history);
104+
105+
$metaapi->accountReplicas()->createReplica('account-id', ['magic' => 123456]);
106+
107+
$transactionId = $history[0]['request']->getHeaderLine('transaction-id');
108+
expect($transactionId)->not->toBeEmpty();
109+
expect(strlen($transactionId))->toBe(32);
110+
expect(ctype_xdigit($transactionId))->toBeTrue();
111+
});
112+
89113
it('exposes accepted account replica creation retry metadata', function (): void {
90114
$metaapi = metaApiClientWithHistory([
91115
new Response(202, ['Retry-After' => '15'], '{"id":"replica-id","state":"DRAFT"}'),

‎tests/DemoAccountTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@
5353
expect($history[0]['request']->getHeaderLine('transaction-id'))->toBe('transaction-id-1234567890123456');
5454
});
5555

56+
it('auto-generates transaction id for mt4 demo account when not provided', function (): void {
57+
$history = [];
58+
$metaapi = metaApiClientWithHistory([new Response(201, [], '{}')], $history);
59+
60+
$metaapi->demoAccounts()->createMT4DemoAccount('profile-id', []);
61+
62+
$transactionId = $history[0]['request']->getHeaderLine('transaction-id');
63+
expect($transactionId)->not->toBeEmpty();
64+
expect(strlen($transactionId))->toBe(32);
65+
expect(ctype_xdigit($transactionId))->toBeTrue();
66+
});
67+
68+
it('auto-generates transaction id for mt5 demo account when not provided', function (): void {
69+
$history = [];
70+
$metaapi = metaApiClientWithHistory([new Response(201, [], '{}')], $history);
71+
72+
$metaapi->demoAccounts()->createMT5DemoAccount('profile-id', []);
73+
74+
$transactionId = $history[0]['request']->getHeaderLine('transaction-id');
75+
expect($transactionId)->not->toBeEmpty();
76+
expect(strlen($transactionId))->toBe(32);
77+
expect(ctype_xdigit($transactionId))->toBeTrue();
78+
});
79+
5680
it('exposes accepted demo account creation retry metadata', function (): void {
5781
$metaapi = metaApiClientWithHistory([
5882
new Response(202, ['Retry-After' => '10'], '{"id":"request-id","state":"DRAFT"}'),

0 commit comments

Comments
 (0)