forked from tulir/whatsmeow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbusiness.go
More file actions
123 lines (107 loc) · 3.42 KB
/
Copy pathbusiness.go
File metadata and controls
123 lines (107 loc) · 3.42 KB
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
// Copyright (c) 2025 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package whatsmeow
import (
"context"
"fmt"
"strconv"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/types"
)
// GetOrderDetails fetches the details of a specific order using its ID and token.
// Both token and orderID are found in the OrderMessage.
func (cli *Client) GetOrderDetails(ctx context.Context, orderID, tokenBase64 string) (*types.OrderDetails, error) {
resp, err := cli.sendIQ(ctx, infoQuery{
Namespace: "fb:thrift_iq",
Type: iqGet,
SMaxID: "5",
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "order",
Attrs: waBinary.Attrs{
"op": "get",
"id": orderID,
},
Content: []waBinary.Node{
{
Tag: "image_dimensions",
Content: []waBinary.Node{
{Tag: "width", Content: []byte("100")},
{Tag: "height", Content: []byte("100")},
},
},
{Tag: "token", Content: []byte(tokenBase64)},
},
}},
})
if err != nil {
return nil, fmt.Errorf("failed to send order IQ: %w", err)
}
orderNode, ok := resp.GetOptionalChildByTag("order")
if !ok {
return nil, &ElementMissingError{Tag: "order", In: "response to order query"}
}
return parseOrderDetailsNode(orderNode)
}
// Helper to get the string content of a child node.
func getStringChild(node waBinary.Node, tag string) string {
child, ok := node.GetOptionalChildByTag(tag)
if !ok {
return ""
}
content, _ := child.Content.([]byte)
return string(content)
}
func parseOrderDetailsNode(orderNode waBinary.Node) (*types.OrderDetails, error) {
ag := orderNode.AttrGetter()
details := &types.OrderDetails{
ID: ag.String("id"),
CreatedAt: ag.UnixTime("creation_ts"),
}
if err := ag.Error(); err != nil {
return nil, err
}
// Parse Price
priceNode, ok := orderNode.GetOptionalChildByTag("price")
if ok {
subtotal, _ := strconv.ParseInt(getStringChild(priceNode, "subtotal"), 10, 64)
total, _ := strconv.ParseInt(getStringChild(priceNode, "total"), 10, 64)
details.Price = types.OrderPrice{
Subtotal: subtotal,
Total: total,
Currency: getStringChild(priceNode, "currency"),
PriceStatus: getStringChild(priceNode, "price_status"),
}
}
// Parse Catalog ID
catalogNode, ok := orderNode.GetOptionalChildByTag("catalog")
if ok {
details.CatalogID = getStringChild(catalogNode, "id")
}
// Parse Products
for _, productNode := range orderNode.GetChildrenByTag("product") {
price, _ := strconv.ParseInt(getStringChild(productNode, "price"), 10, 64)
quantity, _ := strconv.Atoi(getStringChild(productNode, "quantity"))
product := types.OrderProduct{
ID: getStringChild(productNode, "id"),
Price: price,
Currency: getStringChild(productNode, "currency"),
Name: getStringChild(productNode, "name"),
Quantity: quantity,
}
// Parse Product Image
if imageNode, ok := productNode.GetOptionalChildByTag("image"); ok {
product.ImageID = getStringChild(imageNode, "id")
product.ImageURL = getStringChild(imageNode, "url")
}
// Parse Variant Info
if variantNode, ok := productNode.GetOptionalChildByTag("variant_info"); ok {
product.VariantInfo.Properties = getStringChild(variantNode, "properties")
}
details.Products = append(details.Products, product)
}
return details, nil
}