Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Changes #980

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions server/routes/api/checkout.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
const express = require('express')
const axios = require('axios')
const { v4: uuidv4 } = require('uuid')
const { Stripe, StripeError } = require('../../lib/stripe')
const {
PaymentPresenter,
PaymentPresenterError
} = require('../../../shared/presenters/payment-presenter')
const Constituent = require('../../db/models/constituent')
const Transaction = require('../../db/models/transaction')
const { Stripe, StripeError } = require('../../lib/stripe');
const { PaymentPresenter, PaymentPresenterError } = require('../../../shared/presenters/payment-presenter');
const Constituent = require('../../db/models/constituent');
const Transaction = require('../../db/models/transaction');

// Function for payment processing with added error handling
async function processPayment(userId, paymentData) {
try {
// Validate user ID and payment data
const constituent = await Constituent.findByPk(userId);
if (!constituent) {
throw new Error('Invalid user ID');
}

// Process the payment securely
const paymentResult = await Stripe.paymentIntents.create(paymentData);

// Handle post-payment logic
const transaction = await Transaction.create({ userId, amount: paymentResult.amount });
return transaction;

} catch (error) {
// Handle specific errors
if (error instanceof StripeError) {
console.error('Stripe Error:', error.message);
} else if (error instanceof PaymentPresenterError) {
console.error('Payment Presentation Error:', error.message);
} else {
console.error('General Error:', error.message);
}
throw error;
const Letter = require('../../db/models/letter')

const router = express.Router()
Expand Down
Loading