Cart, order, warehouse, product, customer & stripe payment files

This commit is contained in:
2022-12-11 18:19:19 +01:00
parent b636f86151
commit 2dbbd7914d
10 changed files with 1313 additions and 0 deletions

69
src/stripe/stripeApi.ts Normal file
View File

@@ -0,0 +1,69 @@
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(
clientId: string,
total: number,
orderId: string,
customer: ICustomer
): Promise<Stripe.Response<Stripe.PaymentIntent>> {
const stripeCustomer = await this.createCustomer(clientId, customer);
const paymentIntent = await this.stripe.paymentIntents.create({
customer: stripeCustomer?.id,
amount: total * 100,
currency: "NOK",
shipping: {
name: stripeCustomer.name,
address: stripeCustomer.address,
},
metadata: {
clientId,
orderId,
},
});
return paymentIntent;
}
async createCustomer(clientId: 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: {
clientId,
},
});
}
createProduct(cart) {
return;
}
async createShipping() {
return;
}
}
export default StripeApi;

View File

@@ -0,0 +1,87 @@
import establishedDatabase from "../database";
import Configuration from "../config/configuration";
import StripeApi from "./stripeApi";
import Stripe from "stripe";
import type ICustomer from "../interfaces/ICustomer";
const configuration = Configuration.getInstance();
const stripeApi = new StripeApi(
configuration.get("stripe", "publicKey"),
configuration.get("stripe", "secretKey")
);
class StripeRepository {
database: typeof establishedDatabase;
constructor(database = establishedDatabase) {
this.database = database || establishedDatabase;
}
commitPaymentToDatabase(
orderId: string,
payload: Stripe.Response<Stripe.PaymentIntent>
) {
const query = `
INSERT INTO stripe_payments
(order_id, amount, stripe_initiation_response, stripe_transaction_id, stripe_status)
VALUES ($1,$2,$3,$4,$5)`;
return this.database.query(query, [
orderId,
payload.amount,
payload,
payload.id,
payload.status,
]);
}
updatePaymentIntent(payload: Stripe.Response<Stripe.PaymentIntent>) {
const query = `
UPDATE stripe_payments
SET stripe_status = $2, amount_received = $3, updated = $4
WHERE order_id = $1`;
return this.database.update(query, [
payload.metadata.orderId,
payload.status,
payload.amount_received,
new Date(),
]);
}
updatePaymentCharge(payload: Stripe.Response<Stripe.Charge>) {
const query = `
UPDATE stripe_payments
SET stripe_status = $2, amount_captured = $3, amount_refunded = $4, updated = $5
WHERE order_id = $1
`;
return this.database.update(query, [
payload.metadata.orderId,
payload.status,
payload.amount_captured,
payload.amount_refunded,
new Date(),
]);
}
async createPayment(
clientId: string,
total: number,
orderId: string,
customer: ICustomer
) {
const paymentIntent = await stripeApi.createPaymentIntent(
clientId,
total,
orderId,
customer
);
return this.commitPaymentToDatabase(orderId, paymentIntent).then(
() => paymentIntent.client_secret
);
}
}
export default StripeRepository;