github.com/plutov/paypal/v4@v4.7.1/products.go (about) 1 package paypal 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 ) 8 9 type ( 10 // Product struct 11 Product struct { 12 ID string `json:"id,omitempty"` 13 Name string `json:"name"` 14 Description string `json:"description,omitempty"` 15 Category ProductCategory `json:"category,omitempty"` 16 Type ProductType `json:"type"` 17 ImageUrl string `json:"image_url,omitempty"` 18 HomeUrl string `json:"home_url,omitempty"` 19 } 20 21 CreateProductResponse struct { 22 Product 23 SharedResponse 24 } 25 26 ListProductsResponse struct { 27 Products []Product `json:"products"` 28 SharedListResponse 29 } 30 31 ProductListParameters struct { 32 ListParams 33 } 34 ) 35 36 func (self *Product) GetUpdatePatch() []Patch { 37 return []Patch{ 38 { 39 Operation: "replace", 40 Path: "/description", 41 Value: self.Description, 42 }, 43 { 44 Operation: "replace", 45 Path: "/category", 46 Value: self.Category, 47 }, 48 { 49 Operation: "replace", 50 Path: "/image_url", 51 Value: self.ImageUrl, 52 }, 53 { 54 Operation: "replace", 55 Path: "/home_url", 56 Value: self.HomeUrl, 57 }, 58 } 59 } 60 61 // CreateProduct creates a product 62 // Doc: https://developer.paypal.com/docs/api/catalog-products/v1/#products_create 63 // Endpoint: POST /v1/catalogs/products 64 func (c *Client) CreateProduct(ctx context.Context, product Product) (*CreateProductResponse, error) { 65 req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s%s", c.APIBase, "/v1/catalogs/products"), product) 66 response := &CreateProductResponse{} 67 if err != nil { 68 return response, err 69 } 70 err = c.SendWithAuth(req, response) 71 return response, err 72 } 73 74 // UpdateProduct. updates a product information 75 // Doc: https://developer.paypal.com/docs/api/catalog-products/v1/#products_patch 76 // Endpoint: PATCH /v1/catalogs/products/:product_id 77 func (c *Client) UpdateProduct(ctx context.Context, product Product) error { 78 req, err := c.NewRequest(ctx, http.MethodPatch, fmt.Sprintf("%s%s%s", c.APIBase, "/v1/catalogs/products/", product.ID), product.GetUpdatePatch()) 79 if err != nil { 80 return err 81 } 82 err = c.SendWithAuth(req, nil) 83 return err 84 } 85 86 // Get product details 87 // Doc: https://developer.paypal.com/docs/api/catalog-products/v1/#products_get 88 // Endpoint: GET /v1/catalogs/products/:product_id 89 func (c *Client) GetProduct(ctx context.Context, productId string) (*Product, error) { 90 req, err := c.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s%s%s", c.APIBase, "/v1/catalogs/products/", productId), nil) 91 response := &Product{} 92 if err != nil { 93 return response, err 94 } 95 err = c.SendWithAuth(req, response) 96 return response, err 97 } 98 99 // List all products 100 // Doc: https://developer.paypal.com/docs/api/catalog-products/v1/#products_list 101 // Endpoint: GET /v1/catalogs/products 102 func (c *Client) ListProducts(ctx context.Context, params *ProductListParameters) (*ListProductsResponse, error) { 103 req, err := c.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s%s", c.APIBase, "/v1/catalogs/products"), nil) 104 response := &ListProductsResponse{} 105 if err != nil { 106 return response, err 107 } 108 109 if params != nil { 110 q := req.URL.Query() 111 q.Add("page", params.Page) 112 q.Add("page_size", params.PageSize) 113 q.Add("total_required", params.TotalRequired) 114 req.URL.RawQuery = q.Encode() 115 } 116 117 err = c.SendWithAuth(req, response) 118 return response, err 119 }