-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Ahdeyyy/miscellaneous
Miscellaneous
- Loading branch information
Showing
5 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ahdeyy/paystack": patch | ||
--- | ||
|
||
added miscellaneous endpoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters