-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
46 lines (35 loc) · 1.36 KB
/
Program.cs
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
using PayPal.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient();
builder.Services.AddLogging(config => config.AddConsole());
var app = builder.Build();
app.UseHttpsRedirection();
// requires
// - an Ngrok url to test, https://ngrok.com/
// - a PayPal account for use with https://developer.paypal.com/developer/ipnSimulator
app.MapPost("/IpnTest", async (HttpContext context) =>
{
// for efficient HttpClient use
var httpClientFactory = context.RequestServices.GetRequiredService<IHttpClientFactory>();
// for emitting internal info while it runs
var logger = context.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger("IpnTest");
try
{
var result = await context.Request.VerifyPayPalTransactionAsync(PayPalEnvironment.Sandbox, httpClientFactory, logger);
if (result.IsVerified)
{
// should be values you entered in IPN simulator form
logger.LogInformation($"Item num: {result.Transaction.ItemNumber}, gross = {result.Transaction.Gross:c2}");
}
}
catch (Exception exc)
{
// we don't want exception to escape from this handler
logger.LogError(exc, "Error in /IpnTest handler: {message}", exc.Message);
}
});
app.MapGet("/", async (HttpResponse response) =>
{
await response.WriteAsync("Hello");
});
app.Run();