This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.go
133 lines (109 loc) · 3.27 KB
/
orders.go
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
package gobizApi
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
func FetchOrders(restaurantID, authorization, status string) (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.gojekapi.com/mocha/v3/orders?restaurant_id=%s&status=%s&limit=100&page=1", restaurantID, status), nil)
if err != nil {
printError(err)
return "", err
}
body, err := sendRequest(req, authorization)
if err != nil {
printError(err)
return "", err
}
printResponse(string(body))
return string(body), nil
}
func FetchNewOrders(restaurantID, authorization string) (string, error) {
return FetchOrders(restaurantID, authorization, "AWAITING_ACCEPTANCE")
}
func FetchCompletedOrders(restaurantID, authorization string) (string, error) {
return FetchOrders(restaurantID, authorization, "COMPLETED")
}
func FetchCanceledOrders(restaurantID, authorization string) (string, error) {
return FetchOrders(restaurantID, authorization, "UNFULFILLED")
}
func CancelOrder(authorization, orderID, cancelReasonCode, id, uuid string) (bool, error) {
data := map[string]interface{}{
"cancel_reason_code": cancelReasonCode,
}
if cancelReasonCode == "ITEMS_OUT_OF_STOCK" {
data["out_of_stock_items"] = []map[string]interface{}{
{
"id": id,
"uuid": uuid,
},
}
data["out_of_stock_variants"] = []interface{}{}
}
jsonData, err := json.Marshal(data)
if err != nil {
printError(err)
return false, err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("https://api.gojekapi.com/buffet/v1/orders/%s/merchant/cancelled", orderID), bytes.NewBuffer(jsonData))
if err != nil {
printError(err)
return false, err
}
body, err := sendRequest(req, authorization)
if err != nil {
printError(err)
return false, err
}
printResponse(string(body))
return true, nil
}
func CancelOrderStockEmpty(authorization, orderID, id, uuid string) (bool, error) {
return CancelOrder(authorization, orderID, "ITEMS_OUT_OF_STOCK", id, uuid)
}
func CancelOrderRestoClosed(authorization, orderID string) (bool, error) {
return CancelOrder(authorization, orderID, "RESTAURANT_CLOSED", "", "")
}
func CancelOrderHighDemand(authorization, orderID string) (bool, error) {
return CancelOrder(authorization, orderID, "HIGH_DEMAND", "", "")
}
func SetOrderPrepared(authorization, orderID string) (string, error) {
now := time.Now()
data := map[string]interface{}{
"timestamp": now,
"action_name": "FOOD_PREPARED_FOR_PICKUP",
}
jsonData, err := marshalJSON(data)
if err != nil {
printError(err)
return "", err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("https://api.gojekapi.com/buffet/v1/orders/%s/merchant/food-prepared", orderID), bytes.NewBuffer(jsonData))
if err != nil {
printError(err)
return "", err
}
body, err := sendRequest(req, authorization)
if err != nil {
printError(err)
return "", err
}
printResponse(string(body))
return string(body), nil
}
func AcceptNewOrders(authorization, orderID string) (bool, error) {
req, err := http.NewRequest("PUT", fmt.Sprintf("https://api.gojekapi.com/buffet/v1/orders/%s/merchant/accepted", orderID), nil)
if err != nil {
printError(err)
return false, err
}
body, err := sendRequest(req, authorization)
if err != nil {
printError(err)
return false, err
}
printResponse(string(body))
return true, nil
}