Billing.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/dgrijalva/jwt-go"
  7. "github.com/labstack/echo"
  8. "io/ioutil"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type billing struct {
  15. }
  16. func (b billing) list(c echo.Context) error {
  17. type InvoiceLists struct {
  18. Embedded struct {
  19. IaaSInvoices []struct {
  20. CreatedAt time.Time `json:"createdAt"`
  21. UpdatedAt time.Time `json:"updatedAt"`
  22. UUID string `json:"uuid"`
  23. CustomerID string `json:"customerId"`
  24. Plan string `json:"plan"`
  25. DurationDay int `json:"durationDay"`
  26. VcoreQuantity int `json:"vcoreQuantity"`
  27. VcoreCost float64 `json:"vcoreCost"`
  28. RAMQuantity int `json:"ramQuantity"`
  29. RAMCost float64 `json:"ramCost"`
  30. StorageQuantity int `json:"storageQuantity"`
  31. StorageCost float64 `json:"storageCost"`
  32. ExtraIPCount int `json:"extraIpCount"`
  33. ExtraIPCost float64 `json:"extraIpCost"`
  34. ExtraBwQuantity int `json:"extraBwQuantity"`
  35. ExtraBwCost float64 `json:"extraBwCost"`
  36. Sum float64 `json:"sum"`
  37. Links struct {
  38. Self struct {
  39. Href string `json:"href"`
  40. } `json:"self"`
  41. IaaSInvoice struct {
  42. Href string `json:"href"`
  43. } `json:"iaaSInvoice"`
  44. } `json:"_links"`
  45. } `json:"iaaSInvoices"`
  46. } `json:"_embedded"`
  47. Links struct {
  48. Self struct {
  49. Href string `json:"href"`
  50. } `json:"self"`
  51. } `json:"_links"`
  52. }
  53. type InvoiceListsResponse struct {
  54. Data []struct {
  55. SUM float64 `json:"sum"`
  56. PaidState bool `json:"paidstate"`
  57. DueDate time.Time `json:"duedate"`
  58. RemainedDays int64 `json:"remaineddays"`
  59. InvoiceUUID string `json:"invoiceUUID"`
  60. } `json:"data"`
  61. Message string `json:"message"`
  62. Origin string `json:"origin"`
  63. Code int `json:"code"`
  64. }
  65. user := c.Get("user").(*jwt.Token)
  66. claims := user.Claims.(jwt.MapClaims)
  67. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  68. var hashChannel_ = make(chan []byte, 1)
  69. hashChannel_ <- _sha256[:]
  70. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  71. _BA := strings.Split(token, ";")
  72. BA := _BA[len(_BA)-2]
  73. UserUUID := login(BA).AuthenticatedUser.ID
  74. url := "http://172.20.15.24/iaaSInvoices/search/findByCustomerIdEquals?customerId=" + UserUUID
  75. method := "GET"
  76. client := &http.Client{
  77. }
  78. req, err := http.NewRequest(method, url, nil)
  79. if err != nil {
  80. fmt.Println(err)
  81. return nil
  82. }
  83. res, err := client.Do(req)
  84. if err != nil {
  85. fmt.Println(err)
  86. return nil
  87. }
  88. defer res.Body.Close()
  89. body, err := ioutil.ReadAll(res.Body)
  90. if err != nil {
  91. fmt.Println(err)
  92. return nil
  93. }
  94. //fmt.Println(string(body))
  95. _InvoiceList := InvoiceLists{}
  96. err = json.Unmarshal(body, &_InvoiceList)
  97. if err != nil {
  98. fmt.Println(err)
  99. //return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
  100. }
  101. //fmt.Println("Length: ", len(_InvoiceList.Embedded.IaaSInvoices))
  102. _InvoiceListsResponse := InvoiceListsResponse{
  103. Data: nil,
  104. Message: "Done",
  105. Origin: "Billing-listInvoices",
  106. Code: 1000,
  107. }
  108. _Data := _InvoiceListsResponse.Data
  109. x := struct {
  110. SUM float64 `json:"sum"`
  111. PaidState bool `json:"paidstate"`
  112. DueDate time.Time `json:"duedate"`
  113. RemainedDays int64 `json:"remaineddays"`
  114. InvoiceUUID string `json:"invoiceUUID"`
  115. }{}
  116. y := x
  117. for _, i := range _InvoiceList.Embedded.IaaSInvoices {
  118. y.SUM = i.Sum
  119. y.InvoiceUUID = i.UUID
  120. y.PaidState = false
  121. y.DueDate = i.CreatedAt
  122. y.RemainedDays = (i.CreatedAt.Unix() - time.Now().Unix()) / 3600 / 24
  123. _Data = append(_Data, y)
  124. }
  125. //fmt.Println("length of data: ",len(_Data))
  126. _InvoiceListsResponse.Data = _Data
  127. return c.JSON(http.StatusOK, _InvoiceListsResponse)
  128. }
  129. func (b billing) Show(c echo.Context) error {
  130. type Invoice struct {
  131. CreatedAt time.Time `json:"createdAt"`
  132. UpdatedAt time.Time `json:"updatedAt"`
  133. ID int `json:"id"`
  134. UUID string `json:"uuid"`
  135. CustomerID string `json:"customerId"`
  136. Plan string `json:"plan"`
  137. DurationDay int `json:"durationDay"`
  138. VcoreQuantity int `json:"vcoreQuantity"`
  139. VcoreCost float64 `json:"vcoreCost"`
  140. RAMQuantity int `json:"ramQuantity"`
  141. RAMCost float64 `json:"ramCost"`
  142. StorageQuantity int `json:"storageQuantity"`
  143. StorageCost float64 `json:"storageCost"`
  144. ExtraIPCount int `json:"extraIpCount"`
  145. ExtraIPCost float64 `json:"extraIpCost"`
  146. ExtraBwQuantity int `json:"extraBwQuantity"`
  147. ExtraBwCost float64 `json:"extraBwCost"`
  148. Sum float64 `json:"sum"`
  149. }
  150. type InvoiceResponse struct {
  151. Data struct {
  152. CreatedAt time.Time `json:"createdAt"`
  153. UpdatedAt time.Time `json:"updatedAt"`
  154. ID int `json:"id"`
  155. UUID string `json:"uuid"`
  156. CustomerID string `json:"customerId"`
  157. Plan string `json:"plan"`
  158. DurationDay int `json:"durationDay"`
  159. VcoreQuantity int `json:"vcoreQuantity"`
  160. VcoreCost float64 `json:"vcoreCost"`
  161. RAMQuantity int `json:"ramQuantity"`
  162. RAMCost float64 `json:"ramCost"`
  163. StorageQuantity int `json:"storageQuantity"`
  164. StorageCost float64 `json:"storageCost"`
  165. ExtraIPCount int `json:"extraIpCount"`
  166. ExtraIPCost float64 `json:"extraIpCost"`
  167. ExtraBwQuantity int `json:"extraBwQuantity"`
  168. ExtraBwCost float64 `json:"extraBwCost"`
  169. Sum float64 `json:"sum"`
  170. } `json:"data"`
  171. Message string `json:"message"`
  172. Origin string `json:"origin"`
  173. Code int `json:"code"`
  174. }
  175. user := c.Get("user").(*jwt.Token)
  176. claims := user.Claims.(jwt.MapClaims)
  177. _sha256 := sha256.Sum256([]byte(string(claims["name"].(string))))
  178. var hashChannel_ = make(chan []byte, 1)
  179. hashChannel_ <- _sha256[:]
  180. token := decrypt(<-hashChannel_, claims["IPAToken"].(string))
  181. _BA := strings.Split(token, ";")
  182. BA := _BA[len(_BA)-2]
  183. UserUUID := login(BA).AuthenticatedUser.ID
  184. InvoiceUUID := c.FormValue("InvoiceUUID")
  185. url := "http://172.20.15.24/invoice/iaas/get?uuid=" + InvoiceUUID
  186. method := "GET"
  187. client := &http.Client{
  188. }
  189. req, err := http.NewRequest(method, url, nil)
  190. if err != nil {
  191. fmt.Println(err)
  192. return nil
  193. }
  194. res, err := client.Do(req)
  195. if err != nil {
  196. fmt.Println(err)
  197. return nil
  198. }
  199. defer res.Body.Close()
  200. body, err := ioutil.ReadAll(res.Body)
  201. if err != nil {
  202. fmt.Println(err)
  203. return nil
  204. }
  205. _Invoice := Invoice{}
  206. err = json.Unmarshal(body, &_Invoice)
  207. if err != nil {
  208. fmt.Println(err)
  209. //return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
  210. }
  211. if _Invoice.CustomerID != UserUUID {
  212. resp := _response{
  213. Origin: "Billing-ShowInvoice",
  214. Message: "Unauthorized Access",
  215. Code: 1001,
  216. }
  217. return c.JSON(403, resp)
  218. }
  219. _InvoiceResponse := InvoiceResponse{
  220. Data: _Invoice,
  221. Message: "Done",
  222. Origin: "Billing-ShowInvoice",
  223. Code: 1000,
  224. }
  225. return c.JSON(http.StatusOK, _InvoiceResponse)
  226. }
  227. func IaaSCreate(UserUUID string, period string, CPU string, memory string, storageVolume string, extraIP string, extraBW string) (CPUPrice float64, memPrice float64, StoragePrice float64, IPPrice float64, extraBWPrice float64, sum float64, InvoiceID string) {
  228. type CreateResponse struct {
  229. CreatedAt time.Time `json:"createdAt"`
  230. UpdatedAt time.Time `json:"updatedAt"`
  231. ID int `json:"id"`
  232. UUID string `json:"uuid"`
  233. CustomerID string `json:"customerId"`
  234. Plan string `json:"plan"`
  235. DurationDay int `json:"durationDay"`
  236. VcoreQuantity int `json:"vcoreQuantity"`
  237. VcoreCost float64 `json:"vcoreCost"`
  238. RAMQuantity int `json:"ramQuantity"`
  239. RAMCost float64 `json:"ramCost"`
  240. StorageQuantity int `json:"storageQuantity"`
  241. StorageCost float64 `json:"storageCost"`
  242. ExtraIPCount int `json:"extraIpCount"`
  243. ExtraIPCost float64 `json:"extraIpCost"`
  244. ExtraBwQuantity int `json:"extraBwQuantity"`
  245. ExtraBwCost float64 `json:"extraBwCost"`
  246. Sum float64 `json:"sum"`
  247. }
  248. url := "http://172.20.15.24:80/invoice/iaas/create"
  249. method := "POST"
  250. //_period, _ := strconv.Atoi(period)
  251. _CPU, _ := strconv.Atoi(CPU)
  252. _memory, _ := strconv.Atoi(memory)
  253. //_storageVolume := 40//strconv.Atoi(storageVolume)
  254. _extraIP, _ := strconv.Atoi(extraIP)
  255. _extraBW, _ := strconv.Atoi(extraBW)
  256. payload := strings.NewReader(fmt.Sprintf(`{
  257. "customerId": "%s",
  258. "durationDay": "%d",
  259. "vCoreCount": "%d",
  260. "ramVolume": "%d",
  261. "storageVolume": "40",
  262. "extraIPCount": "%d",
  263. "extraBW": "%d"
  264. }`, UserUUID, 1*30, _CPU, _memory/1024/1024/1024, _extraIP, _extraBW))
  265. //}`, UserUUID, _period*30, _CPU, _memory/1024/1024/1024, _storageVolume/1024/1024/1024, _extraIP, _extraBW))
  266. //fmt.Println("Mem1 :",memory," Mem2:",_memory,"Array: ",payload)
  267. client := &http.Client{
  268. }
  269. req, err := http.NewRequest(method, url, payload)
  270. if err != nil {
  271. fmt.Println(err)
  272. return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice, sum, InvoiceID
  273. }
  274. req.Header.Add("Content-Type", "application/json")
  275. res, err := client.Do(req)
  276. if err != nil {
  277. fmt.Println(err)
  278. return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice, sum, InvoiceID
  279. }
  280. defer res.Body.Close()
  281. body, err := ioutil.ReadAll(res.Body)
  282. if err != nil {
  283. fmt.Println(err)
  284. return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice, sum, InvoiceID
  285. }
  286. //fmt.Println(string(body))
  287. //return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
  288. _CreateResponse := CreateResponse{}
  289. err = json.Unmarshal(body, &_CreateResponse)
  290. if err != nil {
  291. fmt.Println(err)
  292. //return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice
  293. }
  294. CPUPrice = _CreateResponse.VcoreCost
  295. memPrice = _CreateResponse.RAMCost
  296. extraBWPrice = _CreateResponse.ExtraBwCost
  297. IPPrice = _CreateResponse.ExtraIPCost
  298. StoragePrice = _CreateResponse.StorageCost
  299. return CPUPrice, memPrice, StoragePrice, IPPrice, extraBWPrice, _CreateResponse.Sum, _CreateResponse.UUID
  300. }