-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad290c0
commit ae59990
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,44 @@ | ||
const express = require('express'); | ||
const Stripe = require('stripe'); | ||
const router = express.Router(); | ||
|
||
const stripe = new Stripe('process.env.STRIPE_SECRET_KEY', { | ||
apiVersion: '2020-08-27', | ||
}); | ||
|
||
router.post('/create-session', async (req, res) => { | ||
const { amount = 500, product = 'test', quantity = 1 } = req.body; | ||
|
||
// Validate that amount, product, and quantity are provided in the request | ||
if (!amount || !product || !quantity) { | ||
return res.status(400).json({ error: 'Amount, product, and quantity are required.' }); | ||
} | ||
|
||
try { | ||
const session = await stripe.checkout.sessions.create({ | ||
payment_method_types: ['card'], | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency: 'usd', | ||
product_data: { | ||
name: product, | ||
}, | ||
unit_amount: amount, // Amount should be in cents, e.g., $50 = 5000 | ||
}, | ||
quantity: quantity, | ||
}, | ||
], | ||
mode: 'payment', | ||
success_url: `${req.headers.origin}/success?session_id={CHECKOUT_SESSION_ID}`, | ||
cancel_url: `${req.headers.origin}/cancel`, | ||
}); | ||
|
||
res.json({ id: session.id }); | ||
} catch (error) { | ||
console.error('Error creating checkout session:', error); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
module.exports = router; |
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,93 @@ | ||
const express = require('express'); | ||
const Stripe = require('stripe'); | ||
const cors = require('cors'); // Import the CORS middleware | ||
|
||
const router = express.Router(); | ||
|
||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { | ||
apiVersion: '2020-08-27', | ||
}); | ||
|
||
const HARD_CODED_CUSTOMER_ID = 'cus_RPmbpSyKY0cF1N'; // Hard-coded customer ID for testing purposes | ||
|
||
// Enable CORS for all routes in this router | ||
router.use(cors()); // Allow cross-origin requests for all routes in this router | ||
|
||
// Route to retrieve all saved payment methods for a customer | ||
router.get('/payment-method', async (req, res) => { | ||
try { | ||
const paymentMethods = await stripe.paymentMethods.list({ | ||
customer: HARD_CODED_CUSTOMER_ID, | ||
type: 'card', | ||
}); | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
message: 'Payment methods retrieved successfully.', | ||
data: paymentMethods.data, | ||
}); | ||
} catch (error) { | ||
console.error('Error fetching payment methods:', error.message); | ||
res.status(500).json({ | ||
status: 'error', | ||
message: 'Failed to fetch payment methods.', | ||
data: null, | ||
}); | ||
} | ||
}); | ||
|
||
// Route to remove a payment method | ||
router.delete('/payment-method/:id', async (req, res) => { | ||
const { id } = req.params; | ||
|
||
try { | ||
await stripe.paymentMethods.detach(id); | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
message: 'Payment method removed successfully.', | ||
data: { id }, | ||
}); | ||
} catch (error) { | ||
console.error('Error removing payment method:', error.message); | ||
res.status(500).json({ | ||
status: 'error', | ||
message: 'Failed to remove payment method.', | ||
data: null, | ||
}); | ||
} | ||
}); | ||
|
||
// Route to attach a payment method to a customer | ||
router.put('/payment-method', async (req, res) => { | ||
const { paymentMethodId } = req.body; | ||
|
||
if (!paymentMethodId) { | ||
return res.status(400).json({ | ||
status: 'error', | ||
message: 'Payment Method ID is required.', | ||
data: null, | ||
}); | ||
} | ||
|
||
try { | ||
const paymentMethod = await stripe.paymentMethods.attach(paymentMethodId, { | ||
customer: HARD_CODED_CUSTOMER_ID, | ||
}); | ||
|
||
res.status(200).json({ | ||
status: 'success', | ||
message: 'Payment method added successfully.', | ||
data: paymentMethod, | ||
}); | ||
} catch (error) { | ||
console.error('Error adding payment method:', error.message); | ||
res.status(500).json({ | ||
status: 'error', | ||
message: 'Failed to add payment method.', | ||
data: null, | ||
}); | ||
} | ||
}); | ||
|
||
module.exports = router; |