Skip to content

Commit

Permalink
Merge pull request #13 from swikars1/add-types
Browse files Browse the repository at this point in the history
Add types
  • Loading branch information
swikars1 authored Apr 7, 2024
2 parents 6082956 + e78e2a9 commit c7389e1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-dragons-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"khalti-epayment": patch
---

add builders
Empty file removed epaymentv2/khaltiInstanceServer.ts
Empty file.
27 changes: 18 additions & 9 deletions epaymentv2/paymentLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ export interface PaymentLookupRequest {
/**
* Represents the base structure of a successful lookup API response.
*/
export interface PaymentLookupSuccessResponse {
interface PaymentLookupGeneralResponse {
/** The initial payment identifier. */
pidx: string;

/** The total amount of the transaction (in what currency? Assuming Paisa here). */
total_amount: number;

/** The current status of the transaction. */
status: PaymentStatus;

/** The unique transaction identifier assigned by Khalti. */
transaction_id: string | null;

Expand All @@ -39,13 +36,24 @@ export interface PaymentLookupSuccessResponse {

/** Indicates whether the transaction has been refunded. */
refunded: boolean;

/** The current status of the transaction. */
status: PaymentStatus;
}

/**
* Represents a specific lookup response for a 'Success' transaction.
*/
export interface PaymentLookupSuccessResponse
extends PaymentLookupGeneralResponse {
status: "Completed"; // Enforce the specific status
}

/**
* Represents a specific lookup response for a 'Pending' transaction.
*/
export interface PaymentLookupPendingResponse
extends PaymentLookupSuccessResponse {
extends PaymentLookupGeneralResponse {
status: "Pending"; // Enforce the specific status
transaction_id: null; // Transaction ID not yet generated
}
Expand All @@ -54,15 +62,15 @@ export interface PaymentLookupPendingResponse
* Represents a specific lookup response for an 'Initiated' transaction.
*/
export interface PaymentLookupInitiatedResponse
extends PaymentLookupSuccessResponse {
extends PaymentLookupGeneralResponse {
status: "Initiated";
}

/**
* Represents a specific lookup response for a 'Refunded' transaction.
*/
export interface PaymentLookupRefundedResponse
extends PaymentLookupSuccessResponse {
extends PaymentLookupGeneralResponse {
status: "Refunded";
refunded: true;
}
Expand All @@ -71,7 +79,7 @@ export interface PaymentLookupRefundedResponse
* Represents a specific lookup response for an 'Expired' transaction.
*/
export interface PaymentLookupExpiredResponse
extends PaymentLookupSuccessResponse {
extends PaymentLookupGeneralResponse {
status: "Expired";
transaction_id: null;
}
Expand All @@ -80,12 +88,13 @@ export interface PaymentLookupExpiredResponse
* Represents a specific lookup response for a 'UserCanceled' transaction.
*/
export interface PaymentLookupCanceledResponse
extends PaymentLookupSuccessResponse {
extends PaymentLookupGeneralResponse {
status: "UserCanceled";
transaction_id: null;
}

export type AllLookupResponse =
| PaymentLookupSuccessResponse
| PaymentLookupPendingResponse
| PaymentLookupInitiatedResponse
| PaymentLookupRefundedResponse
Expand Down
131 changes: 101 additions & 30 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import axios from "axios";
import axios, { AxiosInstance } from "axios";
import type {
InitiatePaymentRequest,
InitiatePaymentResponse,
} from "./epaymentv2/initiatePayment";
import type { AllLookupResponse } from "./epaymentv2/paymentLookup";
import type {
AllLookupResponse,
PaymentLookupRequest,
} from "./epaymentv2/paymentLookup";

async function awTry<T>(promise: unknown) {
try {
Expand All @@ -15,36 +18,104 @@ async function awTry<T>(promise: unknown) {
}
}

const khaltiInstanceServer = axios.create({
baseURL: "https://a.khalti.com/api/v2",
headers: {
"Content-Type": "application/json",
Authorization: `Key f79110302bbf46828fca3a7b7514da97`, // live secret key
},
responseType: "json",
});

const paymentLookup = async (pidx: string) => {
const [response, error] = await awTry<{ data: AllLookupResponse }>(
khaltiInstanceServer.get(`/epayment/lookup?pidx=${pidx}`)
);
if (error) {
console.log("Error, in lookupPayment: ", error);
}
return response.data;
const paymentLookup = (instance: AxiosInstance) => {
return async ({ pidx }: PaymentLookupRequest) => {
const [response, error] = await awTry<{ data: AllLookupResponse }>(
instance.get(`/epayment/lookup?pidx=${pidx}`)
);
if (error) {
console.log("Error, in lookupPayment: ", error);
}
return response.data;
};
};

const initiatePayment = async (payload: InitiatePaymentRequest) => {
const [response, error] = await awTry<{ data: InitiatePaymentResponse }>(
khaltiInstanceServer.post("/epayment/initiate/", payload)
);
if (error) {
console.log("Error, in initiatePayment: ", error);
}
return response.data;
const initiatePayment = (instance: AxiosInstance) => {
return async (payload: InitiatePaymentRequest) => {
const [response, error] = await awTry<{ data: InitiatePaymentResponse }>(
instance.post("/epayment/initiate/", payload)
);

if (error) {
console.log("Error, in initiatePayment: ", error);
}
return response.data;
};
};

export const khaltiApi = {
initiatePayment,
paymentLookup,
export const createKhaltiInstance = ({
env,
secretKey,
}: {
env: "sandbox" | "production";
secretKey: string;
}) => {
const urls = {
sandbox: "https://a.khalti.com/api/v2",
production: "https://khalti.com/api/v2",
};
const instance = axios.create({
baseURL: urls[env],
headers: {
"Content-Type": "application/json",
Authorization: `Key ${secretKey}`, // live secret key
},
responseType: "json",
});

return {
paymentLookup: paymentLookup(instance),
initiatePayment: initiatePayment(instance),
};
};

// const ins = createKhaltiInstance({
// env: "sandbox",
// secretKey: "Asd",
// });

// async function a() {
// const { payment_url, pidx } = await ins.initiatePayment({
// return_url: "https://example.com/payment/",
// website_url: "https://example.com/",
// amount: 1300,
// purchase_order_id: "test12",
// purchase_order_name: "test",
// customer_info: {
// name: "Khalti Bahadur",
// email: "example@gmail.com",
// phone: "9800000123",
// },
// amount_breakdown: [
// {
// label: "Mark Price",
// amount: 1000,
// },
// {
// label: "VAT",
// amount: 300,
// },
// ],
// product_details: [
// {
// identity: "1234567890",
// name: "Khalti logo",
// total_price: 1300,
// quantity: 1,
// unit_price: 1300,
// },
// ],
// merchant_username: "merchant_name",
// merchant_extra: "merchant_extra",
// });

// const ff = await ins.paymentLookup({
// pidx,
// });

// if (ff.status === "Completed") {
// console.log("Payment Success");
// }

// console.log({ payment_url, pidx });
// }

0 comments on commit c7389e1

Please sign in to comment.