Billing.go 10 KB

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