-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathAppController.cs
173 lines (149 loc) · 6.09 KB
/
AppController.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Recurly;
using Recurly.Resources;
namespace dotnet.Controllers
{
[ApiController]
public class AppController : ControllerBase
{
private readonly Client _client;
private readonly ILogger<AppController> _logger;
public AppController(ILogger<AppController> logger)
{
_client = new Client(APIKey);
_logger = logger;
}
// TODO: Bring over advanced item purchasing from ruby example
[HttpPost("api/subscriptions/new")]
public RedirectResult CreatePurchase([FromForm(Name = "recurly-token")] string tokenId, [FromForm(Name = "account-code")] string accountCode, [FromForm(Name = "first-name")] string firstName, [FromForm(Name = "last-name")] string lastName)
{
// If our form specifies an account code, we can use that; otherwise,
// create an account code with a uniq id
accountCode = accountCode ?? Guid.NewGuid().ToString();
var purchaseReq = new PurchaseCreate()
{
Currency = "USD",
Account = new AccountPurchase()
{
Code = accountCode,
FirstName = firstName,
LastName = lastName,
BillingInfo = new BillingInfoCreate()
{
TokenId = tokenId
}
},
Subscriptions = new List<SubscriptionPurchase>()
{
new SubscriptionPurchase() { PlanCode = "basic" }
}
};
try
{
InvoiceCollection collection = _client.CreatePurchase(purchaseReq);
_logger.LogInformation($"Created ChargeInvoice with Number: {collection.ChargeInvoice.Number}");
}
catch (Recurly.Errors.Transaction e)
{
/**
* Note: This is not an example of extensive error handling,
* it is scoped to handling the 3DSecure error for simplicity.
* Please ensure you have proper error handling before going to production.
*/
TransactionError transactionError = e.Error.TransactionError;
if (transactionError != null && transactionError.Code == "three_d_secure_action_required") {
string actionTokenId = transactionError.ThreeDSecureActionTokenId;
return Redirect($"/3d-secure/authenticate.html#token_id={tokenId}&action_token_id={actionTokenId}&account_code={accountCode}");
}
return HandleError(e);
}
catch (Recurly.Errors.ApiError e)
{
return HandleError(e);
}
return Redirect(SuccessURL);
}
[HttpPost("api/accounts/new")]
public ActionResult CreateAccount([FromForm(Name = "account-code")] string accountCode, [FromForm(Name = "first-name")] string firstName, [FromForm(Name = "last-name")] string lastName)
{
// If our form specifies an account code, we can use that; otherwise,
// create an account code with a uniq id
accountCode = accountCode ?? Guid.NewGuid().ToString();
var accountReq = new AccountCreate()
{
Code = accountCode,
FirstName = firstName,
LastName = lastName
};
try
{
Account account = _client.CreateAccount(accountReq);
_logger.LogInformation($"Created account {account.Code}");
}
catch (Recurly.Errors.ApiError e)
{
return HandleError(e);
}
return Redirect(SuccessURL);
}
[HttpPut("api/accounts/{accountId}")]
public ActionResult UpdateAccount(string accountId, [FromForm(Name = "first-name")] string firstName, [FromForm(Name = "last-name")] string lastName)
{
var accountReq = new AccountUpdate() {
FirstName = firstName,
LastName = lastName
};
try
{
Account account = _client.UpdateAccount(accountId, accountReq);
_logger.LogInformation($"Updated account {account.Code}");
}
catch (Recurly.Errors.ApiError e)
{
return HandleError(e);
}
return Redirect(SuccessURL);
}
/* This endpoint provides configuration to recurly.js */
[HttpGet("config")]
public ContentResult GetGonfig()
{
var config = new {
publicKey = APIPublicKey
};
return Content($"window.recurlyConfig = {System.Text.Json.JsonSerializer.Serialize(config)}", "application/javascript");
}
private RedirectResult HandleError(Exception e)
{
_logger.LogError(e, "Exception caught: redirecting");
return Redirect($"{ErrorURL}?error={e.Message}");
}
private string APIKey
{
get { return Environment.GetEnvironmentVariable("RECURLY_API_KEY"); }
}
private string APIPublicKey
{
get { return Environment.GetEnvironmentVariable("RECURLY_PUBLIC_KEY"); }
}
private string SuccessURL
{
get {
string url = Environment.GetEnvironmentVariable("SUCCESS_URL");
return String.IsNullOrEmpty(url) ? "/" : url;
}
}
private string ErrorURL
{
get {
string url = Environment.GetEnvironmentVariable("ERROR_URL");
return String.IsNullOrEmpty(url) ? "/" : url;
}
}
}
}