-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from atlp-rwanda/ft-stripe-payment
Stripe Payment system Integration
- Loading branch information
Showing
11 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,11 @@ import { User, UserInterface } from '../entities/User'; | |
import { v4 as uuid } from 'uuid'; | ||
import { Product } from '../entities/Product'; | ||
import { Category } from '../entities/Category'; | ||
import { Cart } from '../entities/Cart'; | ||
Check warning on line 10 in src/__test__/getProduct.test.ts GitHub Actions / build-lint-test-coverage
Check warning on line 10 in src/__test__/getProduct.test.ts GitHub Actions / build-lint-test-coverage
Check warning on line 10 in src/__test__/getProduct.test.ts GitHub Actions / build-lint-test-coverage
|
||
import { cleanDatabase } from './test-assets/DatabaseCleanup'; | ||
|
||
const vendor1Id = uuid(); | ||
const BuyerID = uuid(); | ||
const product1Id = uuid(); | ||
const Invalidproduct = '11278df2-d026-457a-9471-4749f038df68'; | ||
const catId = uuid(); | ||
|
@@ -37,6 +39,18 @@ const sampleVendor1: UserInterface = { | |
photoUrl: 'https://example.com/photo.jpg', | ||
role: 'VENDOR', | ||
}; | ||
const sampleBuyer1: UserInterface = { | ||
id: BuyerID, | ||
firstName: 'vendor1o', | ||
lastName: 'user', | ||
email: '[email protected]', | ||
password: 'password', | ||
userType: 'Vendor', | ||
gender: 'Male', | ||
phoneNumber: '000380996348', | ||
photoUrl: 'https://example.com/photo.jpg', | ||
role: 'BUYER', | ||
}; | ||
|
||
const sampleCat = { | ||
id: catId, | ||
|
@@ -53,23 +67,23 @@ const sampleProduct1 = { | |
vendor: sampleVendor1, | ||
categories: [sampleCat], | ||
}; | ||
|
||
let cardID : string; | ||
beforeAll(async () => { | ||
const connection = await dbConnection(); | ||
|
||
const categoryRepository = connection?.getRepository(Category); | ||
await categoryRepository?.save({ ...sampleCat }); | ||
|
||
const userRepository = connection?.getRepository(User); | ||
await userRepository?.save({ ...sampleVendor1 }); | ||
await userRepository?.save({ ...sampleVendor1}); | ||
await userRepository?.save({ ...sampleBuyer1 }); | ||
|
||
const productRepository = connection?.getRepository(Product); | ||
await productRepository?.save({ ...sampleProduct1 }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cleanDatabase(); | ||
|
||
server.close(); | ||
}); | ||
|
||
|
@@ -122,3 +136,23 @@ describe('Get single product', () => { | |
expect(response.body.message).toBe('Product not found'); | ||
}, 10000); | ||
}); | ||
describe('Cart Order and payment functionalities', () => { | ||
it('should create a cart for a product', async () => { | ||
const productId = product1Id; | ||
const quantity = 8; | ||
|
||
const token = getAccessToken(BuyerID, sampleBuyer1.email); | ||
|
||
const response = await request(app) | ||
.post('/cart') | ||
.set('Authorization', `Bearer ${token}`) | ||
.send({ productId, quantity }); | ||
|
||
|
||
expect(response.status).toBe(201); | ||
expect(response.body.data.cart).toBeDefined(); | ||
cardID = JSON.stringify(response.body.data.cart.id) | ||
}); | ||
|
||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,4 +58,4 @@ export class Transaction { | |
|
||
@UpdateDateColumn() | ||
updatedAt!: Date; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Request, Response } from 'express'; | ||
import { Cart } from '../../entities/Cart'; // Import your Cart entity | ||
import { Order } from '../../entities/Order'; // Import your Order entity | ||
import { getRepository, getTreeRepository } from 'typeorm'; | ||
import dotenv from 'dotenv'; | ||
import Stripe from 'stripe'; | ||
dotenv.config(); | ||
const stripeInstance = new Stripe(process.env.STRIPE_SECRET_KEY as string, { | ||
apiVersion: "2024-04-10", | ||
}); | ||
|
||
export const confirmPayment = async (req: Request, res: Response) => { | ||
try { | ||
const { payment_method } = req.body; | ||
const cartId = req.params.cartId; // Get the cart ID from the params | ||
|
||
const cartRepository = getRepository(Cart); | ||
const orderRepository = getTreeRepository(Order) | ||
const cart = await cartRepository.findOne({where: {id : cartId}}); | ||
if (!cart) { | ||
return res.status(404).json({ error: 'Cart not found.' }); | ||
} | ||
const order = await orderRepository.findOne({ where: { buyer: cart.user } }); | ||
if (!order) { | ||
return res.status(404).json({ error: 'order not found.' }); | ||
} | ||
|
||
const paymentIntent = await stripeInstance.paymentIntents.create({ | ||
amount: cart.totalAmount, // Convert total to cents | ||
currency: 'usd', | ||
description: `Order #${cartId}`, | ||
return_url: 'https://frontend-website.com/success', | ||
confirm: true, | ||
payment_method, | ||
}); | ||
|
||
order.orderStatus = 'awaiting shipment'; | ||
await orderRepository.save(order); | ||
|
||
|
||
if (paymentIntent.status === 'succeeded') { | ||
// Payment succeeded | ||
res.status(200).json({ message: 'Payment successful!' }); | ||
} else { | ||
// Payment failed | ||
res.status(400).json({ error: 'Payment failed.' }); | ||
} | ||
} catch (error) { | ||
console.error('Error confirming payment:', error); | ||
res.status(500).json({ error: 'Something went wrong' }); | ||
} | ||
}; |