diff --git a/.changeset/orange-dragons-repair.md b/.changeset/orange-dragons-repair.md new file mode 100644 index 0000000..63da30a --- /dev/null +++ b/.changeset/orange-dragons-repair.md @@ -0,0 +1,5 @@ +--- +"khalti-epayment": patch +--- + +add builders diff --git a/epaymentv2/khaltiInstanceServer.ts b/epaymentv2/khaltiInstanceServer.ts deleted file mode 100644 index e69de29..0000000 diff --git a/epaymentv2/paymentLookup.ts b/epaymentv2/paymentLookup.ts index 3946b35..71ac8da 100644 --- a/epaymentv2/paymentLookup.ts +++ b/epaymentv2/paymentLookup.ts @@ -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; @@ -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 } @@ -54,7 +62,7 @@ export interface PaymentLookupPendingResponse * Represents a specific lookup response for an 'Initiated' transaction. */ export interface PaymentLookupInitiatedResponse - extends PaymentLookupSuccessResponse { + extends PaymentLookupGeneralResponse { status: "Initiated"; } @@ -62,7 +70,7 @@ export interface PaymentLookupInitiatedResponse * Represents a specific lookup response for a 'Refunded' transaction. */ export interface PaymentLookupRefundedResponse - extends PaymentLookupSuccessResponse { + extends PaymentLookupGeneralResponse { status: "Refunded"; refunded: true; } @@ -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; } @@ -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 diff --git a/index.ts b/index.ts index fb3443b..5942e8d 100644 --- a/index.ts +++ b/index.ts @@ -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(promise: unknown) { try { @@ -15,36 +18,104 @@ async function awTry(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 }); +// }