-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.php
217 lines (202 loc) · 7.99 KB
/
app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
require __DIR__ . '/vendor/autoload.php';
use ExinOne\MixinSDK\Traits\MixinSDKTrait;
use ExinOne\MixinSDK\MixinSDK;
use Ramsey\Uuid\Uuid;
use Ratchet\RFC6455\Messaging\Frame;
$loop = \React\EventLoop\Factory::create();
$reactConnector = new \React\Socket\Connector($loop, [
'timeout' => 15
]);
class callTraitClass {
use MixinSDKTrait;
public $config;
public function __construct()
{
$config = require(__DIR__.'/config.php');
$this->config = $config;
}
}
$callTrait = new callTraitClass();
$Token = $callTrait->getToken('GET', '/', '');
print_r($callTrait->config['client_id']);
// $Header = 'Authorization'.'Bearer '.$Token;
// print($Header);
$connector = new \Ratchet\Client\Connector($loop,$reactConnector);
// $connector('ws://127.0.0.1:9000', ['protocol' => 'Mixin-Blaze-1'], ['Origin' => 'http://localhost',
$connector('wss://blaze.mixin.one', ['protocol' => 'Mixin-Blaze-1'],[
'Authorization' => 'Bearer '.$Token
])
->then(function(Ratchet\Client\WebSocket $conn) {
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
$jsMsg = json_decode(gzdecode($msg));
print_r($jsMsg);
if ($jsMsg->action === 'CREATE_MESSAGE' and property_exists($jsMsg,'data')) {
echo "\nNeed reply server a receipt!\n";
$RspMsg = generateReceipt($jsMsg->data->message_id);
$msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY);
$conn->send($msg);
if ($jsMsg->data->category === 'PLAIN_TEXT') {
echo "PLAIN_TEXT:".base64_decode($jsMsg->data->data);
$isCmd = strtolower(base64_decode($jsMsg->data->data));
if ($isCmd ==='?' or $isCmd ==='help') {
$msgData = sendUsage($jsMsg->data->conversation_id);
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
$conn->send($msg);
} elseif ($isCmd === '1') {
// print($callTrait->config['client_id']);
$msgData = sendAppButtons($jsMsg);
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
$conn->send($msg);
}//end of pay1
elseif ($isCmd === '2') {
// print($callTrait->config['client_id']);
$msgData = sendAppCard($jsMsg);
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
$conn->send($msg);
}//end of pay2
elseif ($isCmd === '3') {
transfer();
} else {
$msgData = sendPlainText($jsMsg->data->conversation_id,
base64_decode($jsMsg->data->data));
$msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY);
$conn->send($msg);
}
} //end of PLAIN_TEXT
if ($jsMsg->data->category === 'SYSTEM_ACCOUNT_SNAPSHOT') {
// refundInstant
echo "user id:".$jsMsg->data->user_id;
$dtPay = json_decode(base64_decode($jsMsg->data->data));
print_r($dtPay);
if ($dtPay->amount > 0) {
echo "paid!".$dtPay->asset_id;
refundInstant($dtPay->asset_id,$dtPay->amount,$jsMsg->data->user_id);
}
} //end of SYSTEM_ACCOUNT_SNAPSHOT
} //end of CREATE_MESSAGE
});
$conn->on('close', function($code = null, $reason = null) {
echo "Connection closed ({$code} - {$reason})\n";
});
/* start listen for the incoming message */
$message = [
'id' => Uuid::uuid4()->toString(),
'action' => 'LIST_PENDING_MESSAGES',
];
print_r(json_encode($message));
$msg = new Frame(gzencode(json_encode($message)),true,Frame::OP_BINARY);
$conn->send($msg);
// $conn->send(gzencode($msg,1,FORCE_DEFLATE));
}, function(\Exception $e) use ($loop) {
echo "Could not connect: {$e->getMessage()}\n";
$loop->stop();
});
$loop->run();
function sendUsage($conversation_id):Array {
$msgHelp = <<<EOF
Usage:
? or help : for help!
1 : pay by APP_CARD
2 : pay by APP_BUTTON_GROUP
EOF;
return sendPlainText($conversation_id,$msgHelp);
}
function sendPlainText($conversation_id,$msgContent):Array {
$msgParams = [
'conversation_id' => $conversation_id,
'category' => 'PLAIN_TEXT',
'status' => 'SENT',
'message_id' => Uuid::uuid4()->toString(),
'data' => base64_encode($msgContent),//base64_encode("hello!"),
];
$msgPayButton = [
'id' => Uuid::uuid4()->toString(),
'action' => 'CREATE_MESSAGE',
'params' => $msgParams,
];
return $msgPayButton;
}
function sendAppButtons($jsMsg):Array {
$client_id = (require "./config.php")['client_id'];
$payLinkEOS = "https://mixin.one/pay?recipient=".
$client_id."&asset=".
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d".
"&amount=0.0001"."&trace=".Uuid::uuid4()->toString().
"&memo=";
$payLinkBTC = "https://mixin.one/pay?recipient=".
$client_id."&asset=".
"c6d0c728-2624-429b-8e0d-d9d19b6592fa".
"&amount=0.0001"."&trace=".Uuid::uuid4()->toString().
"&memo=";
$msgData = [[
'label' => "Pay 0.001 EOS",
'color' => "#FFABAB",
'action' => $payLinkEOS,
],[
'label' => "Pay 0.0001 BTC",
'color' => "#00EEFF",
'action' => $payLinkBTC,
],
];
$msgParams = [
'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id],
// 'recipient_id' => $jsMsg->data->user_id,
'category' => 'APP_BUTTON_GROUP',//'PLAIN_TEXT',
'status' => 'SENT',
'message_id' => Uuid::uuid4()->toString(),
'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"),
];
$msgPayButtons = [
'id' => Uuid::uuid4()->toString(),
'action' => 'CREATE_MESSAGE',
'params' => $msgParams,
];
return $msgPayButtons;
}
function sendAppCard($jsMsg):Array
{
$client_id = (require "./config.php")['client_id'];
$payLink = "https://mixin.one/pay?recipient=".
$client_id."&asset=".
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d".
"&amount=0.0001"."&trace=".Uuid::uuid4()->toString().
"&memo=";
$msgData = [
'icon_url' => "https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png",
'title' => "Pay 0.001 EOS",
'description' => "pay",
'action' => $payLink,
];
$msgParams = [
'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id],
// 'recipient_id' => $jsMsg->data->user_id,
'category' => 'APP_CARD',//'PLAIN_TEXT',
'status' => 'SENT',
'message_id' => Uuid::uuid4()->toString(),
'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"),
];
$msgPayButton = [
'id' => Uuid::uuid4()->toString(),
'action' => 'CREATE_MESSAGE',
'params' => $msgParams,
];
return $msgPayButton;
}
function transfer() {
$mixinSdk = new MixinSDK(require './config.php');
print_r($mixinSdk->getConfig());
}
function generateReceipt($msgID):Array {
$IncomingMsg = ["message_id" => $msgID, "status" => "READ"];
$RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT",
"params" => $IncomingMsg];
return $RspMsg;
}
function refundInstant($_assetID,$_amount,$_opponent_id) {
$mixinSdk = new MixinSDK(require './config.php');
// print_r();
$BotInfo = $mixinSdk->Wallet()->transfer($_assetID,$_opponent_id,
$mixinSdk->getConfig()['default']['pin'],$_amount);
print_r($BotInfo);
}