| 1 | import StripeCheckout from "react-stripe-checkout";
|
|---|
| 2 | import {Button, Dialog, DialogContent, DialogTitle, IconButton} from "@mui/material";
|
|---|
| 3 | import CloseIcon from "@mui/icons-material/Close";
|
|---|
| 4 | import {CreatePayment, PaymentType, PublishableStripeKey} from "../../services/payment-service";
|
|---|
| 5 | import {useAuthContext} from "../../configurations/AuthContext";
|
|---|
| 6 | import {calculateTotalPrice} from "../functions";
|
|---|
| 7 | import {AttachMoneyOutlined, CreditCard} from "@mui/icons-material";
|
|---|
| 8 | import React, {useState} from "react";
|
|---|
| 9 | import {OrderStatus, UpdateOrder} from "../../services/order-service";
|
|---|
| 10 | import {useLocation, useNavigate} from "react-router-dom";
|
|---|
| 11 |
|
|---|
| 12 | const PaymentModal = ({naracka, ...props}) => {
|
|---|
| 13 |
|
|---|
| 14 | const {loggedUserRole, ownershipChanges} = useAuthContext();
|
|---|
| 15 | const navigate = useNavigate();
|
|---|
| 16 | const location = useLocation();
|
|---|
| 17 |
|
|---|
| 18 | const generateToken = async token => {
|
|---|
| 19 | const requestPayment = {
|
|---|
| 20 | potrosuvacId: loggedUserRole?.roleId,
|
|---|
| 21 | token: token.id,
|
|---|
| 22 | totalPrice: calculateTotalPrice(naracka?.narackaMenuItems),
|
|---|
| 23 | paymentType: PaymentType.DebitCard,
|
|---|
| 24 | narackaId: naracka.id
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | await CreatePayment(requestPayment);
|
|---|
| 28 | ownershipChanges(null);
|
|---|
| 29 | navigate(location.pathname, {replace: true});
|
|---|
| 30 | props.onClose();
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | const handleCashPayment = async () => {
|
|---|
| 34 | let totalPrice = calculateTotalPrice(naracka?.narackaMenuItems);
|
|---|
| 35 | const approval = window.confirm(`Are you sure you want to proceed with your order ? \n Total Amount to pay: ${totalPrice} `);
|
|---|
| 36 |
|
|---|
| 37 | let paymentRequest = {
|
|---|
| 38 | potrosuvacId: naracka?.potrosuvac?.id,
|
|---|
| 39 | narackaId: naracka?.id,
|
|---|
| 40 | paymentType: PaymentType.Cash,
|
|---|
| 41 | totalPrice: totalPrice
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if (approval) {
|
|---|
| 45 | let updatedOrder = {
|
|---|
| 46 | potrosuvacId: naracka?.potrosuvac?.id,
|
|---|
| 47 | status: OrderStatus.PendingAdminApproval,
|
|---|
| 48 | menuItems: naracka.narackaMenuItems.map(nmp => ({
|
|---|
| 49 | menuItemId: nmp?.menuItem?.id,
|
|---|
| 50 | quantity: nmp.quantity
|
|---|
| 51 | }))
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | await UpdateOrder(naracka.id, updatedOrder)
|
|---|
| 55 | await CreatePayment(paymentRequest);
|
|---|
| 56 | ownershipChanges(null);
|
|---|
| 57 | navigate(0);
|
|---|
| 58 | props.onClose();
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | const [open, setOpen] = useState(false);
|
|---|
| 63 |
|
|---|
| 64 | return (
|
|---|
| 65 | naracka && naracka.potrosuvac &&
|
|---|
| 66 | <Dialog
|
|---|
| 67 | {...props}
|
|---|
| 68 | disablebackdropclick="true"
|
|---|
| 69 | fullWidth={true}
|
|---|
| 70 | maxWidth={"md"}
|
|---|
| 71 | >
|
|---|
| 72 | <DialogTitle variant="h4" disabletypography="true" id={"choose-role-title"}
|
|---|
| 73 | className={"text-center font-weight-bolder"}>
|
|---|
| 74 | What will be your payment method ?
|
|---|
| 75 | <IconButton color={"success"} className={"float-end"} aria-label="close"
|
|---|
| 76 | onClick={() => props.onClose()}>
|
|---|
| 77 | <CloseIcon/>
|
|---|
| 78 | </IconButton>
|
|---|
| 79 | </DialogTitle>
|
|---|
| 80 | <DialogContent className={"mb-3 d-flex justify-content-evenly"}>
|
|---|
| 81 | <Button variant={"contained"} color={"success"} size={"small"}
|
|---|
| 82 | onClick={() => handleCashPayment()}>
|
|---|
| 83 | Cash <AttachMoneyOutlined/>
|
|---|
| 84 | </Button>
|
|---|
| 85 |
|
|---|
| 86 | <StripeCheckout
|
|---|
| 87 | amount={calculateTotalPrice(naracka?.narackaMenuItems) * 100}
|
|---|
| 88 | open={open}
|
|---|
| 89 | image={"https://t4.ftcdn.net/jpg/04/73/84/61/360_F_473846184_0k637f6855ZJqaulKqAmgJTEVGVibR1P.jpg"}
|
|---|
| 90 | name={naracka.potrosuvac.korisnik?.username}
|
|---|
| 91 | email={naracka.potrosuvac.korisnik?.email}
|
|---|
| 92 | shippingAddress
|
|---|
| 93 | billingAddress
|
|---|
| 94 | description={`Your total is ${calculateTotalPrice(naracka?.narackaMenuItems)} MKD`}
|
|---|
| 95 | panelLabel={"Pay Order"}
|
|---|
| 96 | currency="MKD"
|
|---|
| 97 | label={<>Card <CreditCard/></>}
|
|---|
| 98 | stripeKey={PublishableStripeKey}
|
|---|
| 99 | allowRememberMe
|
|---|
| 100 | onClose={() => setOpen(false)}
|
|---|
| 101 | token={generateToken}
|
|---|
| 102 | />
|
|---|
| 103 | </DialogContent>
|
|---|
| 104 | </Dialog>
|
|---|
| 105 | )
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | export default PaymentModal; |
|---|