1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Controllers\Payments;
7
8use App\Libraries\Payments\XsollaSignature;
9use App\Models\Store\Order;
10use Tests\TestCase;
11
12class XsollaControllerTest extends TestCase
13{
14 public function testWhenEverythingIsFine()
15 {
16 $data = $this->getPostData();
17 // fake a valid signature, we only want to test if the response is working.
18 $trollSignature = XsollaSignature::calculateSignature(json_encode($data));
19 $response = $this->json(
20 'POST',
21 route('payments.xsolla.callback'),
22 $data,
23 ['HTTP_Authorization' => "Signature {$trollSignature}"]
24 );
25
26 $response->assertStatus(200);
27 }
28
29 public function testValidationSignatureMissing()
30 {
31 $response = $this->json(
32 'POST',
33 route('payments.xsolla.callback'),
34 $this->getPostData()
35 );
36
37 $response->assertStatus(422);
38 }
39
40 public function testSignatureIsMalformed()
41 {
42 $response = $this->json(
43 'POST',
44 route('payments.xsolla.callback'),
45 $this->getPostData(),
46 ['HTTP_Authorization' => 'Sig 1234']
47 );
48
49 $response->assertStatus(422);
50 }
51
52 public function testValidationSignatureNotMatch()
53 {
54 $response = $this->json(
55 'POST',
56 route('payments.xsolla.callback'),
57 $this->getPostData(),
58 ['HTTP_Authorization' => 'Signature 9999000011112222333344445555666677778888']
59 );
60
61 $response->assertStatus(422);
62 }
63
64 protected function setUp(): void
65 {
66 parent::setUp();
67 config_set('payments.xsolla.secret_key', 'magic');
68 $this->order = Order::factory()->checkout()->create();
69 }
70
71 private function getPostData(array $overrides = [])
72 {
73 $order = $this->order;
74 $user = $order->user;
75
76 $base = [
77 'notification_type' => 'payment',
78 'nothing' => 'to see',
79 'transaction' => [
80 'id' => '12344523',
81 'external_id' => $order->getOrderNumber(),
82 ],
83 'purchase' => [
84 'checkout' => [
85 'currency' => 'USD',
86 'amount' => $order->getTotal(),
87 ],
88 ],
89 ];
90
91 return array_merge($base, $overrides);
92 }
93}