run.php

PHP SDK · 5.6 KB · 123 lines

1<?php
2
3// php tests/run.php — starts sdks/testing/mock_server.py (Python 3 on PATH) and runs the SDK against it.
4
5declare(strict_types=1);
6
7require __DIR__ . '/../src/HBankException.php';
8require __DIR__ . '/../src/HBankClient.php';
9require __DIR__ . '/../src/Callback.php';
10
11use HBank\Callback;
12use HBank\HBankClient;
13use HBank\HBankException;
14
15$port = random_int(8400, 8499);
16$base = "http://127.0.0.1:{$port}";
17$python = PHP_OS_FAMILY === 'Windows' ? 'python' : 'python3';
18$proc = proc_open([$python, __DIR__ . '/../../testing/mock_server.py', '--port', (string) $port], [1 => ['pipe', 'w']], $pipes);
19fgets($pipes[1]);
20
21$failures = 0;
22function test(string $name, callable $body): void
23{
24 global $failures;
25 try {
26 $body();
27 echo "ok {$name}\n";
28 } catch (Throwable $e) {
29 $failures++;
30 echo "FAIL {$name}: {$e}\n";
31 }
32}
33function eq(mixed $expected, mixed $actual): void
34{
35 if ($expected != $actual) {
36 throw new RuntimeException('expected ' . var_export($expected, true) . ', got ' . var_export($actual, true));
37 }
38}
39function fails(callable $body): HBankException
40{
41 try {
42 $body();
43 } catch (HBankException $e) {
44 return $e;
45 }
46 throw new RuntimeException('expected an HBankException');
47}
48$pay = fn (string $tid) => file_get_contents("{$base}/_mock/pay/{$tid}", false, stream_context_create(['http' => ['method' => 'POST']]));
49
50$bank = new HBankClient('test_key', $base);
51
52test('payment, QR code, refund', function () use ($bank, $base, $pay) {
53 $tid = $bank->createPayment(24.9, 'EUR', 'Order 1042', 'https://shop.example/cb');
54 eq("{$base}/api/payment?tid={$tid}", $bank->checkoutUrl($tid));
55 eq(true, str_ends_with($bank->createPaymentCode($tid)['qr_svg'], '/qr.svg'));
56 eq('pending', $bank->getPayment($tid)['status']);
57 eq(24.9, $bank->getPayment($tid)['amount']);
58 $pay($tid);
59 $r = $bank->refund($tid, '10', 'Returned one item');
60 eq(14.9, $r['transaction']['refundable']);
61 eq('amount_too_high', fails(fn () => $bank->refund($tid, 100))->errorCode);
62});
63
64test('errors', function () use ($bank, $base) {
65 $e = fails(fn () => $bank->createPayment(0, 'EUR'));
66 eq(['invalid_amount', 400], [$e->errorCode, $e->status]);
67 eq('transaction_not_found', fails(fn () => $bank->getPayment('tx_NoSuchPayment0000000000000'))->errorCode);
68 eq(true, fails(fn () => (new HBankClient('nope', $base))->listSubscriptions())->isAuthentication());
69 eq(true, fails(fn () => (new HBankClient('k', 'http://127.0.0.1:1', 3))->rate('EUR', 'USD'))->isConnection());
70});
71
72test('subscriptions', function () use ($bank, $pay) {
73 $s = $bank->createSubscription('9.99', 'EUR', 'month', 1, 'Pro plan');
74 eq('offered', $s['status']);
75 $c = $bank->checkoutSubscription($s['id']);
76 $pay($c['transaction_id']);
77 eq('active', $bank->getSubscription($s['id'])['status']);
78 eq('merchant', $bank->cancelSubscription($s['id'])['canceled_by']);
79 eq('already_canceled', fails(fn () => $bank->cancelSubscription($s['id']))->errorCode);
80 eq(2, $bank->createSubscriptionCheckout(5, 'EUR', 'week', 2)['subscription']['interval_count']);
81});
82
83test('several subscriptions in one checkout', function () use ($bank, $pay) {
84 $storage = $bank->createSubscription(5, 'EUR', 'month', 1, 'Storage');
85 $c = $bank->createSubscriptionsCheckout([$storage['id'], ['amount' => '2.50', 'currency' => 'EUR', 'interval' => 'week', 'description' => 'Support']],
86 callbackUrl: 'https://shop.example/cb');
87 eq(['Storage', 'Support'], array_column($c['subscriptions'], 'description'));
88 eq($storage['id'], $c['subscriptions'][0]['id']);
89 $pay($c['transaction_id']);
90 $payment = $bank->getPayment($c['transaction_id']);
91 eq(7.5, $payment['amount']);
92 eq(array_column($c['subscriptions'], 'id'), $payment['subscription_ids']);
93 foreach ($c['subscriptions'] as $x) eq('active', $bank->getSubscription($x['id'])['status']);
94 eq('not_open', fails(fn () => $bank->createSubscriptionsCheckout([$storage['id']]))->errorCode);
95 eq('mixed_currencies', fails(fn () => $bank->createSubscriptionsCheckout([
96 ['amount' => 1, 'currency' => 'EUR', 'interval' => 'month'], ['amount' => 1, 'currency' => 'USD', 'interval' => 'month']]))->errorCode);
97});
98
99test('currencies', function () use ($bank) {
100 eq(108.37, $bank->convert(100, 'EUR', 'USD'));
101 eq(1.0837, $bank->rate('EUR', 'USD')['to_rate']);
102});
103
104test('callbacks', function () {
105 $cb = Callback::parse('{"id": "tx_8fK2mQ7xRb4Tn9WcYz3LpH6v", "amount": 24.90, "currency": "EUR", "status": "completed", "description": "Order 1042"}');
106 eq(['tx_8fK2mQ7xRb4Tn9WcYz3LpH6v', '24.90'], [$cb->id, $cb->amount]);
107 eq(['resume' => false, 'error' => 'Out of stock'], Callback::refuse('Out of stock'));
108 eq('invalid_callback', fails(fn () => Callback::parse('{}'))->errorCode);
109});
110
111test('callback signature', function () {
112 $body = '{"id":7310,"amount":24.9,"currency":"EUR","status":"awaiting_approval","description":"Order 1042"}';
113 $header = 't=1700000000,v1=fcfc30b9667c3a8e8aea68178c36716153e075f79c61521bf3586088d9877281';
114 eq(true, Callback::verifySignature('hbcb_test', $header, $body, 300, 1700000100));
115 eq(false, Callback::verifySignature('hbcb_other', $header, $body, 300, 1700000100));
116 eq(false, Callback::verifySignature('hbcb_test', $header, '{}', 300, 1700000100));
117 eq(false, Callback::verifySignature('hbcb_test', $header, $body, 300, 1700000400));
118 eq(false, Callback::verifySignature('hbcb_test', 'garbage', $body, 300, 1700000100));
119});
120
121proc_terminate($proc);
122echo $failures === 0 ? "All passed.\n" : "{$failures} failed.\n";
123exit($failures === 0 ? 0 : 1);