From 60c81070c06292dc9fea980356e3f371b0dfb621 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 16 May 2017 02:16:09 +0200 Subject: [PATCH] Building successfull urls --- .../HairCut/HairCutController.cs | 3 +- Yavsc/ApiControllers/PaymentApiController.cs | 2 +- .../Haircut/HairCutCommandController.cs | 4 ++- Yavsc/Controllers/HomeController.cs | 4 --- Yavsc/Helpers/HtmlHelpers.cs | 9 ++++- Yavsc/Helpers/PayPalHelpers.cs | 33 +++++++++++++++---- .../DisplayTemplates/HairCutQuery.cshtml | 6 ++-- .../DisplayTemplates/PaypalPayment.cshtml | 22 ++++--------- Yavsc/Views/_ViewImports.cshtml | 3 ++ Yavsc/wwwroot/css/main/site.min.css | 2 +- 10 files changed, 51 insertions(+), 37 deletions(-) diff --git a/Yavsc/ApiControllers/HairCut/HairCutController.cs b/Yavsc/ApiControllers/HairCut/HairCutController.cs index 10087567..56dc185d 100644 --- a/Yavsc/ApiControllers/HairCut/HairCutController.cs +++ b/Yavsc/ApiControllers/HairCut/HairCutController.cs @@ -95,8 +95,7 @@ namespace Yavsc.ApiControllers if (query.PaymentId!=null) return new BadRequestObjectResult(new { error = "An existing payment process already exists" }); query.SelectedProfile = _context.BrusherProfile.Single(p => p.UserId == query.PerformerId); - var payment = apiContext.CreatePayment(query, "HairCutCommand", "sale", _logger); - + var payment = Request.CreatePayment("HairCutCommand",apiContext, query, "sale", _logger); switch (payment.state) { case "created": diff --git a/Yavsc/ApiControllers/PaymentApiController.cs b/Yavsc/ApiControllers/PaymentApiController.cs index 6a4a5ed4..9b9754a3 100644 --- a/Yavsc/ApiControllers/PaymentApiController.cs +++ b/Yavsc/ApiControllers/PaymentApiController.cs @@ -65,7 +65,7 @@ namespace Yavsc.ApiControllers public IActionResult Create() { var apiContext = paymentSettings.CreateAPIContext(); - Payment result=apiContext.CreatePayment(new Estimate()); + Payment result= Request.CreatePayment("Command",apiContext,new Estimate()); return Ok(Payment.Create(apiContext,result)); } diff --git a/Yavsc/Controllers/Haircut/HairCutCommandController.cs b/Yavsc/Controllers/Haircut/HairCutCommandController.cs index 315fc07c..5043ffef 100644 --- a/Yavsc/Controllers/Haircut/HairCutCommandController.cs +++ b/Yavsc/Controllers/Haircut/HairCutCommandController.cs @@ -106,7 +106,9 @@ namespace Yavsc.Controllers { return HttpNotFound(); } - + ViewBag.CreatePaymentUrl = Request.ToAbsolute("api/haircut/createpayment/"+id); + ViewBag.ExecutePaymentUrl = Request.ToAbsolute("api/payment/execute"); + ViewBag.Urls=Request.GetPaymentUrls("HairCutCommand",id.ToString()); return View(command); } diff --git a/Yavsc/Controllers/HomeController.cs b/Yavsc/Controllers/HomeController.cs index 18b434bb..f5e79fde 100644 --- a/Yavsc/Controllers/HomeController.cs +++ b/Yavsc/Controllers/HomeController.cs @@ -76,11 +76,7 @@ namespace Yavsc.Controllers .Include(a=>a.Forms).Include(a=>a.Children) .Where(a=>!a.Hidden) .Where(a=> a.ParentCode==id).OrderByDescending(a=>a.Rate)); - } - - - } public IActionResult About() { diff --git a/Yavsc/Helpers/HtmlHelpers.cs b/Yavsc/Helpers/HtmlHelpers.cs index 5e6ebe4c..c471a2f6 100644 --- a/Yavsc/Helpers/HtmlHelpers.cs +++ b/Yavsc/Helpers/HtmlHelpers.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc.Rendering; using Yavsc.Models.Drawing; @@ -11,5 +12,11 @@ namespace Yavsc.Helpers if (c==null) return new HtmlString("#000"); return new HtmlString(String.Format("#{0:X2}{1:X2}{2:X2}", c.Red, c.Green, c.Blue)); } + public static string ToAbsolute(this HttpRequest request, string url) + { + var host = request.Host; + var isSecure = request.Headers[Constants.SshHeaderKey]=="on"; + return (isSecure ? "https" : "http") + $"://{host}/{url}"; + } } -} \ No newline at end of file +} diff --git a/Yavsc/Helpers/PayPalHelpers.cs b/Yavsc/Helpers/PayPalHelpers.cs index 879567c0..cc176b5a 100644 --- a/Yavsc/Helpers/PayPalHelpers.cs +++ b/Yavsc/Helpers/PayPalHelpers.cs @@ -1,8 +1,10 @@ using System.Collections.Generic; + using Microsoft.Extensions.Logging; using PayPal.Api; using PayPal.Exception; using Yavsc.Models.Billing; +using Microsoft.AspNet.Http; namespace Yavsc.Helpers { @@ -18,8 +20,20 @@ namespace Yavsc.Helpers var apiContext = new APIContext(accessToken); return apiContext; } + public class PaymentUrls { + public string Details { get; set; } + public string Cancel { get; set; } + } + public static PaymentUrls GetPaymentUrls(this HttpRequest request, string controllerName, string id ) + { + var result =new PaymentUrls { + Details = request.ToAbsolute($"{controllerName}/Details/{id}") , + Cancel = request.ToAbsolute($"{controllerName}/ClientCancel/{id}") + }; + return result; + } - public static Payment CreatePayment(this APIContext apiContext, NominativeServiceCommand query, string controllerName, string intent = "sale", ILogger logger=null) + public static Payment CreatePayment(this HttpRequest request, string controllerName, APIContext apiContext, NominativeServiceCommand query, string intent = "sale", ILogger logger=null) { var queryType = query.GetType().Name; var transaction = new Transaction @@ -28,7 +42,10 @@ namespace Yavsc.Helpers invoice_number = query.Id.ToString(), custom = query.GetType().Name + "/"+ query.Id.ToString() }; - transaction.order_url = Startup.Audience + "/" +controllerName + "/Details/" + query.Id; + + var urls = request.GetPaymentUrls(controllerName, query.Id.ToString() ); + + transaction.order_url = urls.Details; // transaction.item_list.shipping_address.city // country_code default_address id @@ -78,8 +95,8 @@ namespace Yavsc.Helpers transactions = new List { transaction }, redirect_urls = new RedirectUrls { - return_url = Startup.Audience+ $"/{controllerName}/Details/"+query.Id.ToString(), - cancel_url = Startup.Audience+ $"/{controllerName}/ClientCancel/"+query.Id.ToString() + return_url = urls.Details, + cancel_url = urls.Cancel } }; Payment result = null; @@ -92,8 +109,9 @@ namespace Yavsc.Helpers return result; } - public static Payment CreatePayment(this APIContext apiContext, Estimate estimation) + public static Payment CreatePayment(this HttpRequest request, string controllerName, APIContext apiContext, Estimate estimation) { + var urls = request.GetPaymentUrls( controllerName, estimation.Id.ToString() ); var payment = Payment.Create(apiContext, new Payment { @@ -137,10 +155,11 @@ namespace Yavsc.Helpers }, redirect_urls = new RedirectUrls { - return_url = Startup.Audience+ "/Manage/Credit/Return", - cancel_url = Startup.Audience+ "/Manage/Credit/Cancel" + return_url = urls.Details, + cancel_url = urls.Cancel } }); + return payment; } } diff --git a/Yavsc/Views/Shared/DisplayTemplates/HairCutQuery.cshtml b/Yavsc/Views/Shared/DisplayTemplates/HairCutQuery.cshtml index 6cf25395..1d6e9307 100644 --- a/Yavsc/Views/Shared/DisplayTemplates/HairCutQuery.cshtml +++ b/Yavsc/Views/Shared/DisplayTemplates/HairCutQuery.cshtml @@ -21,10 +21,8 @@
@Html.DisplayFor(m=>m.AdditionalInfo)
@Html.DisplayNameFor(m=>m.Regularisation)
-
@Html.DisplayFor(m=>m.Regularisation,new { - PaymentUrl = "/api/haircut/createpayment/"+Model.Id.ToString(), - SuccessUrl = "/HairCutCommand/Details/"+Model.Id.ToString() - })
+
@Html.DisplayFor(m=>m.Regularisation) +
diff --git a/Yavsc/Views/Shared/DisplayTemplates/PaypalPayment.cshtml b/Yavsc/Views/Shared/DisplayTemplates/PaypalPayment.cshtml index c307b146..acacfea1 100644 --- a/Yavsc/Views/Shared/DisplayTemplates/PaypalPayment.cshtml +++ b/Yavsc/Views/Shared/DisplayTemplates/PaypalPayment.cshtml @@ -1,5 +1,5 @@ @model PaypalPayment - +@inject IOptions PayPalSettings @if (Model!=null && Model.PaypalPaymentId!=null) { @if (Model.Executor.Id == User.GetUserId()) { @@ -18,21 +18,11 @@
- - - - - -