<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using BTCPayServer.Filters;
using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using BTCPayServer.Data;

namespace BTCPayServer.Controllers
{
    public class PublicController : Controller
    {
        public PublicController(InvoiceController invoiceController,
            StoreRepository storeRepository)
        {
            _InvoiceController = invoiceController;
            _StoreRepository = storeRepository;
        }

        private InvoiceController _InvoiceController;
        private StoreRepository _StoreRepository;

        [HttpPost]
        [Route("api/v1/invoices")]
        [MediaTypeAcceptConstraintAttribute("text/html")]
        [IgnoreAntiforgeryToken]
        [EnableCors(CorsPolicies.All)]
        public async Task<IActionResult> PayButtonHandle([FromForm]PayButtonViewModel model, CancellationToken cancellationToken)
        {
            var store = await _StoreRepository.FindStore(model.StoreId);
            if (store == null)
                ModelState.AddModelError("Store", "Invalid store");
            else
            {
                var storeBlob = store.GetStoreBlob();
                if (!storeBlob.AnyoneCanInvoice)
                    ModelState.AddModelError("Store", "Store has not enabled Pay Button");
            }
            
            if (model == null || model.Price <= 0)
                ModelState.AddModelError("Price", "Price must be greater than 0");

            if (!ModelState.IsValid)
                return View();

            DataWrapper<InvoiceResponse> invoice = null;
            try
            {
                invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest()
                {
                    Price = model.Price,
                    Currency = model.Currency,
                    ItemDesc = model.CheckoutDesc,
                    OrderId = model.OrderId,
                    NotificationEmail = model.NotifyEmail,
                    NotificationURL = model.ServerIpn,
                    RedirectURL = model.BrowserRedirect,
                    FullNotifications = true
                }, store, HttpContext.Request.GetAbsoluteRoot(), cancellationToken: cancellationToken);
            }
            catch (BitpayHttpException e)
            {
                ModelState.AddModelError("Store", e.Message);
                return View();
            }

            if (string.IsNullOrEmpty(model.CheckoutQueryString))
            {
                return Redirect(invoice.Data.Url);
            }

            var additionalParamValues = HttpUtility.ParseQueryString(model.CheckoutQueryString);
            var uriBuilder = new UriBuilder(invoice.Data.Url);
            var paramValues = HttpUtility.ParseQueryString(uriBuilder.Query);
            paramValues.Add(additionalParamValues);
            uriBuilder.Query = paramValues.ToString();
            return Redirect(uriBuilder.Uri.AbsoluteUri);
        }
    }
}
<commit_msg>add GET endpoint for pay button (#1349)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using BTCPayServer.Filters;
using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using BTCPayServer.Data;

namespace BTCPayServer.Controllers
{
    public class PublicController : Controller
    {
        public PublicController(InvoiceController invoiceController,
            StoreRepository storeRepository)
        {
            _InvoiceController = invoiceController;
            _StoreRepository = storeRepository;
        }

        private InvoiceController _InvoiceController;
        private StoreRepository _StoreRepository;

        [HttpGet]
        [IgnoreAntiforgeryToken]
        [EnableCors(CorsPolicies.All)]
        [Route("api/v1/invoices")]
        public async Task<IActionResult> PayButtonHandle(PayButtonViewModel model)
        {
            return await PayButtonHandle(model, CancellationToken.None);
        }
        
        [HttpPost]
        [Route("api/v1/invoices")]
        [MediaTypeAcceptConstraintAttribute("text/html")]
        [IgnoreAntiforgeryToken]
        [EnableCors(CorsPolicies.All)]
        public async Task<IActionResult> PayButtonHandle([FromForm]PayButtonViewModel model, CancellationToken cancellationToken)
        {
            var store = await _StoreRepository.FindStore(model.StoreId);
            if (store == null)
                ModelState.AddModelError("Store", "Invalid store");
            else
            {
                var storeBlob = store.GetStoreBlob();
                if (!storeBlob.AnyoneCanInvoice)
                    ModelState.AddModelError("Store", "Store has not enabled Pay Button");
            }
            
            if (model == null || model.Price <= 0)
                ModelState.AddModelError("Price", "Price must be greater than 0");

            if (!ModelState.IsValid)
                return View();

            DataWrapper<InvoiceResponse> invoice = null;
            try
            {
                invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest()
                {
                    Price = model.Price,
                    Currency = model.Currency,
                    ItemDesc = model.CheckoutDesc,
                    OrderId = model.OrderId,
                    NotificationEmail = model.NotifyEmail,
                    NotificationURL = model.ServerIpn,
                    RedirectURL = model.BrowserRedirect,
                    FullNotifications = true
                }, store, HttpContext.Request.GetAbsoluteRoot(), cancellationToken: cancellationToken);
            }
            catch (BitpayHttpException e)
            {
                ModelState.AddModelError("Store", e.Message);
                return View();
            }

            if (string.IsNullOrEmpty(model.CheckoutQueryString))
            {
                return Redirect(invoice.Data.Url);
            }

            var additionalParamValues = HttpUtility.ParseQueryString(model.CheckoutQueryString);
            var uriBuilder = new UriBuilder(invoice.Data.Url);
            var paramValues = HttpUtility.ParseQueryString(uriBuilder.Query);
            paramValues.Add(additionalParamValues);
            uriBuilder.Query = paramValues.ToString();
            return Redirect(uriBuilder.Uri.AbsoluteUri);
        }
    }
}

{'fraction_non_alphanumeric': 0.07979351032448377, 'fraction_numerical': 0.0016224188790560472, 'mean_word_length': 2.0613995485327314, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4032231', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1627, 'n_words': 323}