Advertisement
moonion_nashivan

Untitled

Sep 13th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. const socket = new WebSocket("wss://api-test.clictechnology.com/");
  2. let id = 1;
  3.  
  4.  
  5. socket.onopen = function() {
  6. start();
  7. };
  8.  
  9. socket.onclose = function(event) {
  10. document.write(`code: ${event.code} reason: ${event.reason} `);
  11. };
  12.  
  13. socket.onmessage = function(event) {
  14. document.write(`response ${event.data} <br>`);
  15.  
  16. const data = JSON.parse(event.data)
  17.  
  18. // need to keep in touch with server
  19. if(data.type && data.type === "hello") {
  20. setInterval(function () {
  21. send({"type":"ping"})
  22. }, data.heartbeat.interval)
  23. }
  24.  
  25. };
  26.  
  27. socket.onerror = function(error) {
  28. document.write(`Error ${error.message} <br>`);
  29. };
  30.  
  31.  
  32. function send(object){
  33. socket.send(JSON.stringify(Object.assign({},object,{id: id++})))
  34. }
  35.  
  36. async function start(){
  37. document.write("start <br>");
  38.  
  39. // send auth
  40. send({"type":"hello","version":"2","auth":{"headers":{"authorization":"Bearer def9b9db-ea9f-5eb1-920d-48f380a6a641"}}});
  41.  
  42. try {
  43. transaction = await fetch("https://api-test.clictechnology.com/v1/transaction/instant", {
  44. method: "post",
  45. headers: {
  46. "Authorization": "Bearer def9b9db-ea9f-5eb1-920d-48f380a6a641",
  47. "Content-Type": "application/json"
  48. },
  49. body: JSON.stringify({
  50. "amount": 2,
  51. "email": "lllypuk.ua@gmail.com",
  52. "coin_type": "eth",
  53. "orderId": "123",
  54. "customData": {},
  55. "currency": "usd",
  56. "forMobile": true
  57.  
  58. })
  59. }).then(response=>response.json())
  60. } catch(e){
  61. document.write("error");
  62. document.write(e);
  63. }
  64.  
  65. document.write(`transaction id: ${transaction.id} token: ${transaction.token} amount: ${transaction.amount} link: ${transaction.link} <br>`);
  66.  
  67. // subscribe event
  68. send({"type":"sub","path":`/v1/transaction/subscribe/instant/status/${transaction.token}`})
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement