2018-12-19 07:07:05 +01:00
using System ;
using System.Linq ;
using System.Text ;
2018-08-30 20:16:24 +02:00
using System.Text.Encodings.Web ;
2018-12-19 07:07:05 +01:00
using System.Text.RegularExpressions ;
2018-04-03 04:50:41 +02:00
using System.Threading.Tasks ;
using BTCPayServer.Data ;
using BTCPayServer.Models.AppViewModels ;
using BTCPayServer.Services.Apps ;
2019-04-28 08:27:10 +02:00
using BTCPayServer.Services.Mails ;
2018-08-30 20:16:24 +02:00
using Microsoft.AspNetCore.Mvc ;
using Microsoft.EntityFrameworkCore ;
2018-04-03 04:50:41 +02:00
namespace BTCPayServer.Controllers
{
public partial class AppsController
{
public class PointOfSaleSettings
{
public PointOfSaleSettings ( )
{
2018-11-14 09:45:46 +01:00
Title = "Tea shop" ;
2018-04-03 04:50:41 +02:00
Currency = "USD" ;
Template =
2018-11-13 08:29:18 +01:00
"green tea:\n" +
" price: 1\n" +
" title: Green Tea\n" +
" description: Lovely, fresh and tender, Meng Ding Gan Lu ('sweet dew') is grown in the lush Meng Ding Mountains of the southwestern province of Sichuan where it has been cultivated for over a thousand years.\n" +
" image: https://cdn.pixabay.com/photo/2015/03/26/11/03/green-tea-692339__480.jpg\n\n" +
"black tea:\n" +
" price: 1\n" +
" title: Black Tea\n" +
" description: Tian Jian Tian Jian means 'heavenly tippy tea' in Chinese, and it describes the finest grade of dark tea. Our Tian Jian dark tea is from Hunan province which is famous for making some of the best dark teas available.\n" +
" image: https://cdn.pixabay.com/photo/2016/11/29/13/04/beverage-1869716__480.jpg\n\n" +
"rooibos:\n" +
" price: 1.2\n" +
" title: Rooibos\n" +
" description: Rooibos is a dramatic red tea made from a South African herb that contains polyphenols and flavonoids. Often called 'African redbush tea', Rooibos herbal tea delights the senses and delivers potential health benefits with each caffeine-free sip.\n" +
" image: https://cdn.pixabay.com/photo/2017/01/08/08/14/water-1962388__480.jpg\n\n" +
"pu erh:\n" +
" price: 2\n" +
" title: Pu Erh\n" +
" description: This loose pur-erh tea is produced in Yunnan Province, China. The process in a relatively high humidity environment has mellowed the elemental character of the tea when compared to young Pu-erh.\n" +
" image: https://cdn.pixabay.com/photo/2018/07/21/16/56/tea-cup-3552917__480.jpg\n\n" +
"herbal tea:\n" +
" price: 1.8\n" +
" title: Herbal Tea\n" +
2018-11-16 10:36:18 +01:00
" description: Chamomile tea is made from the flower heads of the chamomile plant. The medicinal use of chamomile dates back to the ancient Egyptians, Romans and Greeks. Pay us what you want!\n" +
" image: https://cdn.pixabay.com/photo/2015/07/02/20/57/chamomile-829538__480.jpg\n" +
" custom: true\n\n" +
2018-11-13 08:29:18 +01:00
"fruit tea:\n" +
" price: 1.5\n" +
" title: Fruit Tea\n" +
2018-11-16 10:36:18 +01:00
" description: The Tibetan Himalayas, the land is majestic and beautiful—a spiritual place where, despite the perilous environment, many journey seeking enlightenment. Pay us what you want!\n" +
" image: https://cdn.pixabay.com/photo/2016/09/16/11/24/darts-1673812__480.jpg\n" +
2019-09-02 15:37:52 +02:00
" inventory: 5\n" +
2018-11-16 10:36:18 +01:00
" custom: true" ;
2018-11-27 07:14:32 +01:00
EnableShoppingCart = false ;
2018-04-26 15:09:18 +02:00
ShowCustomAmount = true ;
2019-02-25 07:11:03 +01:00
ShowDiscount = true ;
EnableTips = true ;
2018-04-03 04:50:41 +02:00
}
public string Title { get ; set ; }
public string Currency { get ; set ; }
public string Template { get ; set ; }
2018-11-27 07:14:32 +01:00
public bool EnableShoppingCart { get ; set ; }
2018-04-26 15:09:18 +02:00
public bool ShowCustomAmount { get ; set ; }
2019-02-25 07:11:03 +01:00
public bool ShowDiscount { get ; set ; }
public bool EnableTips { get ; set ; }
2018-11-17 03:39:43 +01:00
public const string BUTTON_TEXT_DEF = "Buy for {0}" ;
public string ButtonText { get ; set ; } = BUTTON_TEXT_DEF ;
public const string CUSTOM_BUTTON_TEXT_DEF = "Pay" ;
public string CustomButtonText { get ; set ; } = CUSTOM_BUTTON_TEXT_DEF ;
2018-11-27 07:14:32 +01:00
public const string CUSTOM_TIP_TEXT_DEF = "Do you want to leave a tip?" ;
public string CustomTipText { get ; set ; } = CUSTOM_TIP_TEXT_DEF ;
2018-12-19 07:07:05 +01:00
public static readonly int [ ] CUSTOM_TIP_PERCENTAGES_DEF = new int [ ] { 15 , 18 , 20 } ;
public int [ ] CustomTipPercentages { get ; set ; } = CUSTOM_TIP_PERCENTAGES_DEF ;
2018-12-13 14:36:19 +01:00
2018-11-17 03:39:43 +01:00
public string CustomCSSLink { get ; set ; }
2019-08-19 07:13:42 +02:00
public string EmbeddedCSS { get ; set ; }
public string Description { get ; set ; }
2019-03-29 07:51:00 +01:00
public string NotificationEmail { get ; set ; }
public string NotificationUrl { get ; set ; }
2019-04-11 11:53:31 +02:00
public bool? RedirectAutomatically { get ; set ; }
2018-04-03 04:50:41 +02:00
}
[HttpGet]
[Route("{appId}/settings/pos")]
public async Task < IActionResult > UpdatePointOfSale ( string appId )
{
var app = await GetOwnedApp ( appId , AppType . PointOfSale ) ;
if ( app = = null )
return NotFound ( ) ;
var settings = app . GetSettings < PointOfSaleSettings > ( ) ;
2019-04-28 08:27:10 +02:00
2018-05-24 16:54:48 +02:00
var vm = new UpdatePointOfSaleViewModel ( )
{
2019-04-28 08:28:22 +02:00
NotificationEmailWarning = ! await IsEmailConfigured ( app . StoreDataId ) ,
2019-01-31 08:56:21 +01:00
Id = appId ,
2019-07-14 15:54:27 +02:00
StoreId = app . StoreDataId ,
2018-05-24 16:54:48 +02:00
Title = settings . Title ,
2018-11-27 07:14:32 +01:00
EnableShoppingCart = settings . EnableShoppingCart ,
2018-05-24 16:54:48 +02:00
ShowCustomAmount = settings . ShowCustomAmount ,
2019-02-25 07:11:03 +01:00
ShowDiscount = settings . ShowDiscount ,
EnableTips = settings . EnableTips ,
2018-05-24 16:54:48 +02:00
Currency = settings . Currency ,
2018-11-17 03:39:43 +01:00
Template = settings . Template ,
ButtonText = settings . ButtonText ? ? PointOfSaleSettings . BUTTON_TEXT_DEF ,
CustomButtonText = settings . CustomButtonText ? ? PointOfSaleSettings . CUSTOM_BUTTON_TEXT_DEF ,
2018-11-27 07:14:32 +01:00
CustomTipText = settings . CustomTipText ? ? PointOfSaleSettings . CUSTOM_TIP_TEXT_DEF ,
2018-12-19 07:07:05 +01:00
CustomTipPercentages = settings . CustomTipPercentages ! = null ? string . Join ( "," , settings . CustomTipPercentages ) : string . Join ( "," , PointOfSaleSettings . CUSTOM_TIP_PERCENTAGES_DEF ) ,
2019-03-29 07:51:00 +01:00
CustomCSSLink = settings . CustomCSSLink ,
2019-08-19 07:13:42 +02:00
EmbeddedCSS = settings . EmbeddedCSS ,
Description = settings . Description ,
2019-03-29 07:51:00 +01:00
NotificationEmail = settings . NotificationEmail ,
2019-04-11 11:08:42 +02:00
NotificationUrl = settings . NotificationUrl ,
2019-09-10 10:03:24 +02:00
SearchTerm = $"storeid:{app.StoreDataId}" ,
2019-04-11 11:53:31 +02:00
RedirectAutomatically = settings . RedirectAutomatically . HasValue ? settings . RedirectAutomatically . Value ? "true" : "false" : ""
2018-05-24 16:54:48 +02:00
} ;
if ( HttpContext ? . Request ! = null )
{
var appUrl = HttpContext . Request . GetAbsoluteRoot ( ) . WithTrailingSlash ( ) + $"apps/{appId}/pos" ;
var encoder = HtmlEncoder . Default ;
if ( settings . ShowCustomAmount )
{
StringBuilder builder = new StringBuilder ( ) ;
builder . AppendLine ( $"<form method=\" POST \ " action=\"{encoder.Encode(appUrl)}\">" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"amount\" value=\"100\" />" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"email\" value=\"customer@example.com\" />" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"orderId\" value=\"CustomOrderId\" />" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"notificationUrl\" value=\"https://example.com/callbacks\" />" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"redirectUrl\" value=\"https://example.com/thanksyou\" />" ) ;
builder . AppendLine ( $" <button type=\" submit \ ">Buy now</button>" ) ;
builder . AppendLine ( $"</form>" ) ;
vm . Example1 = builder . ToString ( ) ;
}
try
{
2019-02-19 05:04:58 +01:00
var items = _AppService . Parse ( settings . Template , settings . Currency ) ;
2018-05-24 16:54:48 +02:00
var builder = new StringBuilder ( ) ;
builder . AppendLine ( $"<form method=\" POST \ " action=\"{encoder.Encode(appUrl)}\">" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"email\" value=\"customer@example.com\" />" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"orderId\" value=\"CustomOrderId\" />" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"notificationUrl\" value=\"https://example.com/callbacks\" />" ) ;
builder . AppendLine ( $" <input type=\" hidden \ " name=\"redirectUrl\" value=\"https://example.com/thanksyou\" />" ) ;
builder . AppendLine ( $" <button type=\" submit \ " name=\"choiceKey\" value=\"{items[0].Id}\">Buy now</button>" ) ;
builder . AppendLine ( $"</form>" ) ;
vm . Example2 = builder . ToString ( ) ;
}
catch { }
vm . InvoiceUrl = appUrl + "invoices/SkdsDghkdP3D3qkj7bLq3" ;
}
2018-05-25 10:35:01 +02:00
vm . ExampleCallback = "{\n \"id\":\"SkdsDghkdP3D3qkj7bLq3\",\n \"url\":\"https://btcpay.example.com/invoice?id=SkdsDghkdP3D3qkj7bLq3\",\n \"status\":\"paid\",\n \"price\":10,\n \"currency\":\"EUR\",\n \"invoiceTime\":1520373130312,\n \"expirationTime\":1520374030312,\n \"currentTime\":1520373179327,\n \"exceptionStatus\":false,\n \"buyerFields\":{\n \"buyerEmail\":\"customer@example.com\",\n \"buyerNotify\":false\n },\n \"paymentSubtotals\": {\n \"BTC\":114700\n },\n \"paymentTotals\": {\n \"BTC\":118400\n },\n \"transactionCurrency\": \"BTC\",\n \"amountPaid\": \"1025900\",\n \"exchangeRates\": {\n \"BTC\": {\n \"EUR\": 8721.690715789999,\n \"USD\": 10817.99\n }\n }\n}" ;
2018-05-24 16:54:48 +02:00
return View ( vm ) ;
2018-04-03 04:50:41 +02:00
}
[HttpPost]
[Route("{appId}/settings/pos")]
public async Task < IActionResult > UpdatePointOfSale ( string appId , UpdatePointOfSaleViewModel vm )
{
2019-02-17 08:53:41 +01:00
if ( _currencies . GetCurrencyData ( vm . Currency , false ) = = null )
2018-04-03 04:50:41 +02:00
ModelState . AddModelError ( nameof ( vm . Currency ) , "Invalid currency" ) ;
try
{
2019-02-19 05:04:58 +01:00
_AppService . Parse ( vm . Template , vm . Currency ) ;
2018-04-03 04:50:41 +02:00
}
catch
{
ModelState . AddModelError ( nameof ( vm . Template ) , "Invalid template" ) ;
}
if ( ! ModelState . IsValid )
{
return View ( vm ) ;
}
var app = await GetOwnedApp ( appId , AppType . PointOfSale ) ;
if ( app = = null )
return NotFound ( ) ;
app . SetSettings ( new PointOfSaleSettings ( )
{
Title = vm . Title ,
2018-11-27 07:14:32 +01:00
EnableShoppingCart = vm . EnableShoppingCart ,
2018-04-26 15:09:18 +02:00
ShowCustomAmount = vm . ShowCustomAmount ,
2019-02-25 07:11:03 +01:00
ShowDiscount = vm . ShowDiscount ,
EnableTips = vm . EnableTips ,
2018-04-03 04:50:41 +02:00
Currency = vm . Currency . ToUpperInvariant ( ) ,
2018-11-17 03:39:43 +01:00
Template = vm . Template ,
ButtonText = vm . ButtonText ,
CustomButtonText = vm . CustomButtonText ,
2018-11-27 07:14:32 +01:00
CustomTipText = vm . CustomTipText ,
2018-12-19 07:07:05 +01:00
CustomTipPercentages = ListSplit ( vm . CustomTipPercentages ) ,
2019-04-11 09:14:39 +02:00
CustomCSSLink = vm . CustomCSSLink ,
NotificationUrl = vm . NotificationUrl ,
2019-04-11 11:08:42 +02:00
NotificationEmail = vm . NotificationEmail ,
2019-08-19 07:13:42 +02:00
Description = vm . Description ,
EmbeddedCSS = vm . EmbeddedCSS ,
2019-04-11 11:53:31 +02:00
RedirectAutomatically = string . IsNullOrEmpty ( vm . RedirectAutomatically ) ? ( bool? ) null : bool . Parse ( vm . RedirectAutomatically )
2019-04-11 09:14:39 +02:00
2018-04-03 04:50:41 +02:00
} ) ;
2019-09-02 15:37:52 +02:00
await _AppService . UpdateOrCreateApp ( app ) ;
2019-10-31 04:29:59 +01:00
TempData [ WellKnownTempData . SuccessMessage ] = "App updated" ;
2019-08-01 08:55:41 +02:00
return RedirectToAction ( nameof ( UpdatePointOfSale ) , new { appId } ) ;
2018-04-03 04:50:41 +02:00
}
2018-12-19 07:07:05 +01:00
private int [ ] ListSplit ( string list , string separator = "," )
{
if ( string . IsNullOrEmpty ( list ) )
{
return Array . Empty < int > ( ) ;
}
else
{
// Remove all characters except numeric and comma
Regex charsToDestroy = new Regex ( @"[^\d|\" + separator + "]" ) ;
list = charsToDestroy . Replace ( list , "" ) ;
return list . Split ( separator , System . StringSplitOptions . RemoveEmptyEntries ) . Select ( int . Parse ) . ToArray ( ) ;
}
}
2018-04-03 04:50:41 +02:00
}
}