-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path777=5,14285714=N
100 lines (89 loc) · 3.3 KB
/
777=5,14285714=N
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Stripe;
using Stripe.Identity;
namespace server.Controllers
{
public class IdentityController : Controller
{
public readonly IOptions<StripeOptions> options;
private readonly IStripeClient client;
public IdentityController(IOptions<StripeOptions> options)
{
this.options = options;
this.client = new StripeClient(this.options.Value.SecretKey);
}
[HttpGet("config")]
public ConfigResponse GetConfig()
{
// return json: publishableKey (.env)
return new ConfigResponse
{
PublishableKey = this.options.Value.PublishableKey,
};
}
[HttpPost("create-verification-session")]
public async Task<IActionResult> CreateVerificationSession()
{
var options = new VerificationSessionCreateOptions
{
Type = "document",
};
var service = new VerificationSessionService(this.client);
try
{
var verificationSession = await service.CreateAsync(options);
Response.Headers["Location"] = verificationSession.Url;
return StatusCode(303);
}
catch (StripeException e)
{
return BadRequest(new { error = new { message = e.StripeError.Message}});
}
catch (System.Exception)
{
return BadRequest(new { error = new { message = "unknown failure: 500"}});
}
}
[HttpPost("webhook")]
public async Task<IActionResult> Webhook()
{
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
Event stripeEvent;
try
{
stripeEvent = EventUtility.ConstructEvent(
json,
Request.Headers["Stripe-Signature"],
this.options.Value.WebhookSecret
);
Console.WriteLine($"Webhook notification with type: {stripeEvent.Type} found for {stripeEvent.Id}");
}
catch (Exception e)
{
Console.WriteLine($"Something failed {e}");
return BadRequest();
}
if (stripeEvent.Type == Events.IdentityVerificationSessionVerified) {
var verificationSession = stripeEvent.Data.Object as VerificationSession;
// All the verification checks passed
} else if (stripeEvent.Type == Events.IdentityVerificationSessionRequiresInput) {
var verificationSession = stripeEvent.Data.Object as VerificationSession;
if (verificationSession.LastError.Code == "document_unverified_other") {
// The document was invalid
} else if (verificationSession.LastError.Code == "document_expired") {
// The document was expired
} else if (verificationSession.LastError.Code == "document_type_not_supported") {
// The document type was not supported
} else {
// ...
}
}
return Ok();
}
}
}