This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 325
/
transaction.go
60 lines (51 loc) · 2.17 KB
/
transaction.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
package goshopify
import "fmt"
// TransactionService is an interface for interfacing with the transactions endpoints of
// the Shopify API.
// See: https://help.shopify.com/api/reference/transaction
type TransactionService interface {
List(int, interface{}) ([]Transaction, error)
Count(int, interface{}) (int, error)
Get(int, int, interface{}) (*Transaction, error)
Create(int, Transaction) (*Transaction, error)
}
// TransactionServiceOp handles communication with the transaction related methods of the
// Shopify API.
type TransactionServiceOp struct {
client *Client
}
// TransactionResource represents the result from the orders/X/transactions/Y.json endpoint
type TransactionResource struct {
Transaction *Transaction `json:"transaction"`
}
// TransactionsResource represents the result from the orders/X/transactions.json endpoint
type TransactionsResource struct {
Transactions []Transaction `json:"transactions"`
}
// List transactions
func (s *TransactionServiceOp) List(orderID int, options interface{}) ([]Transaction, error) {
path := fmt.Sprintf("%s/%d/transactions.json", ordersBasePath, orderID)
resource := new(TransactionsResource)
err := s.client.Get(path, resource, options)
return resource.Transactions, err
}
// Count transactions
func (s *TransactionServiceOp) Count(orderID int, options interface{}) (int, error) {
path := fmt.Sprintf("%s/%d/transactions/count.json", ordersBasePath, orderID)
return s.client.Count(path, options)
}
// Get individual transaction
func (s *TransactionServiceOp) Get(orderID int, transactionID int, options interface{}) (*Transaction, error) {
path := fmt.Sprintf("%s/%d/transactions/%d.json", ordersBasePath, orderID, transactionID)
resource := new(TransactionResource)
err := s.client.Get(path, resource, options)
return resource.Transaction, err
}
// Create a new transaction
func (s *TransactionServiceOp) Create(orderID int, transaction Transaction) (*Transaction, error) {
path := fmt.Sprintf("%s/%d/transactions.json", ordersBasePath, orderID)
wrappedData := TransactionResource{Transaction: &transaction}
resource := new(TransactionResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Transaction, err
}