Skip to content

Commit

Permalink
Merge pull request #11 from swikars1/add-types
Browse files Browse the repository at this point in the history
add khalti api calls
  • Loading branch information
swikars1 authored Apr 7, 2024
2 parents 3e3e6f2 + 2db5b55 commit b63fbf1
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 79 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-ladybugs-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"khalti-epayment": minor
---

add khalti api calls
98 changes: 49 additions & 49 deletions epaymentv2/initiatePayment.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
import { khaltiInstanceServer } from "./khaltiInstanceServer";
import { awTry } from "./utils";

/**
* Represents the successful response when initiating a payment.
*/
interface InitiatePaymentResponse {
/**
* A unique request identifier provided upon success. Use this 'pidx'
* for future references to this payment.
*/
pidx: string;

/** The URL to redirect the user to in order to complete the payment. */
payment_url: string;

/** Date and time (likely in ISO format) when the payment link expires. */
expires_at: string;

/** Number of seconds until the payment link expires. */
expires_in: number;
}

/**
* Represents an error response when initiating a payment.
*/
interface InitiatePaymentError {
/** The return URL that was likely invalid or problematic. */
return_url: string[];

/** A fixed error key indicating a validation issue. */
error_key: "validation_error";
}

/**
* Represents the request payload for initiating a payment.
*/
interface InitiatePaymentRequest {
export interface InitiatePaymentRequest {
/** URL where the user will be redirected after completing the transaction */
return_url: string;

Expand All @@ -59,12 +25,15 @@ interface InitiatePaymentRequest {

/** Details about the products or services included in the transaction (optional) */
product_details?: ProductDetail[];

/** Additional merchant-specific information (optional) merchant_info?: MerchantInfo; */
[key: `merchant_${string}`]: any;
}

/**
* Contains customer information for billing purposes.
*/
interface CustomerInfo {
export interface CustomerInfo {
/** Customer's full name */
name: string;

Expand All @@ -78,7 +47,7 @@ interface CustomerInfo {
/**
* Represents a single charge item within the amount breakdown.
*/
interface AmountBreakdownItem {
export interface AmountBreakdownItem {
/** Description of a particular charge within the total amount */
label: string;

Expand All @@ -89,7 +58,7 @@ interface AmountBreakdownItem {
/**
* Holds information about a product included in the transaction.
*/
interface ProductDetail {
export interface ProductDetail {
/** Unique identifier for the product */
identity: string;

Expand All @@ -106,14 +75,45 @@ interface ProductDetail {
unit_price: number;
}

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;
};

export { initiatePayment };
/**
* Represents the successful response when initiating a payment.
*/
export interface InitiatePaymentResponse {
/**
* A unique request identifier provided upon success. Use this 'pidx'
* for future references to this payment.
*/
pidx: string;

/** The URL to redirect the user to in order to complete the payment. */
payment_url: string;

/** Date and time (likely in ISO format) when the payment link expires. */
expires_at: string;

/** Number of seconds until the payment link expires. */
expires_in: number;

/** Additional merchant-specific information (optional) merchant_info?: MerchantInfo; */
[key: `merchant_${string}`]: any;
}

/**
* Represents an error response when initiating a payment.
*/
export interface InitiatePaymentError {
/** The return URL that was likely invalid or problematic. */
return_url: string[];

/** A fixed error key indicating a validation issue. */
error_key: "validation_error";
}

// initiatePayment({
// merchant_asd: 1,
// amount: 12,
// purchase_order_id: "1",
// return_url: "asd",
// website_url: "sad",
// purchase_order_name: "asd",
// })
10 changes: 0 additions & 10 deletions epaymentv2/khaltiInstanceServer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
import axios from "axios";

export const khaltiInstanceServer = axios.create({
baseURL: "https://a.khalti.com/api/v2",
headers: {
"Content-Type": "application/json",
Authorization: `Key f79110302bbf46828fca3a7b7514da97`, // live secret key
},
responseType: "json",
});
28 changes: 20 additions & 8 deletions epaymentv2/paymentLookup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Represents the possible statuses of a payment transaction.
*/
type PaymentStatus =
export type PaymentStatus =
| "Completed"
| "Pending"
| "Refunded"
Expand All @@ -13,15 +13,15 @@ type PaymentStatus =
/**
* Represents the request payload for the payment lookup API.
*/
interface PaymentLookupRequest {
export interface PaymentLookupRequest {
/** The initial payment identifier. */
pidx: string;
}

/**
* Represents the base structure of a successful lookup API response.
*/
interface PaymentLookupSuccessResponse {
export interface PaymentLookupSuccessResponse {
/** The initial payment identifier. */
pidx: string;

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

/**
* Represents a specific lookup response for an 'Initiated' transaction.
*/
interface PaymentLookupInitiatedResponse extends PaymentLookupSuccessResponse {
export interface PaymentLookupInitiatedResponse
extends PaymentLookupSuccessResponse {
status: "Initiated";
}

/**
* Represents a specific lookup response for a 'Refunded' transaction.
*/
interface PaymentLookupRefundedResponse extends PaymentLookupSuccessResponse {
export interface PaymentLookupRefundedResponse
extends PaymentLookupSuccessResponse {
status: "Refunded";
refunded: true;
}

/**
* Represents a specific lookup response for an 'Expired' transaction.
*/
interface PaymentLookupExpiredResponse extends PaymentLookupSuccessResponse {
export interface PaymentLookupExpiredResponse
extends PaymentLookupSuccessResponse {
status: "Expired";
transaction_id: null;
}

/**
* Represents a specific lookup response for a 'UserCanceled' transaction.
*/
interface PaymentLookupCanceledResponse extends PaymentLookupSuccessResponse {
export interface PaymentLookupCanceledResponse
extends PaymentLookupSuccessResponse {
status: "UserCanceled";
transaction_id: null;
}

export type AllLookupResponse =
| PaymentLookupPendingResponse
| PaymentLookupInitiatedResponse
| PaymentLookupRefundedResponse
| PaymentLookupExpiredResponse
| PaymentLookupCanceledResponse;

// Completed - Provide service to user.
// Pending - Hold, do not provide service. And contact Khalti team.
// Refunded - Transaction has been refunded to user. Do not provide service.
Expand Down
2 changes: 1 addition & 1 deletion epaymentv2/paymentSuccessCallback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Represents the data received in the redirect callback URL after a payment attempt.
*/
interface PaymentRedirectResponse {
export interface PaymentRedirectResponse {
/** The initial payment identifier provided during initiation. */
pidx: string;

Expand Down
9 changes: 0 additions & 9 deletions epaymentv2/utils.ts

This file was deleted.

51 changes: 49 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
export const add = (a: number, b: number) => {
return a + b + 4;
import axios from "axios";
import type {
InitiatePaymentRequest,
InitiatePaymentResponse,
} from "./epaymentv2/initiatePayment";
import type { AllLookupResponse } from "./epaymentv2/paymentLookup";

async function awTry<T>(promise: unknown) {
try {
const data = await promise;
return [data, null] as [T, never];
} catch (err) {
console.error(err);
return [null, err] as [never, 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 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;
};

export const khaltiApi = {
initiatePayment,
paymentLookup,
};

0 comments on commit b63fbf1

Please sign in to comment.