hotfix make order
parent
802b70a601
commit
b8b4e89bc0
|
@ -13,8 +13,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var EP = os.Getenv("BITRIX_API_EP")
|
|
||||||
|
|
||||||
type ClientsResource struct {
|
type ClientsResource struct {
|
||||||
EntityId string `json:"entityId"`
|
EntityId string `json:"entityId"`
|
||||||
}
|
}
|
||||||
|
@ -166,7 +164,9 @@ type BasketResource struct {
|
||||||
} `json:"properties"`
|
} `json:"properties"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type bitrix struct{}
|
type bitrix struct {
|
||||||
|
EP string
|
||||||
|
}
|
||||||
|
|
||||||
type Bitrix interface {
|
type Bitrix interface {
|
||||||
CreateAnonymousUser() (int, error)
|
CreateAnonymousUser() (int, error)
|
||||||
|
@ -191,7 +191,7 @@ type createAnonymousUserResponse struct {
|
||||||
Result int `json:"result"`
|
Result int `json:"result"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ bitrix) CreateAnonymousUser() (int, error) {
|
func (b bitrix) CreateAnonymousUser() (int, error) {
|
||||||
uid, _ := uuid.NewUUID()
|
uid, _ := uuid.NewUUID()
|
||||||
req := createAnonymousUserRequest{
|
req := createAnonymousUserRequest{
|
||||||
Email: fmt.Sprintf("anonymous%s@anonym.ru", uid.String()),
|
Email: fmt.Sprintf("anonymous%s@anonym.ru", uid.String()),
|
||||||
|
@ -203,7 +203,7 @@ func (_ bitrix) CreateAnonymousUser() (int, error) {
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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{}
|
result := createAnonymousUserResponse{}
|
||||||
|
|
||||||
|
@ -231,7 +231,7 @@ type createOrderResponse struct {
|
||||||
} `json:"result"`
|
} `json:"result"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ bitrix) CreateOrder(userId int) (int, error) {
|
func (b bitrix) CreateOrder(userId int) (int, error) {
|
||||||
req := createOrderRequestWrapper{createOrderRequest{
|
req := createOrderRequestWrapper{createOrderRequest{
|
||||||
Lid: "s2",
|
Lid: "s2",
|
||||||
PersonTypeId: 5,
|
PersonTypeId: 5,
|
||||||
|
@ -241,7 +241,7 @@ func (_ bitrix) CreateOrder(userId int) (int, error) {
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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{}
|
result := createOrderResponse{}
|
||||||
|
|
||||||
err := json.NewDecoder(resp.Body).Decode(&result)
|
err := json.NewDecoder(resp.Body).Decode(&result)
|
||||||
|
@ -249,7 +249,7 @@ func (_ bitrix) CreateOrder(userId int) (int, error) {
|
||||||
return result.Result.Order.Id, err
|
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{}{
|
req := map[string]interface{}{
|
||||||
"id": paymentId,
|
"id": paymentId,
|
||||||
"fields": map[string]interface{}{"paid": "Y", "paySystemId": paySystemId},
|
"fields": map[string]interface{}{"paid": "Y", "paySystemId": paySystemId},
|
||||||
|
@ -257,12 +257,12 @@ func (_ bitrix) ApprovePayment(paymentId int, paySystemId int) error {
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ bitrix) CancelOrder(orderId int) error {
|
func (b bitrix) CancelOrder(orderId int) error {
|
||||||
req := map[string]interface{}{
|
req := map[string]interface{}{
|
||||||
"id": orderId,
|
"id": orderId,
|
||||||
"fields": map[string]interface{}{"canceled": "Y"},
|
"fields": map[string]interface{}{"canceled": "Y"},
|
||||||
|
@ -270,19 +270,19 @@ func (_ bitrix) CancelOrder(orderId int) error {
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ bitrix) GetOrderInfo(orderId int) (*OrderResource, error) {
|
func (b bitrix) GetOrderInfo(orderId int) (*OrderResource, error) {
|
||||||
req := map[string]interface{}{
|
req := map[string]interface{}{
|
||||||
"id": orderId,
|
"id": orderId,
|
||||||
}
|
}
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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 {
|
||||||
Result struct {
|
Result struct {
|
||||||
|
@ -300,7 +300,7 @@ func (_ bitrix) GetOrderInfo(orderId int) (*OrderResource, error) {
|
||||||
return &resultParsed, err
|
return &resultParsed, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ bitrix) CreatePayment(orderId int, sum float64) error {
|
func (b bitrix) CreatePayment(orderId int, sum float64) error {
|
||||||
req := map[string]interface{}{
|
req := map[string]interface{}{
|
||||||
"fields": map[string]interface{}{
|
"fields": map[string]interface{}{
|
||||||
"orderId": orderId,
|
"orderId": orderId,
|
||||||
|
@ -312,12 +312,12 @@ func (_ bitrix) CreatePayment(orderId int, sum float64) error {
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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
|
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{}{
|
req := map[string]interface{}{
|
||||||
"fields": map[string]interface{}{
|
"fields": map[string]interface{}{
|
||||||
"orderId": orderId,
|
"orderId": orderId,
|
||||||
|
@ -330,12 +330,12 @@ func (_ bitrix) AddProductToOrder(orderId int, productId int, price float64, qua
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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
|
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{}{
|
req := map[string]interface{}{
|
||||||
"id": contactId,
|
"id": contactId,
|
||||||
"fields": map[string]interface{}{
|
"fields": map[string]interface{}{
|
||||||
|
@ -346,7 +346,6 @@ func (_ bitrix) UpdateContact(contactId int, email string, name string, phone st
|
||||||
}),
|
}),
|
||||||
"NAME": strings.Split(name, " ")[1],
|
"NAME": strings.Split(name, " ")[1],
|
||||||
"LAST_NAME": strings.Split(name, " ")[0],
|
"LAST_NAME": strings.Split(name, " ")[0],
|
||||||
"SECOND_NAME": strings.Split(name, " ")[2],
|
|
||||||
"PHONE": append([]map[string]interface{}{}, map[string]interface{}{
|
"PHONE": append([]map[string]interface{}{}, map[string]interface{}{
|
||||||
"VALUE_TYPE": "other",
|
"VALUE_TYPE": "other",
|
||||||
"VALUE": phone,
|
"VALUE": phone,
|
||||||
|
@ -357,11 +356,13 @@ func (_ bitrix) UpdateContact(contactId int, email string, name string, phone st
|
||||||
|
|
||||||
query, _ := json.Marshal(req)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Initialize() Bitrix {
|
func Initialize() Bitrix {
|
||||||
return &bitrix{}
|
return &bitrix{
|
||||||
|
EP: os.Getenv("BITRIX_API_EP"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,17 +7,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"relynolli-server/external/kassa/Measure"
|
"relynolli-server/external/kassa/Measure"
|
||||||
"relynolli-server/external/kassa/PaymentMode"
|
"relynolli-server/external/kassa/PaymentMode"
|
||||||
"relynolli-server/external/kassa/PaymentSubject"
|
"relynolli-server/external/kassa/PaymentSubject"
|
||||||
"relynolli-server/external/kassa/TaxSystemCode"
|
"relynolli-server/external/kassa/TaxSystemCode"
|
||||||
"relynolli-server/external/kassa/VatCodes"
|
"relynolli-server/external/kassa/VatCodes"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var once sync.Once
|
|
||||||
|
|
||||||
type KassaAmount struct {
|
type KassaAmount struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
Currency string `json:"currency"`
|
Currency string `json:"currency"`
|
||||||
|
@ -105,7 +103,8 @@ func CreatePayment(orderId int, sum float64, fullName string, email string, phon
|
||||||
|
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
request, _ := http.NewRequest(http.MethodPost, BASE_URL, bytes.NewBuffer(query))
|
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("Idempotence-Key", uid.String())
|
||||||
request.Header.Set("Content-Type", "application/json")
|
request.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"relynolli-server/models"
|
"relynolli-server/models"
|
||||||
"relynolli-server/services"
|
"relynolli-server/services"
|
||||||
"relynolli-server/status"
|
"relynolli-server/status"
|
||||||
|
@ -85,7 +86,15 @@ func (h handlers) MakeOrder(c *gin.Context) {
|
||||||
if validationErr != nil {
|
if validationErr != nil {
|
||||||
responseErr := validationErr.(validator.ValidationErrors)[0]
|
responseErr := validationErr.(validator.ValidationErrors)[0]
|
||||||
meta.RequestFinished = time.Now().Unix()
|
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
|
resp.Status = status.STATUS_BAD_REQUEST
|
||||||
c.JSON(http.StatusBadRequest, resp)
|
c.JSON(http.StatusBadRequest, resp)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue