Files
planetposen-backend/src/stripe/stripeApi.ts
Kevin a28b47413a Improvements to order, product, checkout & added shipping (#2)
* planet_id variable casing consistent with database field

Renames all clientId variables to planet_id

* Store stripe json response from latest payment & charge webhook

Also added db seed file for stripe payments

* Image support! Can now add, update & delete images from products

Images moved away from product schema to it's own table.
Accepts a images string and relates it to a product. Does not store the
images or verify the existance.

* Instead of deleting a product set field unlisted to true

Endpoints with current inventory checks for unlisted = false

* order_id & customer_no gets enhanced base64 function for generating id

* Implemented shipping for orders using Post tracking api

Added CRUD for making changes to a order's shippment.
Split shipping table into shipment_courier, shipment & shipment_event.

* Updated and add product & product_sku functions updated

* Cart increment funciton checks stock before updating

* Endpoint for getting product audit log using 91pluss trigger

Read more about usage here: https://wiki.postgresql.org/wiki/Audit_trigger_91plus

* On stripe charge successfull send email to user with planetposen-mail

* More product seed data, linting & formatting

* Log file at /var/log/planetposen_logs & rotate max 3 files 100MB each

This will prob throw a error if folder does not exist, run: `(sudo)
mkdir -p /var/log/planetposen_logs`.

* All endpoints now prefixed with /api/v1

* Linting
2022-12-29 19:25:39 +01:00

70 lines
1.5 KiB
TypeScript

import Stripe from "stripe";
import type ICustomer from "../interfaces/ICustomer";
/**
* Does calls to stripe API
*/
class StripeApi {
publicKey: string;
secretKey: string;
stripe: Stripe;
constructor(publicKey: string, secretKey: string) {
this.publicKey = publicKey;
this.secretKey = secretKey;
this.stripe = new Stripe(this.secretKey, {
apiVersion: "2022-08-01",
});
}
async createPaymentIntent(
planet_id: string,
total: number,
orderId: string,
customer: ICustomer
): Promise<Stripe.Response<Stripe.PaymentIntent>> {
const stripeCustomer = await this.createCustomer(planet_id, customer);
const paymentIntent = await this.stripe.paymentIntents.create({
customer: stripeCustomer?.id,
amount: total * 100,
currency: "NOK",
shipping: {
name: stripeCustomer.name,
address: stripeCustomer.address,
},
metadata: {
planet_id,
orderId,
},
});
return paymentIntent;
}
async createCustomer(planet_id: string, customer: ICustomer) {
return await this.stripe.customers.create({
email: customer.email,
name: `${customer.first_name} ${customer.last_name}`,
address: {
city: customer.city,
line1: customer.street_address,
postal_code: String(customer.zip_code),
},
metadata: {
planet_id,
},
});
}
createProduct() {
return;
}
async createShipping() {
return;
}
}
export default StripeApi;