mirror of
https://github.com/KevinMidboe/planetposen-backend.git
synced 2026-01-26 02:55:28 +00:00
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
This commit is contained in:
@@ -19,12 +19,12 @@ class StripeApi {
|
||||
}
|
||||
|
||||
async createPaymentIntent(
|
||||
clientId: string,
|
||||
planet_id: string,
|
||||
total: number,
|
||||
orderId: string,
|
||||
customer: ICustomer
|
||||
): Promise<Stripe.Response<Stripe.PaymentIntent>> {
|
||||
const stripeCustomer = await this.createCustomer(clientId, customer);
|
||||
const stripeCustomer = await this.createCustomer(planet_id, customer);
|
||||
const paymentIntent = await this.stripe.paymentIntents.create({
|
||||
customer: stripeCustomer?.id,
|
||||
amount: total * 100,
|
||||
@@ -34,7 +34,7 @@ class StripeApi {
|
||||
address: stripeCustomer.address,
|
||||
},
|
||||
metadata: {
|
||||
clientId,
|
||||
planet_id,
|
||||
orderId,
|
||||
},
|
||||
});
|
||||
@@ -42,7 +42,7 @@ class StripeApi {
|
||||
return paymentIntent;
|
||||
}
|
||||
|
||||
async createCustomer(clientId: string, customer: ICustomer) {
|
||||
async createCustomer(planet_id: string, customer: ICustomer) {
|
||||
return await this.stripe.customers.create({
|
||||
email: customer.email,
|
||||
name: `${customer.first_name} ${customer.last_name}`,
|
||||
@@ -52,7 +52,7 @@ class StripeApi {
|
||||
postal_code: String(customer.zip_code),
|
||||
},
|
||||
metadata: {
|
||||
clientId,
|
||||
planet_id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import establishedDatabase from "../database";
|
||||
import Configuration from "../config/configuration";
|
||||
import StripeApi from "./stripeApi";
|
||||
import Stripe from "stripe";
|
||||
import logger from "../logger";
|
||||
import type ICustomer from "../interfaces/ICustomer";
|
||||
|
||||
const configuration = Configuration.getInstance();
|
||||
@@ -38,46 +39,59 @@ class StripeRepository {
|
||||
updatePaymentIntent(payload: Stripe.Response<Stripe.PaymentIntent>) {
|
||||
const query = `
|
||||
UPDATE stripe_payments
|
||||
SET stripe_status = $2, amount_received = $3, updated = $4
|
||||
SET stripe_status = $2, amount_received = $3, updated = $4, stripe_payment_response = $5
|
||||
WHERE order_id = $1`;
|
||||
|
||||
logger.info("Updating stripe payment intent", { payment_intent: payload });
|
||||
|
||||
return this.database.update(query, [
|
||||
payload.metadata.orderId,
|
||||
payload.status,
|
||||
payload.amount_received,
|
||||
new Date(),
|
||||
payload,
|
||||
]);
|
||||
}
|
||||
|
||||
updatePaymentCharge(payload: Stripe.Response<Stripe.Charge>) {
|
||||
const query = `
|
||||
UPDATE stripe_payments
|
||||
SET stripe_status = $2, amount_captured = $3, amount_refunded = $4, updated = $5
|
||||
SET stripe_status = $2, amount_captured = $3, amount_refunded = $4, updated = $5, stripe_charge_response = $6
|
||||
WHERE order_id = $1
|
||||
`;
|
||||
|
||||
logger.info("Updating stripe payment charge", { payment_charge: payload });
|
||||
|
||||
return this.database.update(query, [
|
||||
payload.metadata.orderId,
|
||||
payload.status,
|
||||
payload.amount_captured,
|
||||
payload.amount_refunded,
|
||||
new Date(),
|
||||
payload,
|
||||
]);
|
||||
}
|
||||
|
||||
async createPayment(
|
||||
clientId: string,
|
||||
planet_id: string,
|
||||
total: number,
|
||||
orderId: string,
|
||||
customer: ICustomer
|
||||
) {
|
||||
const paymentIntent = await stripeApi.createPaymentIntent(
|
||||
clientId,
|
||||
planet_id,
|
||||
total,
|
||||
orderId,
|
||||
customer
|
||||
);
|
||||
|
||||
logger.info("Payment intent from stripe", {
|
||||
payment_intent: paymentIntent,
|
||||
planet_id,
|
||||
order_id: orderId,
|
||||
customer_no: customer.customer_no,
|
||||
});
|
||||
|
||||
return this.commitPaymentToDatabase(orderId, paymentIntent).then(
|
||||
() => paymentIntent.client_secret
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user