diff --git a/external/bitrix/api.go b/external/bitrix/api.go index 086bc82..1145cea 100644 --- a/external/bitrix/api.go +++ b/external/bitrix/api.go @@ -13,8 +13,6 @@ import ( "time" ) -var EP = os.Getenv("BITRIX_API_EP") - type ClientsResource struct { EntityId string `json:"entityId"` } @@ -166,7 +164,9 @@ type BasketResource struct { } `json:"properties"` } -type bitrix struct{} +type bitrix struct { + EP string +} type Bitrix interface { CreateAnonymousUser() (int, error) @@ -191,7 +191,7 @@ type createAnonymousUserResponse struct { Result int `json:"result"` } -func (_ bitrix) CreateAnonymousUser() (int, error) { +func (b bitrix) CreateAnonymousUser() (int, error) { uid, _ := uuid.NewUUID() req := createAnonymousUserRequest{ Email: fmt.Sprintf("anonymous%s@anonym.ru", uid.String()), @@ -203,7 +203,7 @@ func (_ bitrix) CreateAnonymousUser() (int, error) { query, _ := json.Marshal(req) - resp, err := http.Post(EP+"/user.add", "application/json", bytes.NewBuffer(query)) + resp, err := http.Post(b.EP+"/user.add", "application/json", bytes.NewBuffer(query)) result := createAnonymousUserResponse{} @@ -231,7 +231,7 @@ type createOrderResponse struct { } `json:"result"` } -func (_ bitrix) CreateOrder(userId int) (int, error) { +func (b bitrix) CreateOrder(userId int) (int, error) { req := createOrderRequestWrapper{createOrderRequest{ Lid: "s2", PersonTypeId: 5, @@ -241,7 +241,7 @@ func (_ bitrix) CreateOrder(userId int) (int, error) { query, _ := json.Marshal(req) - resp, _ := http.Post(EP+"/sale.order.add", "application/json", bytes.NewBuffer(query)) + resp, _ := http.Post(b.EP+"/sale.order.add", "application/json", bytes.NewBuffer(query)) result := createOrderResponse{} err := json.NewDecoder(resp.Body).Decode(&result) @@ -249,7 +249,7 @@ func (_ bitrix) CreateOrder(userId int) (int, error) { return result.Result.Order.Id, err } -func (_ bitrix) ApprovePayment(paymentId int, paySystemId int) error { +func (b bitrix) ApprovePayment(paymentId int, paySystemId int) error { req := map[string]interface{}{ "id": paymentId, "fields": map[string]interface{}{"paid": "Y", "paySystemId": paySystemId}, @@ -257,12 +257,12 @@ func (_ bitrix) ApprovePayment(paymentId int, paySystemId int) error { query, _ := json.Marshal(req) - _, err := http.Post(EP+"/sale.payment.update", "application/json", bytes.NewBuffer(query)) + _, err := http.Post(b.EP+"/sale.payment.update", "application/json", bytes.NewBuffer(query)) return err } -func (_ bitrix) CancelOrder(orderId int) error { +func (b bitrix) CancelOrder(orderId int) error { req := map[string]interface{}{ "id": orderId, "fields": map[string]interface{}{"canceled": "Y"}, @@ -270,19 +270,19 @@ func (_ bitrix) CancelOrder(orderId int) error { query, _ := json.Marshal(req) - _, err := http.Post(EP+"/sale.order.update", "application/json", bytes.NewBuffer(query)) + _, err := http.Post(b.EP+"/sale.order.update", "application/json", bytes.NewBuffer(query)) return err } -func (_ bitrix) GetOrderInfo(orderId int) (*OrderResource, error) { +func (b bitrix) GetOrderInfo(orderId int) (*OrderResource, error) { req := map[string]interface{}{ "id": orderId, } query, _ := json.Marshal(req) - response, err := http.Post(EP+"/sale.order.get", "application/json", bytes.NewBuffer(query)) + response, err := http.Post(b.EP+"/sale.order.get", "application/json", bytes.NewBuffer(query)) result := struct { Result struct { @@ -300,7 +300,7 @@ func (_ bitrix) GetOrderInfo(orderId int) (*OrderResource, error) { return &resultParsed, err } -func (_ bitrix) CreatePayment(orderId int, sum float64) error { +func (b bitrix) CreatePayment(orderId int, sum float64) error { req := map[string]interface{}{ "fields": map[string]interface{}{ "orderId": orderId, @@ -312,12 +312,12 @@ func (_ bitrix) CreatePayment(orderId int, sum float64) error { query, _ := json.Marshal(req) - _, err := http.Post(EP+"/sale.payment.add", "application/json", bytes.NewBuffer(query)) + _, err := http.Post(b.EP+"/sale.payment.add", "application/json", bytes.NewBuffer(query)) return err } -func (_ bitrix) AddProductToOrder(orderId int, productId int, price float64, quantity int) error { +func (b bitrix) AddProductToOrder(orderId int, productId int, price float64, quantity int) error { req := map[string]interface{}{ "fields": map[string]interface{}{ "orderId": orderId, @@ -330,12 +330,12 @@ func (_ bitrix) AddProductToOrder(orderId int, productId int, price float64, qua query, _ := json.Marshal(req) - _, err := http.Post(EP+"/sale.basketitem.addCatalogProduct", "application/json", bytes.NewBuffer(query)) + _, err := http.Post(b.EP+"/sale.basketitem.addCatalogProduct", "application/json", bytes.NewBuffer(query)) return err } -func (_ bitrix) UpdateContact(contactId int, email string, name string, phone string) error { +func (b bitrix) UpdateContact(contactId int, email string, name string, phone string) error { req := map[string]interface{}{ "id": contactId, "fields": map[string]interface{}{ @@ -344,9 +344,8 @@ func (_ bitrix) UpdateContact(contactId int, email string, name string, phone st "VALUE": email, "TYPE_ID": "EMAIL", }), - "NAME": strings.Split(name, " ")[1], - "LAST_NAME": strings.Split(name, " ")[0], - "SECOND_NAME": strings.Split(name, " ")[2], + "NAME": strings.Split(name, " ")[1], + "LAST_NAME": strings.Split(name, " ")[0], "PHONE": append([]map[string]interface{}{}, map[string]interface{}{ "VALUE_TYPE": "other", "VALUE": phone, @@ -357,11 +356,13 @@ func (_ bitrix) UpdateContact(contactId int, email string, name string, phone st query, _ := json.Marshal(req) - _, err := http.Post(EP+"/crm.contact.update", "application/json", bytes.NewBuffer(query)) + _, err := http.Post(b.EP+"/crm.contact.update", "application/json", bytes.NewBuffer(query)) return err } func Initialize() Bitrix { - return &bitrix{} + return &bitrix{ + EP: os.Getenv("BITRIX_API_EP"), + } } diff --git a/external/kassa/kassa.go b/external/kassa/kassa.go index 97fe23f..b4de977 100644 --- a/external/kassa/kassa.go +++ b/external/kassa/kassa.go @@ -7,17 +7,15 @@ import ( "fmt" "github.com/google/uuid" "net/http" + "os" "relynolli-server/external/kassa/Measure" "relynolli-server/external/kassa/PaymentMode" "relynolli-server/external/kassa/PaymentSubject" "relynolli-server/external/kassa/TaxSystemCode" "relynolli-server/external/kassa/VatCodes" - "sync" "time" ) -var once sync.Once - type KassaAmount struct { Value string `json:"value"` Currency string `json:"currency"` @@ -105,7 +103,8 @@ func CreatePayment(orderId int, sum float64, fullName string, email string, phon client := http.Client{} request, _ := http.NewRequest(http.MethodPost, BASE_URL, bytes.NewBuffer(query)) - request.Header.Set("Authorization", "Basic "+basicAuth(ACCOUNT_ID, PASSWORD)) + request.Header.Set("Authorization", "Basic "+basicAuth(os.Getenv("YOOKASSA_ACCOUNT_ID"), + os.Getenv("YOOKASSA_ACCOUNT_SECRET"))) request.Header.Set("Idempotence-Key", uid.String()) request.Header.Set("Content-Type", "application/json") diff --git a/handlers/order/endpoints/ep.go b/handlers/order/endpoints/ep.go index 977d11d..987f8d9 100644 --- a/handlers/order/endpoints/ep.go +++ b/handlers/order/endpoints/ep.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "regexp" "relynolli-server/models" "relynolli-server/services" "relynolli-server/status" @@ -85,7 +86,15 @@ func (h handlers) MakeOrder(c *gin.Context) { if validationErr != nil { responseErr := validationErr.(validator.ValidationErrors)[0] meta.RequestFinished = time.Now().Unix() - resp.Info = fmt.Sprintf("Error: %s", responseErr.Error()) + resp.Info = responseErr.Error() + resp.Status = status.STATUS_BAD_REQUEST + c.JSON(http.StatusBadRequest, resp) + return + } + phoneMatched, err := regexp.Match("^\\d{11}$", []byte(req.PhoneNumber)) + if phoneMatched == false { + meta.RequestFinished = time.Now().Unix() + resp.Info = "Phone number is not valid" resp.Status = status.STATUS_BAD_REQUEST c.JSON(http.StatusBadRequest, resp) return