# ptrajectory-payments-node

> This guide will walk you through the basic usage of the `ptrajectory-payments-node` package with an Express server.

Latest version **0.1.0** (published 2023-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install ptrajectory-payments-node
pnpm add ptrajectory-payments-node
yarn add ptrajectory-payments-node
bun add ptrajectory-payments-node
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-09-15 |
| First published | 2023-09-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 124.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | porkytheblack https://github.com/porkytheblack |
| Maintainers | porkytheblack |

## Links

- npm: https://www.npmjs.com/package/ptrajectory-payments-node
- Repository: https://github.com/ptrajectory/payments
- Homepage: https://github.com/ptrajectory/payments#readme
- Issues: https://github.com/ptrajectory/payments/issues
- npm.io page: https://npm.io/package/ptrajectory-payments-node

## Dependencies (4)

- [zod](https://npm.io/package/zod.md) ^3.21.4
- [dotenv](https://npm.io/package/dotenv.md) ^16.1.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [zodiac](https://npm.io/package/zodiac.md) 1.0.0

## Recent versions

- 0.1.0 (latest) — 2023-09-15

## README

# Using Ptrajectory Payments Node Package with Express

This guide will walk you through the basic usage of the `ptrajectory-payments-node` package with an Express server.

#### 📝 This library is dependant on an api that is still under development, it isn't ready for usage!!

## Installation

First, ensure you have the package installed:

```bash
npm install ptrajectory-payments-node
```

## Setup

Import the necessary modules and initialize with your host url and publishable key.

```ts
import { createPaymentClient } from "ptrajectory-payments-node";

const paymentsClient = createPaymentClient(process.env.URL, process.env.PUBLISHABLE_KEY);
```


## How to use 

After the initial setup you'll be able to interact with  payments, products, carts, checkouts and customer objects:


```ts

const new_customer = await paymentsClient.customers.create(the_customer_object)

const new_product = await paymentsClient.products.create(the_product_object)

```

You can also initialize payments

```ts

const payment_reference = await paymentsClient.payments.start({
    ...payment related detail {refer to types}
})
```

Confirm if a payment was successfully, which uses polling under the hood to check consistently for about a minute before erroring out.

```ts
const payment_status = await paymentsClient.payments.confirm(payment_reference.id)

if(payment_status === "SUCCESS") {
    // do something
}
else
{
    // do something else
}
```


## Express example

```ts
import 'dotenv/config'
import express from "express"
import morgan from "morgan"
import { createPaymentClient } from "ptrajectory-payments-node"

const app = express()
app.use(express.json())
app.use(morgan("combined"))



const paymentsClient = createPaymentClient(process.env.URL, process.env.PUBLISHABLE_KEY)

// Create a customet
app.post("/customer", async (req, res)=>{
    const body = req.body
    const new_customer = await paymentsClient.customers.create(body)

    return res.status(201).send(new_customer)
})

// get a customer
app.get("/customer/:customer_id", async (req, res) => {
    const id = req.params.customer_id

    const the_customer = await paymentsClient.customers.retrieve(id)

    return res.status(200).send(the_customer)
})

// update a customer
app.put("/customers/:customer_id", async (req, res)=>{
    const id = req.params.customer_id

    const body = req.body

    const the_updated_customer = await paymentsClient.customers.update(id, body)

    return res.status(200).send(the_updated_customer)
})

// archive a customer
app.patch("/customers/:customer_id", async (req, res)=>{

    const id = req.params.customer_id

    const the_archived_customer = await paymentsClient.customers.archive(id) 

    return res.status(200).send(the_archived_customer)

})

app.post("/products", async (req, res)=>{

    const body = req.body

    const new_product = await paymentsClient.products.create(body)

    return res.status(201).send(new_product)

})

app.get("/products/:product_id", async (req, res) =>{

    const id = req.params.product_id

    const the_product = await paymentsClient.products.retrieve(id)

    return res.status(200).send(the_product)

})

app.put("/products/:product_id", async (req, res)=>{

    const id = req.params.product_id 

    const body = req.body 

    const the_updated_product = await paymentsClient.products.update(id, body)

    return res.status(200).send(the_updated_product)

})

app.patch("/products/:product_id", async (req, res)=>{

    const id = req.params.product_id

    const the_archived_product = await paymentsClient.products.archive(id)

    return res.status(200).send(the_archived_product)

})

app.post("/carts", async (req, res)=>{

    const new_cart = await paymentsClient.carts.create(req.body)

    return res.status(201).send(new_cart)

})

app.put("/carts/:cart_id", async (req, res)=>{

    const updated_cart = await paymentsClient.carts.update(req.params.cart_id, req.body)

    return res.status(200).send(updated_cart)

})

app.post("/carts/:cart_id", async (req, res)=>{

    const new_cart_item = await paymentsClient.carts.addCartItem(req.params.cart_id, req.body)


    return res.status(200).send(new_cart_item)

})


app.put("/carts/:cart_id/:cart_item_id", async (req, res)=>{

    const updated_cart_item = await paymentsClient.carts.updateCartItem(req.params.cart_id, req.params.cart_item_id, req.body)

    return res.status(200).send(updated_cart_item)

})

app.delete("/carts/:cart_id/:cart_item_id", async (req, res)=>{
    const { cart_id, cart_item_id } = req.params
    const deleted_item =  await paymentsClient.carts.deleteCartItem(cart_id, cart_item_id)

    return res.status(200).send(deleted_item)

})


// .... you get the idea



app.listen(8090, ()=>{
    console.log("🚀 Huston we have ignition.")
})
```


-------



## Development Notice

Please be aware that this library is currently under active development. I am working hard to make this library as robust, user-friendly, and feature-complete as possible.

As I continue to improve and expand the library, you may notice regular updates. These will often include bug fixes, performance improvements, and even new features. I strongly recommend keeping up to date with the latest versions to benefit from these improvements.

Please rest assured that every update is thoroughly tested to ensure compatibility and minimize any potential disruptions.

My goal is to provide you with a stable, reliable, and efficient tool for interacting with the Mpesa API. I appreciate your support and patience as I continue to enhance the library.

---
_Source: https://npm.io/package/ptrajectory-payments-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
