Skip to content

Commit

Permalink
Merge pull request #10 from Ahdeyyy/miscellaneous
Browse files Browse the repository at this point in the history
Miscellaneous
  • Loading branch information
Ahdeyyy authored Apr 18, 2024
2 parents 5ce0506 + 86e659d commit 014afdd
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cyan-cherries-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ahdeyy/paystack": patch
---

added miscellaneous endpoint
33 changes: 33 additions & 0 deletions lib/miscellaneous/miscellaneous.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Paystack } from "../paystack";
import { test, mock, expect } from "bun:test"


const paystack = mock(() => new Paystack(import.meta.env.PAYSTACK_SECRET_KEY ?? ""))

test("list banks", async () => {
const response = await paystack().miscellaneous.list_banks({ country: "nigeria", use_cursor: true, perPage: 40 })
expect(response.status).toBe(true)

if (response.status) {
expect(response.data).toBeInstanceOf(Array)
expect(response.meta.perPage).toBe(40)
}
})

test("list countries", async () => {
const response = await paystack().miscellaneous.list_countries()
expect(response.status).toBe(true)

if (response.status) {
expect(response.data).toBeInstanceOf(Array)
}
})

test("list states", async () => {
const response = await paystack().miscellaneous.list_states("US")
expect(response.status).toBe(true)

if (response.status) {
expect(response.data).toBeInstanceOf(Array)
}
})
60 changes: 60 additions & 0 deletions lib/miscellaneous/miscellaneous.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { ListBanksQuery, ListBanksResponse, ListCountryResponse, ListStatesResponse } from "./types";

export class Miscellaneous {
private secret_key: string;
constructor(secret_key: string) {
this.secret_key = secret_key;
}

/** Get a list of all supported banks and their properties */

async list_banks(query: ListBanksQuery): Promise<ListBanksResponse> {
const url = new URL("https://api.paystack.co/bank")
const keys = Object.keys(query)

for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (!key) continue
const value = query[key]
url.searchParams.set(key, value)
}

const response = await fetch(url, {
headers: this.get_headers(),
method: "GET"
})
const response_data = await response.json() as ListBanksResponse
return response_data
}

/** Gets a list of countries that Paystack currently supports */
async list_countries(): Promise<ListCountryResponse> {
const url = "https://api.paystack.co/country"
const response = await fetch(url, {
headers: this.get_headers(),
method: "GET"
})
const response_data = await response.json() as ListCountryResponse
return response_data
}


/** Get a list of states for a country for address verification */
async list_states(country: string): Promise<ListStatesResponse> {
const url = new URL("https://api.paystack.co/address_verification/states")
url.searchParams.set("country", country)

const response = await fetch(url, {
headers: this.get_headers(),
method: "GET"
})
const response_data = await response.json() as ListStatesResponse
return response_data
}
private get_headers() {
return {
"Content-Type": "application/json",
"Authorization": "Bearer " + this.secret_key
}
}
}
111 changes: 111 additions & 0 deletions lib/miscellaneous/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import type { Currency, PaystackResponseError } from "../types";

export type ListBanksQuery = {
/** The country from which to obtain the list of supported banks. e.g country=ghana or country=nigeria */
country: string;
/** Flag to enable cursor pagination on the endpoint */
use_cursor: boolean;

/** The number of objects to return per page. Defaults to 50, and limited to 100 records per page. */
perPage: number;


/** A flag to filter for banks a customer can pay directly from */
pay_with_bank?: boolean;

/** A flag to filter the banks that are supported for account
* verification in South Africa.
* You need to combine this with either the currency or country filter.
* */
enabled_for_verification?: boolean;

/** A cursor that indicates your place in the list. It can be used to fetch the next page of the list */
next?: string;

/** A cursor that indicates your place in the list.
* It should be used to fetch the previous page of the list after an intial next request
* */
previous?: string;

/** The gateway type of the bank. It can be one of these: [emandate, digitalbankmandate] */
gateway?: string;

/** Type of financial channel.
* For Ghanaian channels, please use either mobile_money for mobile money channels OR ghipps for bank channels
* */
type?: string;

/** One of the supported currency */
currency?: Currency;

[key: string]: any;
}

type BankData = {
name: string;
slug: string;
code: string;
longcode: string;
// TODO: unknown type
gateway: any | null;
pay_with_bank: boolean;
active: boolean;
is_deleted: boolean;
country: string;
currency: Currency;
type: string;
id: number;
createdAt: string;
updatedAt: string;
};

type Meta = {
next: string | null;
previous: string | null;
perPage: number;
};

export type ListBanksResponse = { message: string } & ({
status: true;
data: BankData[];
meta: Meta;
} | PaystackResponseError)

type IntegrationType = string;

type PaymentMethod = string;

type RelationshipData = {
type: string;
data: Array<(string | Currency | IntegrationType | PaymentMethod)>;
};

type Relationships = {
currency: RelationshipData;
integration_feature: RelationshipData;
integration_type: RelationshipData;
payment_method: RelationshipData;
};

type CountryData = {
id: number;
name: string;
iso_code: string;
default_currency_code: string;
integration_defaults: Object;
relationships: Relationships;
};

export type ListCountryResponse = { message: string } & ({
status: true;
data: CountryData[];
} | PaystackResponseError)

export type ListStatesResponse = { message: string } & ({
status: true;
data: Array<{
name: string;
slug: string;
abbreviation: string;
}>
} | PaystackResponseError)
4 changes: 3 additions & 1 deletion lib/paystack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Customer } from "./customer/customer";
import { Product } from "./product/product";
import { PaymentRequest } from "./payment request/payment_request";
import { DedicatedVirtualAccounts } from "./dedicated virtual accounts/dva";
import { Miscellaneous } from "./miscellaneous/miscellaneous"

export class Paystack {
private secret_key: string;
private endpoint = "https://api.paystack.co";
payment_request: PaymentRequest;
customer: Customer;
product: Product;
miscellaneous: Miscellaneous;
/** Dedicated virtual accounts */
dva: DedicatedVirtualAccounts;
constructor(secret_key: string) {
Expand All @@ -17,5 +18,6 @@ export class Paystack {
this.customer = new Customer(this.secret_key);
this.product = new Product(this.secret_key);
this.dva = new DedicatedVirtualAccounts(this.secret_key);
this.miscellaneous = new Miscellaneous(this.secret_key);
}
}

0 comments on commit 014afdd

Please sign in to comment.