diff --git a/casdoorsdk/product.go b/casdoorsdk/product.go index 891f210..920e751 100644 --- a/casdoorsdk/product.go +++ b/casdoorsdk/product.go @@ -124,23 +124,28 @@ func (c *Client) DeleteProduct(product *Product) (bool, error) { return affected, err } -func (c *Client) BuyProduct(name string, providerName string) (*Product, error) { +func (c *Client) BuyProduct(name string, providerName string, userName string) (*Product, error) { queryMap := map[string]string{ "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), "providerName": providerName, + "userName": userName, } - url := c.GetUrl("buy-product", queryMap) + resp, err := c.DoPost("buy-product", queryMap, []byte(""), false, false) + if err != nil { + return nil, err + } - bytes, err := c.DoGetBytes(url) + productJson, err := json.Marshal(resp.Data) if err != nil { return nil, err } - var product *Product - err = json.Unmarshal(bytes, &product) + var product Product + err = json.Unmarshal(productJson, &product) if err != nil { return nil, err } - return product, nil + + return &product, nil } diff --git a/casdoorsdk/product_global.go b/casdoorsdk/product_global.go index 3147de4..e6426b2 100644 --- a/casdoorsdk/product_global.go +++ b/casdoorsdk/product_global.go @@ -38,6 +38,6 @@ func DeleteProduct(product *Product) (bool, error) { return globalClient.DeleteProduct(product) } -func BuyProduct(name string, providerName string) (*Product, error) { - return globalClient.BuyProduct(name, providerName) +func BuyProduct(name string, providerName string, userName string) (*Product, error) { + return globalClient.BuyProduct(name, providerName, userName) } diff --git a/casdoorsdk/product_test.go b/casdoorsdk/product_test.go index a30246d..a9ebfa1 100644 --- a/casdoorsdk/product_test.go +++ b/casdoorsdk/product_test.go @@ -34,9 +34,10 @@ func TestProduct(t *testing.T) { Description: "Casdoor Website", Tag: "auto_created_product_for_plan", - Quantity: 999, - Sold: 0, - State: "Published", + Quantity: 999, + Sold: 0, + State: "Published", + Providers: []string{"provider_payment_dummy"}, } _, err := AddProduct(product) if err != nil { @@ -85,6 +86,14 @@ func TestProduct(t *testing.T) { t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedProduct.Description, updatedDescription) } + boughtProduct, err := BuyProduct(name, "provider_payment_dummy", "admin") + if err != nil { + t.Fatalf("Failed to buy product: %v", err) + } + if boughtProduct == nil { + t.Fatalf("Failed to buy product: nil response") + } + // Delete the object _, err = DeleteProduct(product) if err != nil {