Untitled

mail@pastecode.io avatar
unknown
tsx
a year ago
2.3 kB
4
Indexable
Never
import api from '@/services/apiServcie';
import { FetchedLogginedUserDataType } from '@/types/global/fetchedLogginedUserData.type';
import { BuyCheckoutFormDataType } from '@/types/routes/buy/buyCheckoutFormData.type';
import { BookType } from '@/types/routes/global/book.type';
import { AxiosResponse } from 'axios';

type AxiosResponseType = {
  data: string | null;
  msg: string;
  errorCode: 0 | 103;
};

export const APIvalidateCartItemsAndGetZarinpalUrlForPayment = async (args: {
  token: string | undefined;
  totalBookPrice: number;
  formData: BuyCheckoutFormDataType;
  totalWeight: number;
  phoneNumber: string;
  fetchedLogginedUserData: FetchedLogginedUserDataType | null;
  booksData:
    | (BookType &
        {
          status: 1 | 2 | 3;
          max_sell: number;
          quantity: number;
          modifyPrice: number;
        }[])
    | null;
}): Promise<AxiosResponseType> => {
  const { data }: AxiosResponse<AxiosResponseType> = await api.post(
    '/checkoutsell',
    {
      customerid: args.phoneNumber,
      city: args.formData.city!.key,
      state: args.formData.province!.key,
      address: args.formData.address,
      postalCode: args.formData.postalCode,
      first_name: args.formData.firstName,
      last_name: args.formData.lastName,
      post_price: Number(args.formData!.transportPrice) * 10,
      countPack: Number(args.fetchedLogginedUserData?.postalcode),
      postMethod: args.fetchedLogginedUserData?.postmethod,
      sendTime: args.fetchedLogginedUserData?.sendtime,
      sendDay: args.fetchedLogginedUserData?.sendday,
      bank: args.fetchedLogginedUserData?.bank,
      accNumber: args.fetchedLogginedUserData?.accnumber,
      accountholder: args.fetchedLogginedUserData?.accountholder,
      email: args.fetchedLogginedUserData?.email,
      discountcode: '',
      sell: '1',
      total_weight: args.totalWeight,
      payment_method: 'post',
      total_books_price: args.totalBookPrice,
      total_dicounts: '',
      final_total_price:
        args.totalBookPrice + Number(args.formData!.transportPrice),
      books: args.booksData,
    },
    {
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${args.token}`,
      },
    },
  );
  return data;
};