-
Notifications
You must be signed in to change notification settings - Fork 5
/
offer.go
148 lines (137 loc) · 5.25 KB
/
offer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ebay
import (
"context"
"fmt"
"net/http"
"time"
)
// OfferService handles communication with the Offer API
//
// eBay API docs: https://developer.ebay.com/api-docs/buy/offer/static/overview.html
type OfferService service
// Valid marketplace IDs
const (
BuyMarketplaceAustralia = "EBAY_AU"
BuyMarketplaceCanada = "EBAY_CA"
BuyMarketplaceGermany = "EBAY_DE"
BuyMarketplaceSpain = "EBAY_ES"
BuyMarketplaceFrance = "EBAY_FR"
BuyMarketplaceGreatBritain = "EBAY_GB"
BuyMarketplaceHongKong = "EBAY_HK"
BuyMarketplaceItalia = "EBAY_IT"
BuyMarketplaceUSA = "EBAY_US"
)
// Valid values for the "auctionStatus" Bidding field.
const (
BiddingAuctionStatusEnded = "ENDED"
)
// OptBuyMarketplace adds the header containing the marketplace id:
// https://developer.ebay.com/api-docs/buy/static/ref-marketplace-supported.html
//
// eBay API docs: https://developer.ebay.com/api-docs/buy/static/api-browse.html#Headers
func OptBuyMarketplace(marketplaceID string) func(*http.Request) {
return func(req *http.Request) {
req.Header.Set("X-EBAY-C-MARKETPLACE-ID", marketplaceID)
}
}
// Bidding represents an eBay item bidding.
type Bidding struct {
AuctionStatus string `json:"auctionStatus"`
AuctionEndDate time.Time `json:"auctionEndDate"`
ItemID string `json:"itemId"`
CurrentPrice struct {
Value string `json:"value"`
Currency string `json:"currency"`
} `json:"currentPrice"`
BidCount int `json:"bidCount"`
HighBidder bool `json:"highBidder"`
ReservePriceMet bool `json:"reservePriceMet"`
SuggestedBidAmounts []struct {
Value string `json:"value"`
Currency string `json:"currency"`
} `json:"suggestedBidAmounts"`
CurrentProxyBid struct {
ProxyBidID string `json:"proxyBidId"`
MaxAmount struct {
Value string `json:"value"`
Currency string `json:"currency"`
} `json:"maxAmount"`
} `json:"currentProxyBid"`
}
// Some valid eBay error codes for the GetBidding method.
//
// eBay API docs: https://developer.ebay.com/api-docs/buy/offer/resources/bidding/methods/getBidding#h2-error-codes
const (
ErrGetBiddingMarketplaceNotSupported = 120017
ErrGetBiddingNoBiddingActivity = 120033
)
// GetBidding retrieves the buyer's bidding details on a specific auction item.
//
// eBay API docs: https://developer.ebay.com/api-docs/buy/offer/resources/bidding/methods/getBidding
func (s *OfferService) GetBidding(ctx context.Context, itemID, marketplaceID string, opts ...Opt) (Bidding, error) {
u := fmt.Sprintf("buy/offer/v1_beta/bidding/%s", itemID)
opts = append(opts, OptBuyMarketplace(marketplaceID))
req, err := s.client.NewRequest(http.MethodGet, u, nil, opts...)
if err != nil {
return Bidding{}, err
}
var bid Bidding
return bid, s.client.Do(ctx, req, &bid)
}
// ProxyBid represents an eBay proxy bid.
type ProxyBid struct {
ProxyBidID string `json:"proxyBidId"`
}
// Some valid eBay error codes for the PlaceProxyBid method.
//
// eBay API docs: https://developer.ebay.com/api-docs/buy/offer/resources/bidding/methods/placeProxyBid#h2-error-codes
const (
ErrPlaceProxyBidAuctionEndedBecauseOfBuyItNow = 120002
ErrPlaceProxyBidBidCannotBeGreaterThanBuyItNowPrice = 120005
ErrPlaceProxyBidAmountTooHigh = 120007
ErrPlaceProxyBidAmountTooLow = 120008
ErrPlaceProxyBidCurrencyMustMatchItemPriceCurrency = 120009
ErrPlaceProxyBidCannotLowerYourProxyBid = 120010
ErrPlaceProxyBidAmountExceedsLimit = 120011
ErrPlaceProxyBidAuctionHasEnded = 120012
ErrPlaceProxyBidAmountInvalid = 120013
ErrPlaceProxyBidCurrencyInvalid = 120014
ErrPlaceProxyBidMaximumBidAmountMissing = 120016
)
// PlaceProxyBid places a proxy bid for the buyer on a specific auction item.
//
// Curency is the three-letter ISO 4217 code representing the currency.
// For one hundred US dollars, MaxAmout is "100.00" and currency is "USD".
//
// You must ensure the user agrees to the "Terms of use for Adult Only category"
// (https://signin.ebay.com/ws/eBayISAPI.dll?AdultSignIn2) if he wishes to bid on on a adult-only item.
// An item is adult-only if the AdultOnly field returned by the Browse API is set to true.
//
// eBay API docs: https://developer.ebay.com/api-docs/buy/offer/resources/bidding/methods/placeProxyBid
func (s *OfferService) PlaceProxyBid(ctx context.Context, itemID, marketplaceID, maxAmount, currency string, userConsentAdultOnlyItem bool, opts ...Opt) (ProxyBid, error) {
type userConsent struct {
AdultOnlyItem bool `json:"adultOnlyItem,omitempty"`
}
type amount struct {
Currency string `json:"currency"`
Value string `json:"value"`
}
type payload struct {
MaxAmount amount `json:"maxAmount"`
UserConsent *userConsent `json:"userConsent,omitempty"`
}
pl := payload{
MaxAmount: amount{Currency: currency, Value: maxAmount},
}
if userConsentAdultOnlyItem {
pl.UserConsent = &userConsent{userConsentAdultOnlyItem}
}
u := fmt.Sprintf("buy/offer/v1_beta/bidding/%s/place_proxy_bid", itemID)
opts = append(opts, OptBuyMarketplace(marketplaceID))
req, err := s.client.NewRequest(http.MethodPost, u, &pl, opts...)
if err != nil {
return ProxyBid{}, err
}
var bid ProxyBid
return bid, s.client.Do(ctx, req, &bid)
}