2024-03-15 21:27:45 +03:00
|
|
|
|
package services
|
|
|
|
|
|
2024-03-26 02:21:35 +03:00
|
|
|
|
//func GetTotal(fuserId int) float64 {
|
|
|
|
|
// rdb := internal.InitRedis()
|
|
|
|
|
// keys, _ := rdb.Keys(context.Background(), fmt.Sprintf("api.api_cart.%d.*", fuserId)).Result()
|
|
|
|
|
//
|
|
|
|
|
// result := []models.CatalogWithQuantityWeb{}
|
|
|
|
|
//
|
|
|
|
|
// for _, key := range keys {
|
|
|
|
|
// str, _ := rdb.Get(context.Background(), key).Result()
|
|
|
|
|
// item := models.CatalogWithQuantityWeb{}
|
|
|
|
|
//
|
|
|
|
|
// json.Unmarshal([]byte(str), &item)
|
|
|
|
|
// result = append(result, item)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// sum := float64(0)
|
|
|
|
|
//
|
|
|
|
|
// for _, catalogItem := range result {
|
|
|
|
|
// sum = sum + catalogItem.Price["BASE"].(float64)*float64(catalogItem.Quantity)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return sum
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//type addProductsToOrderReq struct {
|
|
|
|
|
// ProductId int `db:"product_id"`
|
|
|
|
|
// PriceTypeId int `db:"price_type_id"`
|
|
|
|
|
// Quantity int `db:"quantity"`
|
|
|
|
|
// Price float64 `db:"price"`
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func addProductsToOrder(api bitrix.Bitrix, fuserId int, orderId int) error {
|
|
|
|
|
// //Получаем данные из корзины
|
|
|
|
|
//
|
|
|
|
|
// cartItems := GetCartItems(fuserId)
|
|
|
|
|
//
|
|
|
|
|
// rdb := internal.InitRedis()
|
|
|
|
|
// rdb.Keys(context.Background(), "")
|
|
|
|
|
//
|
|
|
|
|
// for _, product := range cartItems {
|
|
|
|
|
// err := api.AddProductToOrder(orderId, product.Id, product.Price["BASE"].(float64), product.Quantity)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return err
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return nil
|
|
|
|
|
//
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func MakeOrder(fuserId int, email string, fullName string, phone string) (map[string]interface{}, error) {
|
|
|
|
|
//
|
|
|
|
|
// // Инициализируем api
|
|
|
|
|
//
|
|
|
|
|
// api := bitrix.Initialize()
|
|
|
|
|
//
|
|
|
|
|
// // 1. Создаем анонимного пользователя
|
|
|
|
|
//
|
|
|
|
|
// userId, _ := api.CreateAnonymousUser()
|
|
|
|
|
//
|
|
|
|
|
// // 2. Создаем заказ
|
|
|
|
|
// orderId, _ := api.CreateOrder(userId)
|
|
|
|
|
//
|
|
|
|
|
// // --- обновляем контакт пользователя
|
|
|
|
|
// order, orderErr := api.GetOrderInfo(orderId)
|
|
|
|
|
// if orderErr != nil {
|
|
|
|
|
// return nil, orderErr
|
|
|
|
|
// }
|
|
|
|
|
// clientId, _ := strconv.Atoi(order.Clients[0].EntityId)
|
|
|
|
|
// api.UpdateContact(clientId, email, fullName, phone)
|
|
|
|
|
//
|
|
|
|
|
// // 3. Добавляем элементы в корзину
|
|
|
|
|
// addProductErr := addProductsToOrder(api, fuserId, orderId)
|
|
|
|
|
// if addProductErr != nil {
|
|
|
|
|
// return nil, addProductErr
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // 4. Получаем обновленный ресурс заказа
|
|
|
|
|
// order, _ = api.GetOrderInfo(orderId)
|
|
|
|
|
//
|
|
|
|
|
// // 5. Добавляем способ оплаты товара
|
|
|
|
|
// createPaymentError := api.CreatePayment(orderId, order.Price)
|
|
|
|
|
//
|
|
|
|
|
// if createPaymentError != nil {
|
|
|
|
|
// return nil, createPaymentError
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // 6. Получаем ресурс оплаты и url для нее
|
|
|
|
|
// paymentData, _ := kassa.CreatePayment(orderId, order.Price, fullName, email, phone, getItemsForPayment(order))
|
|
|
|
|
//
|
|
|
|
|
// insPaymentDataStmt := fmt.Sprintf(`
|
|
|
|
|
// insert into api_youkassa_payment (payment_id, order_id, link, status)
|
|
|
|
|
// values ('%s',
|
|
|
|
|
// '%s',
|
|
|
|
|
// '%s',
|
|
|
|
|
// '%s');
|
|
|
|
|
// `, paymentData["id"].(string),
|
|
|
|
|
// orderId,
|
|
|
|
|
// paymentData["confirmation"].(map[string]interface{})["confirmation_url"].(string),
|
|
|
|
|
// paymentData["status"].(string),
|
|
|
|
|
// )
|
|
|
|
|
//
|
|
|
|
|
// db := internal.InitDatabase()
|
|
|
|
|
//
|
|
|
|
|
// db.Execute(insPaymentDataStmt)
|
|
|
|
|
//
|
|
|
|
|
// return paymentData, nil
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func getItemsForPayment(order *bitrix.OrderResource) []kassa.KassaReceiptItems {
|
|
|
|
|
// result := []kassa.KassaReceiptItems{}
|
|
|
|
|
//
|
|
|
|
|
// for _, basketItem := range order.BasketItems {
|
|
|
|
|
// quantity, _ := strconv.Atoi(strings.Split(basketItem.Quantity, ".")[0])
|
|
|
|
|
// item := kassa.KassaReceiptItems{
|
|
|
|
|
// Description: basketItem.Name,
|
|
|
|
|
// Amount: kassa.KassaAmount{
|
|
|
|
|
// Value: fmt.Sprintf("%f", basketItem.Price),
|
|
|
|
|
// Currency: "RUB",
|
|
|
|
|
// },
|
|
|
|
|
// VatCode: VatCodes.NDS_20,
|
|
|
|
|
// Quantity: fmt.Sprintf("%d", quantity),
|
|
|
|
|
// Measure: Measure.PIECE,
|
|
|
|
|
// PaymentSubject: PaymentSubject.COMMODITY,
|
|
|
|
|
// PaymentMode: PaymentMode.FULL_PAYMENT,
|
|
|
|
|
// }
|
|
|
|
|
// result = append(result, item)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return result
|
|
|
|
|
//}
|