Unify domain names in README
[stouyapi.git] / www / api / v1 / games / purchase.php
1 <?php
2 if (!isset($_POST['blob'])) {
3     echo "blob key missing in POST data\n";
4     exit(1);
5 }
6 $innerJson = base64_decode($_POST['blob']);
7
8 $inner = json_decode($innerJson);
9 if ($inner === null) {
10     echo "Error decoding inner JSON data\n";
11     exit(1);
12 }
13 $buyRequest = json_decode(base64_decode($inner->blob));
14 if ($buyRequest === null) {
15     echo "Error decoding encrypted inner JSON data\n";
16     exit(1);
17 }
18 if (!isset($buyRequest->uuid)) {
19     echo "uuid key missing in JSON data\n";
20     exit(1);
21 }
22 if (!isset($buyRequest->identifier)) {
23     echo "identifier key missing in JSON data\n";
24     exit(1);
25 }
26
27 ini_set('html_errors', false);
28
29 $productFiles = glob(
30     __DIR__ . '/../developers/*/products/'
31     . $buyRequest->identifier . '.json'
32 );
33 if (!count($productFiles)) {
34     echo "Cannot find product file for product identifier\n";
35     exit(1);
36 }
37 $product = json_decode(file_get_contents($productFiles[0]))->products[0];
38 if ($product === null) {
39     echo "could not find product in purchases file\n";
40     exit(1);
41 }
42
43 $payload = $product;
44 $payload->uuid = $buyRequest->uuid;
45
46 //"god of blades" and "pinball arcade" want double-encrypted responses
47 // muffin knights works with single encryption
48 $enc1 = [
49     'key'  => base64_encode('0123456789abcdef'),
50     'iv'   => 't3jir1LHpICunvhlM76edQ==',//random bytes
51     'blob' => base64_encode(
52         json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
53     ),
54 ];
55
56 $enc2 = [
57     'key'  => base64_encode('0123456789abcdef'),
58     'iv'   => 't3jir1LHpICunvhlM76edQ==',//random bytes
59     'blob' => base64_encode(
60         json_encode($enc1, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
61     ),
62 ];
63 echo json_encode($enc2, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";
64 ?>