Automatic conversion to lower Camel Case for JSON

This commit is contained in:
lepipele 2017-10-20 22:37:01 -05:00
parent e49f25af09
commit f1f19369a3
5 changed files with 45 additions and 43 deletions

View File

@ -142,28 +142,28 @@ namespace BTCPayServer.Controllers
var model = new PaymentModel()
{
serverUrl = HttpContext.Request.GetAbsoluteRoot(),
ServerUrl = HttpContext.Request.GetAbsoluteRoot(),
OrderId = invoice.OrderId,
invoiceId = invoice.Id,
btcAddress = invoice.DepositAddress.ToString(),
BTCAmount = (invoice.GetTotalCryptoDue() - invoice.TxFee).ToString(),
BTCTotalDue = invoice.GetTotalCryptoDue().ToString(),
btcDue = invoice.GetCryptoDue().ToString(),
customerEmail = invoice.RefundMail,
expirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
maxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
InvoiceId = invoice.Id,
BtcAddress = invoice.DepositAddress.ToString(),
BtcAmount = (invoice.GetTotalCryptoDue() - invoice.TxFee).ToString(),
BtcTotalDue = invoice.GetTotalCryptoDue().ToString(),
BtcDue = invoice.GetCryptoDue().ToString(),
CustomerEmail = invoice.RefundMail,
ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
MaxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
ItemDesc = invoice.ProductInformation.ItemDesc,
Rate = invoice.Rate.ToString("C", GetCurrencyProvider(invoice.ProductInformation.Currency)),
merchantRefLink = invoice.RedirectURL,
MerchantRefLink = invoice.RedirectURL,
StoreName = store.StoreName,
TxFees = invoice.TxFee.ToString(),
InvoiceBitcoinUrl = dto.PaymentUrls.BIP72,
TxCount = invoice.GetTxCount(),
BTCPaid = invoice.GetTotalPaid().ToString(),
status = invoice.Status
BtcPaid = invoice.GetTotalPaid().ToString(),
Status = invoice.Status
};
var expiration = TimeSpan.FromSeconds(model.expirationSeconds);
var expiration = TimeSpan.FromSeconds(model.ExpirationSeconds);
model.TimeLeft = PrettyPrint(expiration);
return model;
}

View File

@ -1,9 +1,12 @@
using BTCPayServer.Authentication;
using BTCPayServer.Configuration;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text;
@ -44,5 +47,12 @@ namespace BTCPayServer
return throws ? throw new UnauthorizedAccessException("no-bitid") : (BitIdentity)null;
return (BitIdentity)controller.User.Identity;
}
private static JsonSerializerSettings jsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
public static HtmlString ToJson(this object o)
{
var res = JsonConvert.SerializeObject(o, Formatting.None, jsonSettings);
return new HtmlString(res);
}
}
}

View File

@ -5,38 +5,31 @@ using System.Threading.Tasks;
namespace BTCPayServer.Models.InvoicingModels
{
// going with lowercase for property names to enable easy ToJson conversion
// down the road I can look into mapper who transforms capital into lower case
// because of different conventions between server and client side
public class PaymentModel
{
public string serverUrl { get; set; }
public string invoiceId { get; set; }
public string btcAddress { get; set; }
public string btcDue { get; set; }
public string customerEmail { get; set; }
public int expirationSeconds { get; set; }
public string status { get; set; }
public string merchantRefLink { get; set; }
public int maxTimeSeconds { get; set; }
public string ServerUrl { get; set; }
public string InvoiceId { get; set; }
public string BtcAddress { get; set; }
public string BtcDue { get; set; }
public string CustomerEmail { get; set; }
public int ExpirationSeconds { get; set; }
public string Status { get; set; }
public string MerchantRefLink { get; set; }
public int MaxTimeSeconds { get; set; }
// These properties are still not used in client side code
// so will stick with C# notation for now
// These properties are not used in client side code
public string StoreName { get; set; }
public string ItemDesc { get; set; }
public string TimeLeft { get; set; }
public string Rate { get; set; }
public string BTCAmount { get; set; }
public string BtcAmount { get; set; }
public string TxFees { get; set; }
public string InvoiceBitcoinUrl { get; set; }
public string BTCTotalDue { get; set; }
public string BtcTotalDue { get; set; }
public int TxCount { get; set; }
public string BTCPaid { get; set; }
public string BtcPaid { get; set; }
public string StoreEmail { get; set; }
public string OrderId
{
get; set;
}
public string OrderId { get; set; }
}
}

View File

@ -27,7 +27,7 @@
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<script type="text/javascript">
var srvModel = @Html.Raw(JsonConvert.SerializeObject(Model));
var srvModel = @Model.ToJson();
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js"></script>
<script src="~/js/core.js" type="text/javascript" defer="defer"></script>
@ -113,7 +113,7 @@
<!---->
<div class="single-item-order__right">
<div class="single-item-order__right__btc-price clickable" id="buyerTotalBtcAmount">
<span>@Model.BTCTotalDue</span>
<span>@Model.BtcTotalDue</span>
</div>
<!---->
<div class="single-item-order__right__ex-rate">
@ -128,7 +128,7 @@
<!---->
<div class="line-items__item">
<div class="line-items__item__label" i18n="">Payment Amount</div>
<div class="line-items__item__value">@Model.BTCAmount BTC</div>
<div class="line-items__item__value">@Model.BtcAmount BTC</div>
</div>
<div class="line-items__item">
<div class="line-items__item__label">
@ -140,11 +140,11 @@
<div class="line-items__item__label">
<span i18n="">Already Paid</span>
</div>
<div class="line-items__item__value" i18n="">-@Model.BTCPaid BTC</div>
<div class="line-items__item__value" i18n="">-@Model.BtcPaid BTC</div>
</div>
<div class="line-items__item line-items__item--total">
<div class="line-items__item__label" i18n="">Due </div>
<div class="line-items__item__value">@Model.btcDue BTC</div>
<div class="line-items__item__value">@Model.BtcDue BTC</div>
</div>
<!---->
</div>
@ -363,7 +363,7 @@
<div class="manual-box__amount__label label" i18n="">Amount</div>
<!---->
<div class="manual-box__amount__value copy-cursor" ngxclipboard="">
<span>@Model.btcDue</span> BTC
<span>@Model.BtcDue</span> BTC
<div class="copied-label">
<span i18n="">Copied</span>
</div>
@ -382,7 +382,7 @@
<div class="manual-box__address__wrapper__logo">
<img src="~/img/bitcoin-symbol.svg">
</div>
<div class="manual-box__address__wrapper__value">@Model.btcAddress</div>
<div class="manual-box__address__wrapper__value">@Model.BtcAddress</div>
</div>
<div class="copied-label" style="top: 5px;">
<span i18n="">Copied</span>
@ -520,7 +520,7 @@
hours.
</div>
<div class="expired__text">
<span class="expired__text__bullet" i18n="">Invoice ID:</span> @Model.invoiceId<br>
<span class="expired__text__bullet" i18n="">Invoice ID:</span> @Model.InvoiceId<br>
<!---->
<span>
<span class="expired__text__bullet" i18n="">Order ID:</span> @Model.OrderId

View File

@ -5,5 +5,4 @@
@using BTCPayServer.Models.InvoicingModels
@using BTCPayServer.Models.ManageViewModels
@using BTCPayServer.Models.StoreViewModels
@using Newtonsoft.Json;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers