@@ -121,7 +121,8 @@ func (h *Handler) invoiceCreate(c *gin.Context) {
121
121
}
122
122
123
123
// POST /invoice/info
124
- func (h * Handler ) info (c * gin.Context ) {
124
+ func (h * Handler ) invoiceInfo (c * gin.Context ) {
125
+ // TODO: validation
125
126
var data struct {
126
127
InvoiceId string `json:"invoice_id"`
127
128
ApiKey string `json:"api_key"`
@@ -183,7 +184,7 @@ func (h *Handler) info(c *gin.Context) {
183
184
CreatedAt : invoice .CreatedAt .Format ("2006-01-02 15:04:05" ),
184
185
}
185
186
186
- if time .Now ().Unix () > invoice .EndTimestamp && invoice .Status .IsNotPaid () {
187
+ if time .Now ().Unix () > invoice .EndTimestamp && invoice .Status .IsNotPaid () && ! invoice . Status . IsCancelled () {
187
188
response .Status = "end"
188
189
}
189
190
@@ -228,8 +229,66 @@ func (h *Handler) qrCode(c *gin.Context) {
228
229
c .Data (http .StatusOK , "image/png" , []byte (qrCode ))
229
230
}
230
231
232
+ func (h * Handler ) invoiceCancel (c * gin.Context ) {
233
+ var data struct {
234
+ InvoiceId string `json:"invoice_id"`
235
+ ApiKey string `json:"api_key"`
236
+ }
237
+
238
+ var errid = logger .GenErrorId ()
239
+
240
+ if err := c .ShouldBindJSON (& data ); err != nil {
241
+ responseErr (c , http .StatusBadRequest , domain .ErrMsgBadRequest , "" )
242
+ fmt .Println ("unmarshal error: " + err .Error ())
243
+ return
244
+ }
245
+
246
+ if data .InvoiceId == "" {
247
+ responseErr (c , http .StatusBadRequest , fmt .Sprintf (domain .ErrMsgParamsBadRequest , domain .ErrParamEmptyInvoiceId ), "" )
248
+ return
249
+ }
250
+
251
+ if data .ApiKey == "" {
252
+ responseErr (c , http .StatusBadRequest , domain .ErrMsgApiKeyInvalid , "" )
253
+ return
254
+ }
255
+
256
+ exists , err := h .services .Merchants .ApiKeyExists (h .db , data .ApiKey )
257
+ if err != nil {
258
+ responseErr (c , http .StatusInternalServerError , domain .ErrMsgInternalServerError , errid )
259
+ h .log .TemplInvoiceErr ("api key exists error: " + err .Error (), errid , data .InvoiceId , decimal .Zero , logger .NA , c .Request .RequestURI , logger .NA , c .ClientIP ())
260
+ return
261
+ }
262
+
263
+ if ! exists {
264
+ responseErr (c , http .StatusBadRequest , domain .ErrMsgApiKeyNotFound , "" )
265
+ return
266
+ }
267
+
268
+ if err = h .services .Invoices .Cancel (data .InvoiceId ); err != nil {
269
+ var _errid = ""
270
+ var errmsg = err .Error ()
271
+
272
+ code := domain .GetStatusByErr (err )
273
+ if code == http .StatusInternalServerError {
274
+ _errid = errid
275
+ errmsg = domain .ErrMsgInternalServerError
276
+ h .log .TemplInvoiceErr ("invoice cancel error: " + err .Error (), errid , data .InvoiceId , decimal .Zero , logger .NA , c .Request .RequestURI , logger .NA , c .ClientIP ())
277
+ }
278
+ responseErr (c , code , errmsg , _errid )
279
+ return
280
+ }
281
+
282
+ c .AbortWithStatusJSON (http .StatusOK , responseInvoiceCancelled {
283
+ Error : false ,
284
+ })
285
+
286
+ }
287
+
231
288
func (h * Handler ) initPubInvoiceRoutes (g * gin.RouterGroup ) {
232
289
g .POST ("/invoice/create" , h .invoiceCreate )
233
- g .POST ("/invoice/info" , h .info )
290
+ g .POST ("/invoice/info" , h .invoiceInfo )
291
+ g .POST ("/invoice/cancel" , h .invoiceCancel )
292
+
234
293
g .GET ("/invoice/qr-code/:invoice_id" , h .qrCode )
235
294
}
0 commit comments