ptrajectory / payments Goto Github PK
View Code? Open in Web Editor NEWA mobile money payment intergration sdk.
Home Page: https://payments.ptrajectory.com
A mobile money payment intergration sdk.
Home Page: https://payments.ptrajectory.com
Changes made in pr #17
const client = createClient(the_hosted_url, anonymous_key)
client.from("quotes").select()
this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, { // ๐ your url's getting passed in here
headers: this.headers, // ๐ includes your supabase key as an Authorization header
schema: settings.db?.schema,
fetch: this.fetch,
})
const client = new PaymentsClient(your_url, your_secret_or_publishable_key)
export const createPaymentClient = (url: string, secretKey:string ) => {
if(isUndefined(url) || url?.length === 0) throw new Error("INVALID URL")
if(isUndefined(secretKey) || secretKey?.length === 0) throw new Error("INVALID URL")
const client = new PaymentsHttpClient()
client.url = url
client.secretKey = secretKey
return {
/**
* Interact with payments
*/
payments: new Payment(client),
/**
* Interact with products
*/
products: new Product(client),
/**
* Interact with customers
*/
customers: new Customer(client),
/**
* Interact with payment methods
*/
paymentMethods: new PaymentMethod(client),
/**
* Interact with carts
*/
carts: new Cart(client),
/**
* Interact with checkouts
*/
checkouts: new Checkout(client)
}
}
const A_TEST_KEY = "test_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiVEVTVElORyBTVE9SRSIsImlkIjoic3RyX2Y0ODQzNTg1NGFiODYzZTBmZWQ5MTZlZmU5OTdkMmFmIiwic2VsbGVyX2lkIjoic2xsX2UwMzRlN2UxYWYwMDFiYTdmNjAxYjJlZWY4ZmE1OWZkIiwiZW52IjoidGVzdGluZyIsImlhdCI6MTY5NDA5OTc5NX0.UBodqZrttDIRUW3TXObwT6jwsFP1ns9iDsPvxGXXq0Y"
there are 2 different kinds of keys i.e secret_key
(only to be used server side) and publishable key
(can be used client side)
These two are generated and signed differently
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
})
export function generatePublishableKey(store: STORE, env: environment){
const privateKey = fs.readFileSync("./keys/privateKey.pem", {
encoding: 'utf-8'
})
const publishableKey = jwt.sign({
...store,
env
}, privateKey, {
algorithm: 'RS256'
})
return publishableKey
}
export function verifyPublishableKey(token: string): Promise<STORE & { env: environment } >{
const publicKey = fs.readFileSync("./keys/publicKey.pem",{
encoding: 'utf-8'
})
return new Promise((res, rej)=>{
jwt.verify(token, publicKey, (err, store)=>{
if(err) return rej("Unable to verify")
return res(store as any)
})
})
}
randomBytes(32).toString('base64')
used for signing and verifying
Middleware is used to control access to the resources
export const generateCheckoutEphemeralKey = (checkout_id: string) => {
const privateKey = fs.readFileSync("./keys/privateKey.pem", {
encoding: 'utf-8'
})
const publishableKey = jwt.sign({
checkout_id
}, privateKey, {
algorithm: 'RS256'
})
return publishableKey
}
pages -> app
introduction of new file naming conventions under the app directory:
FILE CONVENTIONS
within the dir you can create sub components of the folder which can allow you to put any page sections within the same directory as the page.
caching
// This will be automatically cached if used in a server component / route handler / server action
const data = fetch("https://kanye.rest")
const data = cache(await db.query.CUSTOMERS.findMany())
server component
"use server"
const getQuote = async () => {
const q = await db.QUOTES.findMand()
return q
}
const MyComponent = async () => {
const quote = await getQuote()
return (
<h1>
{ quoute.title }
<h2>
)
}
route handlers
initially you would export a single handler function from
defining different handlers for different http verbs separately
export const GET = async (request: Request,
params: { customer_slug?: string[] }
) => {
const { customer_slug } = params
const { userId, user } = auth()
if(isEmpty(userId) || isUndefined(userId)) return NextResponse.json(generate_dto(null, "UNAUTHORIZED", 'error'), {
status: 401
})
....some_hidden_code
}
"use server"
export const fetch_home_daily_purchases_chart_data= async (store_id: string, range: Partial<{from: string, to: string}> | undefined ): Promise<Array<ChartData>> => {
try {
// vvalidate the start and end dates
const { from = get_today_start().toISOString(), to = get_today_end().toISOString() } = range ? range : {}
// get all the payments between those dates
let all_payments: Array<tPAYMENT & { created_at?: string }> | null = (await db.select().from(PAYMENT).where(
and(
gt(PAYMENT.created_at, new Date(from)),
lt(PAYMENT.created_at, new Date(to))
),
)) ?? null
const date_categories = getAllDaysBetweenDates(from, to)
// transform the data into a format that can go into the chart
const date_categorized_chart_data = date_categories?.val?.map((date)=>{
const payment_aggregation = all_payments?.reduce((prev, cur, i)=>{
if(date_categories?.is_same_date) {
if(isSameHour(date, cur.created_at ?? new Date())) return prev + (cur?.amount ?? 0)
return prev + 0
}
if(isSameDate(date, cur.created_at ?? new Date())) return prev + (cur?.amount ?? 0)
return prev + 0
}, 0) ?? 0
return {
day: dayjs(date).format(
date_categories?.is_same_date ? "hh A" :"MMM D"
),
total_amount_spent: payment_aggregation
}
})
return date_categorized_chart_data
}
catch (e)
{
console.log("SOMETHING WENT WRONG::", e)
//TODO: add better error handling
return []
}
}
const POST = async (request: Request) {
const formData = await request.formData()
....other interesting stuff
}
```
--------------------
--------------------
- importing server component in a client component won't work but you can pass a server component as a prop to a client component
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.