128 lines
4.9 KiB
JavaScript
128 lines
4.9 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createDonation = createDonation;
|
|
const functions_1 = require("@azure/functions");
|
|
const DIContainer_1 = __importDefault(require("../DIContainer"));
|
|
const uuid_1 = require("uuid");
|
|
const stripe_1 = __importDefault(require("stripe"));
|
|
async function createDonation(request, context) {
|
|
try {
|
|
await DIContainer_1.default.getInstance().initializeServices();
|
|
const { donationsContainer, secretClient } = DIContainer_1.default.getInstance().getServices();
|
|
// Get request body
|
|
const donationRequest = await request.json();
|
|
// Validate required fields
|
|
if (!donationRequest.amount || !donationRequest.donorEmail || !donationRequest.donorName) {
|
|
const response = {
|
|
success: false,
|
|
error: 'Missing required fields: amount, donorEmail, donorName',
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
return {
|
|
status: 400,
|
|
jsonBody: response,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
};
|
|
}
|
|
// Initialize Stripe if payment method is stripe
|
|
let stripePaymentIntentId;
|
|
if (donationRequest.paymentMethod === 'stripe') {
|
|
try {
|
|
const stripeSecretKey = await secretClient.getSecret('stripe-secret-key');
|
|
const stripe = new stripe_1.default(stripeSecretKey.value, {
|
|
apiVersion: '2025-02-24.acacia'
|
|
});
|
|
const paymentIntent = await stripe.paymentIntents.create({
|
|
amount: Math.round(donationRequest.amount * 100), // Convert to cents
|
|
currency: donationRequest.currency.toLowerCase(),
|
|
metadata: {
|
|
donorEmail: donationRequest.donorEmail,
|
|
donorName: donationRequest.donorName,
|
|
program: donationRequest.program || 'general'
|
|
}
|
|
});
|
|
stripePaymentIntentId = paymentIntent.id;
|
|
}
|
|
catch (stripeError) {
|
|
context.error('Stripe payment intent creation failed:', stripeError);
|
|
const response = {
|
|
success: false,
|
|
error: 'Payment processing failed',
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
return {
|
|
status: 500,
|
|
jsonBody: response,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
};
|
|
}
|
|
}
|
|
// Create donation record
|
|
const donation = {
|
|
id: (0, uuid_1.v4)(),
|
|
amount: donationRequest.amount,
|
|
currency: donationRequest.currency,
|
|
donorName: donationRequest.donorName,
|
|
donorEmail: donationRequest.donorEmail,
|
|
donorPhone: donationRequest.donorPhone,
|
|
program: donationRequest.program,
|
|
isRecurring: donationRequest.isRecurring,
|
|
frequency: donationRequest.frequency,
|
|
paymentMethod: donationRequest.paymentMethod,
|
|
stripePaymentIntentId,
|
|
status: 'pending',
|
|
message: donationRequest.message,
|
|
isAnonymous: donationRequest.isAnonymous,
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString()
|
|
};
|
|
// Save to Cosmos DB
|
|
await donationsContainer.items.create(donation);
|
|
const response = {
|
|
success: true,
|
|
data: donation,
|
|
message: 'Donation created successfully',
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
return {
|
|
status: 201,
|
|
jsonBody: response,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
};
|
|
}
|
|
catch (error) {
|
|
context.error('Error creating donation:', error);
|
|
const response = {
|
|
success: false,
|
|
error: 'Failed to create donation',
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
return {
|
|
status: 500,
|
|
jsonBody: response,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
};
|
|
}
|
|
}
|
|
functions_1.app.http('createDonation', {
|
|
methods: ['POST'],
|
|
authLevel: 'anonymous',
|
|
route: 'donations',
|
|
handler: createDonation
|
|
});
|
|
//# sourceMappingURL=createDonation.js.map
|