This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
StripeWebhooksController.cs
141 lines (134 loc) · 8.74 KB
/
StripeWebhooksController.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity.Owin;
using SaasEcom.Core.DataServices.Storage;
using SaasEcom.Core.Models;
using Stripe;
using MyNotes.Models;
namespace MyNotes.Controllers
{
public class StripeWebhooksController : Controller
{
private InvoiceDataService<ApplicationDbContext, ApplicationUser> _invoiceDataService;
private InvoiceDataService<ApplicationDbContext, ApplicationUser> InvoiceDataService
{
get
{
return _invoiceDataService ??
(_invoiceDataService = new InvoiceDataService<ApplicationDbContext, ApplicationUser>(Request.GetOwinContext().Get<ApplicationDbContext>()));
}
}
// GET: /StripeWebhooks/
public async Task<HttpStatusCodeResult> Index()
{
var json = new StreamReader(Request.InputStream).ReadToEnd();
var stripeEvent = StripeEventUtility.ParseEvent(json);
#region All Event types
// All the event types explained here: https://stripe.com/docs/api#event_types
switch (stripeEvent.Type)
{
case "account.updated": //Occurs whenever an account status or property has changed.
break;
case "account.application.deauthorized": // Occurs whenever a user deauthorizes an application. Sent to the related application only.
break;
case "application_fee.created": // Occurs whenever an application fee is created on a charge.
break;
case "application_fee.refunded": // Occurs whenever an application fee is refunded, whether from refunding a charge or from refunding the application fee directly, including partial refunds.
break;
case "balance.available": // Occurs whenever your Stripe balance has been updated (e.g. when a charge collected is available to be paid out). By default, Stripe will automatically transfer any funds in your balance to your bank account on a daily basis.
break;
case "charge.succeeded": // Occurs whenever a new charge is created and is successful.
break;
case "charge.failed": // Occurs whenever a failed charge attempt occurs.
break;
case "charge.refunded": // Occurs whenever a charge is refunded, including partial refunds.
break;
case "charge.captured": // Occurs whenever a previously uncaptured charge is captured.
break;
case "charge.updated": // Occurs whenever a charge description or metadata is updated.
break;
case "charge.dispute.created": // Occurs whenever a customer disputes a charge with their bank (chargeback).
break;
case "charge.dispute.updated": // Occurs when the dispute is updated (usually with evidence).
break;
case "charge.dispute.closed": // Occurs when the dispute is resolved and the dispute status changes to won or lost.
break;
case "customer.created": // Occurs whenever a new customer is created.
break;
case "customer.updated": // Occurs whenever any property of a customer changes.
break;
case "customer.deleted": // Occurs whenever a customer is deleted.
break;
case "customer.card.created": // Occurs whenever a new card is created for the customer.
break;
case "customer.card.updated": // Occurs whenever a card's details are changed.
// TODO: Save card updated, might happen when the card is close to expire
break;
case "customer.card.deleted": // Occurs whenever a card is removed from a customer.
break;
case "customer.subscription.created": // Occurs whenever a customer with no subscription is signed up for a plan.
break;
case "customer.subscription.updated": // Occurs whenever a subscription changes. Examples would include switching from one plan to another, or switching status from trial to active.
break;
case "customer.subscription.deleted": // Occurs whenever a customer ends their subscription.
break;
case "customer.subscription.trial_will_end": // Occurs three days before the trial period of a subscription is scheduled to end.
// TODO: If the user hasn't added credit card details -> Send email reminder.
break;
case "customer.discount.created": // Occurs whenever a coupon is attached to a customer.
break;
case "customer.discount.updated": // Occurs whenever a customer is switched from one coupon to another.
break;
case "customer.discount.deleted":
break;
case "invoice.created": // Occurs whenever a new invoice is created. If you are using webhooks, Stripe will wait one hour after they have all succeeded to attempt to pay the invoice; the only exception here is on the first invoice, which gets created and paid immediately when you subscribe a customer to a plan. If your webhooks do not all respond successfully, Stripe will continue retrying the webhooks every hour and will not attempt to pay the invoice. After 3 days, Stripe will attempt to pay the invoice regardless of whether or not your webhooks have succeeded. See how to respond to a webhook.
break;
case "invoice.payment_failed": // Occurs whenever an invoice attempts to be paid, and the payment fails. This can occur either due to a declined payment, or because the customer has no active card. A particular case of note is that if a customer with no active card reaches the end of its free trial, an invoice.payment_failed notification will occur.
// TODO: Notify customer
break;
case "invoice.payment_succeeded": // Occurs whenever an invoice attempts to be paid, and the payment succeeds.
StripeInvoice stripeInvoice = Stripe.Mapper<StripeInvoice>.MapFromJson(stripeEvent.Data.Object.ToString());
Invoice invoice = SaasEcom.Core.Infrastructure.Mappers.Map(stripeInvoice);
if (invoice != null && invoice.Total > 0)
{
// TODO get the customer billing address, we still have to instantiate the address on the invoice
invoice.BillingAddress = new BillingAddress();
await InvoiceDataService.CreateOrUpdateAsync(invoice);
// TODO: Send invoice by email
}
break;
case "invoice.updated": // Occurs whenever an invoice changes (for example, the amount could change).
break;
case "invoiceitem.created": // Occurs whenever an invoice item is created.
break;
case "invoiceitem.updated": // Occurs whenever an invoice item is updated.
break;
case "invoiceitem.deleted": // Occurs whenever an invoice item is deleted.
break;
case "plan.created": // Occurs whenever a plan is created.
break;
case "plan.updated": // Occurs whenever a plan is updated.
break;
case "plan.deleted": // Occurs whenever a plan is deleted.
break;
case "coupon.created": // Occurs whenever a coupon is created.
break;
case "coupon.deleted": // Occurs whenever a coupon is deleted.
break;
case "transfer.created": // Occurs whenever a new transfer is created.
break;
case "transfer.updated": // Occurs whenever the description or metadata of a transfer is updated.
break;
case "transfer.paid": // Occurs whenever a sent transfer is expected to be available in the destination bank account. If the transfer failed, a transfer.failed webhook will additionally be sent at a later time.
break;
case "transfer.failed": // Occurs whenever Stripe attempts to send a transfer and that transfer fails.
break;
}
#endregion
return new HttpStatusCodeResult(HttpStatusCode.Accepted);
}
}
}