-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment_request.go
More file actions
30 lines (27 loc) · 938 Bytes
/
payment_request.go
File metadata and controls
30 lines (27 loc) · 938 Bytes
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
package mppx
import (
"encoding/base64"
"encoding/json"
"fmt"
)
// SerializePaymentRequest serializes a payment request map to a base64url-encoded canonical JSON string.
// Go's encoding/json sorts map keys, producing deterministic output suitable for HMAC computation.
func SerializePaymentRequest(req map[string]any) string {
data, err := json.Marshal(req)
if err != nil {
return ""
}
return base64.RawURLEncoding.EncodeToString(data)
}
// DeserializePaymentRequest decodes a base64url-encoded payment request string into a map.
func DeserializePaymentRequest(encoded string) (map[string]any, error) {
data, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil {
return nil, fmt.Errorf("mpp: invalid base64url in request: %w", err)
}
var result map[string]any
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("mpp: invalid JSON in request: %w", err)
}
return result, nil
}