123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
package services
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"relynolli-server/external/bitrix"
|
||
"relynolli-server/external/kassa"
|
||
"relynolli-server/external/kassa/Measure"
|
||
"relynolli-server/external/kassa/PaymentMode"
|
||
"relynolli-server/external/kassa/PaymentSubject"
|
||
"relynolli-server/external/kassa/VatCodes"
|
||
"relynolli-server/internal"
|
||
"relynolli-server/storage"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
func addProductsToOrder(ctx context.Context, storage storage.StorageCart, api bitrix.Bitrix, fuserId, orderId int) error {
|
||
// //Получаем данные из корзины
|
||
//
|
||
items, err := storage.GetCartItems(ctx, int64(fuserId))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
for _, product := range *items {
|
||
err = api.AddProductToOrder(orderId, int(product.ProductId), product.Product.Price.BASE, int(product.Quantity))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
|
||
}
|
||
|
||
//
|
||
|
||
func MakeOrder(ctx context.Context, fuserId int, email string, fullName string, phone string) (*kassa.KassaResult, error) {
|
||
//
|
||
// Инициализируем api
|
||
s := storage.NewStorageCart()
|
||
|
||
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)
|
||
err := api.UpdateContact(clientId, email, fullName, phone)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 3. Добавляем элементы в корзину
|
||
addProductErr := addProductsToOrder(ctx, s, 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))
|
||
|
||
db := internal.InitDatabase().GetInstance()
|
||
|
||
db.NewRaw(`
|
||
insert into api_youkassa_payment (payment_id, order_id, link, status)
|
||
values (?, ?, ?,? );`,
|
||
paymentData.Id,
|
||
orderId,
|
||
paymentData.Confirmation.ConfirmationUrl,
|
||
paymentData.Status).Exec(ctx)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
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
|
||
}
|