-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.go
118 lines (107 loc) · 4.25 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
package mollie
import (
"encoding/json"
"fmt"
"time"
)
// https://docs.mollie.com/reference/v2/orders-api/create-order
type CreateOrderParameters struct {
Amount PaymentAmount `json:"amount"`
OrderNumber string `json:"orderNumber"`
Lines []*OrderLine `json:"lines"`
BillingAddress *Address `json:"billingAddress"`
ShippingAddress *Address `json:"shippingAddress,omitempty"`
RedirectURL string `json:"redirectUrl"`
WebhookURL string `json:"webhookUrl,omitempty"`
CancelURL string `json:"cancelUrl,omitempty"`
Locale Locale `json:"locale"`
Method string `json:"method,omitempty"`
Payment map[string]string `json:"payment,omitempty"`
}
type OrderLine struct {
Type ProductType `json:"type,omitempty"`
Name string `json:"name"`
Quantity int `json:"quantity"`
UnitPrice PaymentAmount `json:"unitPrice"`
DiscountAmount *PaymentAmount `json:"discountAmount,omitempty"`
TotalAmount PaymentAmount `json:"totalAmount"`
VATRate string `json:"vatRate"`
VATAmount PaymentAmount `json:"vatAmount"`
}
type ProductType string
type PaymentStatus string
const (
ProductTypePhysical ProductType = "physical"
ProductTypeDiscount ProductType = "discount"
ProductTypeDigital ProductType = "digital"
ProductTypeShippingFee ProductType = "shipping_fee"
ProductTypeStoreCredit ProductType = "store_credit"
ProductTypeGiftCard ProductType = "gift_card"
ProductTypeSurcharge ProductType = "surcharge"
PaymentStatusCreated PaymentStatus = "created"
PaymentStatusPaid PaymentStatus = "paid"
PaymentStatusAuthorized PaymentStatus = "authorized"
PaymentStatusCanceled PaymentStatus = "canceled"
PaymentStatusShipping PaymentStatus = "shipping"
PaymentStatusCompleted PaymentStatus = "completed"
PaymentStatusExpired PaymentStatus = "expired"
)
type Address struct {
OrganizationName string `json:"organizationName,omitempty"`
Title string `json:"title,omitempty"`
GivenName string `json:"givenName"`
FamilyName string `json:"familyName"`
StreetAndNumber string `json:"streetAndNumber"`
StreetAdditional string `json:"streetAdditional,omitempty"`
PostalCode string `json:"postalCode"`
City string `json:"city"`
Region string `json:"region,omitempty"`
Country string `json:"country"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
}
type Order struct {
Resource string `json:"resource"`
ID string `json:"id"`
ProfileId string `json:"profileId"`
Method string `json:"method"`
Amount PaymentAmount `json:"amount"`
Status PaymentStatus `json:"status"`
IsCancelable bool `json:"isCancelable"`
Metadata map[string]any `json:"metadata"`
CreatedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
Mode string `json:"mode"`
Locale Locale `json:"locale"`
BillingAddress Address `json:"billingAddress"`
ConsumerDOB string `json:"consumerDateOfBirth"`
OrderNumber string `json:"orderNumber"`
ShippingAddress Address `json:"shippingAddress"`
RedirectURL string `json:"redirectUrl"`
WehookURL string `json:"webhookUrl"`
Lines []OrderLine `json:"lines"`
Links map[string]Link `json:"_links"`
}
func (c *APIClient) CreateOrder(param *CreateOrderParameters) (*Order, error) {
raw, _ := json.Marshal(param)
raw, err := c.request("orders", "POST", raw)
if err != nil {
return nil, err
}
o := &Order{}
if err = json.Unmarshal(raw, o); err != nil {
return nil, fmt.Errorf("failed to unmarshal json body: %v", err)
}
return o, nil
}
func (c *APIClient) GetOrder(id string) (*Order, error) {
raw, err := c.request(fmt.Sprintf("orders/%v", id), "GET", nil)
if err != nil {
return nil, err
}
o := &Order{}
if err = json.Unmarshal(raw, o); err != nil {
return nil, fmt.Errorf("failed to unmarshal json body: %v", err)
}
return o, nil
}