-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePaymentSession.cs
More file actions
68 lines (58 loc) · 2.28 KB
/
Copy pathCreatePaymentSession.cs
File metadata and controls
68 lines (58 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.HttpResults;
using HoTeach.Payments.Models;
using HoTeach.Payments.Services;
namespace HoTeach
{
public class CreatePaymentSession
{
private readonly ILogger<CreatePaymentSession> _logger;
public CreatePaymentSession(ILogger<CreatePaymentSession> logger)
{
_logger = logger;
}
[Function("CreatePaymentSession")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "PaymentSession")] HttpRequest req)
{
try
{
var principal = AuthHelper.ValidateToken(req);
_logger.LogInformation($"Authenticated user: {principal.Identity?.Name}");
}
catch (Exception ex)
{
_logger.LogWarning(ex.Message);
return new UnauthorizedObjectResult(HttpStatusCode.Unauthorized);
}
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var request = JsonConvert.DeserializeObject<PaymentRequest>(requestBody);
if (request == null)
{
return new BadRequestObjectResult(HttpStatusCode.BadRequest);
}
var stripeSecretKey = Environment.GetEnvironmentVariable("STRIPE_SECRET_KEY");
var webhookSecret = Environment.GetEnvironmentVariable("STRIPE_WEBHOOK_SECRET");
var stripeService = new StripeService(stripeSecretKey, webhookSecret);
var paymentResponse = await stripeService.CreatePaymentSessionAsync(request);
return new OkObjectResult(paymentResponse);
}
catch (Exception ex)
{
_logger.LogError($"Error creating payment session: {ex.Message}");
return new BadRequestObjectResult(new { error = ex.Message }); ;
}
}
}
}