Skip to content

Commit 8bec1d2

Browse files
committed
feat: remove unwanted files
1 parent 3c9e7e6 commit 8bec1d2

File tree

7 files changed

+88
-112
lines changed

7 files changed

+88
-112
lines changed

src/middlewares/cors.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/pages/api/checkout.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cors from "../../middlewares/cors";
21
import { PAYMENT_TYPE } from "../../core/constants";
32
import * as quoteService from "../../services/quote";
43
import * as latitudeService from "../../services/latitude";
@@ -18,7 +17,7 @@ const handlePost = async (req, res) => {
1817
// Other payment methods here
1918

2019
default:
21-
res.status(403).end(`Payment method ${paymentType} not allowed`);
20+
res.status(403).end(`Payment method ${paymentType} not supported`);
2221
break;
2322
}
2423
};
@@ -34,7 +33,5 @@ const handlers = {
3433
default: handleDefault,
3534
};
3635

37-
export default async (req, res) => {
38-
await cors(req, res);
39-
return (handlers[req.method] || handlers.default)(req, res);
40-
};
36+
export default async (req, res) =>
37+
(handlers[req.method] || handlers.default)(req, res);

src/pages/api/latitude/complete.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/pages/api/payment.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as quoteService from "../../services/quote";
2+
import * as latitudeService from "../../services/latitude";
3+
4+
import { PAYMENT_TYPE } from "../../core/constants";
5+
6+
const handlePost = async (req, res) => {
7+
const {
8+
paymentType,
9+
merchantReference,
10+
transactionReference,
11+
gatewayReference,
12+
} = req?.body || {};
13+
14+
if (
15+
!paymentType ||
16+
!merchantReference ||
17+
!transactionReference ||
18+
!gatewayReference
19+
) {
20+
res.status(400).end("Missing required params");
21+
return;
22+
}
23+
24+
switch (paymentType) {
25+
case PAYMENT_TYPE.LATITUDE_INTEREST_FREE:
26+
// verify whether quote exists in your DB
27+
const quote = quoteService.getById(merchantReference);
28+
29+
if (!quote.id) {
30+
res.status(400).end(`Quote ${merchantReference} not found`);
31+
return;
32+
}
33+
34+
// verify payment with Latitude
35+
const { result } = await latitudeService.verifyPurchase({
36+
merchantReference,
37+
transactionReference,
38+
gatewayReference,
39+
});
40+
41+
// change payment status in DB (or similar)
42+
quoteService.changePaymentStatus({
43+
quoteId: merchantReference,
44+
result,
45+
transactionReference,
46+
gatewayReference,
47+
});
48+
49+
res.status(200).json({ result });
50+
break;
51+
52+
// Other payment methods here
53+
54+
default:
55+
res.status(403).end(`Payment method ${paymentType} not supported`);
56+
break;
57+
}
58+
};
59+
60+
const handleDefault = (req, res) => {
61+
const { method } = req;
62+
res.setHeader("Allow", Object.keys(handlers));
63+
res.status(405).end(`Method ${method} not allowed`);
64+
};
65+
66+
const handlers = {
67+
POST: handlePost,
68+
default: handleDefault,
69+
};
70+
71+
export default async (req, res) =>
72+
(handlers[req.method] || handlers.default)(req, res);

src/pages/complete.js renamed to src/pages/payment/latitude.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import axios from "axios";
33
import { useRouter } from "next/router";
44
import { Button, Container, Loader, Message, Icon } from "semantic-ui-react";
55

6-
import { Layout } from "../components";
6+
import { PAYMENT_TYPE } from "../../core/constants";
7+
import { Layout } from "../../components";
78

89
const MESSAGES = {
910
LOADING: "Please wait while we complete your order ..",
@@ -30,12 +31,11 @@ const CompletePage = () => {
3031
}
3132

3233
try {
33-
const { data } = await axios.get("/api/latitude/complete", {
34-
params: {
35-
merchantReference,
36-
transactionReference,
37-
gatewayReference,
38-
},
34+
const { data } = await axios.post("/api/payment", {
35+
paymentType: PAYMENT_TYPE.LATITUDE_INTEREST_FREE,
36+
merchantReference,
37+
transactionReference,
38+
gatewayReference,
3939
});
4040

4141
const msg =

src/services/latitude/prepareRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default (quote) => ({
3535
},
3636
merchantUrls: {
3737
cancel: `${STORE_BASE_URL}/cart`,
38-
complete: `${STORE_BASE_URL}/complete`,
38+
complete: `${STORE_BASE_URL}/payment/latitude`,
3939
},
4040
orderLines: quote.lineItems.map((item) => ({
4141
name: item.title,

src/services/quote/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import mockQuote from "../../__mocks__/quote.json";
88
export const getById = (quoteId) => mockQuote;
99

1010
/**
11-
* Currently returns a mock order id.
12-
* Ideally this function is expected to
13-
* - change status to "Paid" (or similar)
11+
* Currently doesnt do anything, ideally this function is expected to
12+
* - change status to "Paid" (or relevant status based on result)
1413
* - store transactionReference gatewayReference in DB for future reference
1514
* @param {string} quoteId
1615
* @param {string} transactionReference
1716
* @param {string} gatewayReference
1817
*/
19-
export const markAsPaid = ({
18+
export const changePaymentStatus = ({
19+
result,
2020
quoteId,
2121
transactionReference,
2222
gatewayReference,
2323
}) => ({
24-
orderId: 30440,
24+
result,
2525
quoteId,
2626
transactionReference,
2727
gatewayReference,

0 commit comments

Comments
 (0)