Skip to content

Commit

Permalink
#1085. An error occurs when paying for an order (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
s04v committed Feb 24, 2024
1 parent 7fb6d40 commit 3f389c8
Show file tree
Hide file tree
Showing 21 changed files with 80 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public async Task<IViewComponentResult> InvokeAsync(Guid checkoutId)
{
ClientToken = await _braintreeConfiguration.GetClientToken(),
Amount = zeroDecimalAmount,
ISOCurrencyCode = regionInfo.ISOCurrencySymbol
ISOCurrencyCode = regionInfo.ISOCurrencySymbol,
CheckoutId = checkoutId,
};

return View(this.GetViewPath(), model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ public class BraintreeController : Controller
}

[HttpPost]
public async Task<IActionResult> Charge(string nonce)
public async Task<IActionResult> Charge(string nonce, Guid checkoutId)
{
var gateway = await _braintreeConfiguration.BraintreeGateway();

var curentUser = await _workContext.GetCurrentUser();
//TODO: pass checkout Id here
var cart = await _checkoutService.GetCheckoutDetails(Guid.NewGuid());

var cart = await _checkoutService.GetCheckoutDetails(checkoutId);
if(cart == null)
{
return NotFound();
}
var checkoutId = Guid.NewGuid();
var orderCreateResult = await _orderService.CreateOrder(checkoutId, PaymentProviderHelper.BraintreeProviderId, 0, OrderStatus.PendingPayment);

if (!orderCreateResult.Success)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SimplCommerce.Module.PaymentBraintree.Areas.PaymentBraintree.ViewModels
using System;

namespace SimplCommerce.Module.PaymentBraintree.Areas.PaymentBraintree.ViewModels
{
public class BraintreeCheckoutForm
{
Expand All @@ -7,5 +9,7 @@ public class BraintreeCheckoutForm
public decimal Amount { get; set; }

public string ISOCurrencyCode { get; set; }

public Guid CheckoutId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
var request = $.ajax({
type: 'POST',
url: '@Url.Action("Charge", "Braintree", new { Area = "PaymentBraintree" })',
data: { nonce: payload.nonce }
data: { nonce: payload.nonce, checkoutId: "@Model.CheckoutId" }
})
.done(function (data) {
window.location = "/checkout/success?orderId="+ data.orderId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
Expand All @@ -19,13 +20,14 @@ public MomoLandingViewComponent(IRepositoryWithTypedId<PaymentProvider, string>
_paymentProviderRepository = paymentProviderRepository;
}

public async Task<IViewComponentResult> InvokeAsync()
public async Task<IViewComponentResult> InvokeAsync(Guid checkoutId)
{
var momoProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.MomoPaymentProviderId);
var momoSetting = JsonConvert.DeserializeObject<MomoPaymentConfigForm>(momoProvider.AdditionalSettings);

var model = new MomoCheckoutForm();
model.PaymentFee = momoSetting.PaymentFee;
model.CheckoutId = checkoutId;

return View(this.GetViewPath(), model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public class MomoPaymentController : Controller
_setting = new Lazy<MomoPaymentConfigForm>(GetSetting());
}

public async Task<IActionResult> MomoCheckout()
[HttpPost]
public async Task<IActionResult> MomoCheckout(Guid checkoutId)
{
var currentUser = await _workContext.GetCurrentUser();
//TODO: pass checkout Id here
var cart = await _checkoutService.GetCheckoutDetails(Guid.Empty);

var cart = await _checkoutService.GetCheckoutDetails(checkoutId);
if (cart == null)
{
return NotFound();
}

var checkoutId = Guid.NewGuid();
var orderCreateResult = await _orderService.CreateOrder(checkoutId, "MomoPayment", 0, OrderStatus.PendingPayment);

if (!orderCreateResult.Success)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<label>Momo payment</label>
@model SimplCommerce.Module.PaymentMomo.ViewModels.MomoCheckoutForm

<label>Momo payment</label>
<form asp-area="PaymentMomo" asp-controller="MomoPayment" asp-action="MomoCheckout" method="POST" id="checkout-payment" class="form-horizontal">
<div class="form-group">
<div class="col-md-12">
<input type="hidden" value="@Model.CheckoutId" name="checkoutId" />
<button type="submit" class="btn btn-order">@Localizer["Momo payment"]</button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace SimplCommerce.Module.PaymentMomo.ViewModels
using System;

namespace SimplCommerce.Module.PaymentMomo.ViewModels
{
public class MomoCheckoutForm
{
public decimal PaymentFee { get; set; }

public Guid CheckoutId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
Expand All @@ -19,12 +20,12 @@ public NganLuongLandingViewComponent(IRepositoryWithTypedId<PaymentProvider, str
_paymentProviderRepository = paymentProviderRepository;
}

public async Task<IViewComponentResult> InvokeAsync()
public async Task<IViewComponentResult> InvokeAsync(Guid checkoutId)
{
var nganLuongProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.NganLuongPaymentProviderId);
var nganLuongSetting = JsonConvert.DeserializeObject<NganLuongConfigForm>(nganLuongProvider.AdditionalSettings);

return View(this.GetViewPath());
return View(this.GetViewPath(), checkoutId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ public IActionResult PaymentMethods()
}

[HttpPost]
public async Task<IActionResult> SubmitPayment(string paymentOption, string bankCode)
public async Task<IActionResult> SubmitPayment(string paymentOption, string bankCode, Guid checkoutId)
{
var currentUser = await _workContext.GetCurrentUser();
//TODO: pass checkout Id here
var cart = await _checkoutService.GetCheckoutDetails(Guid.Empty);

var cart = await _checkoutService.GetCheckoutDetails(checkoutId);
if (cart == null)
{
return NotFound();
}

var checkoutId = Guid.NewGuid();
var orderCreateResult = await _orderService.CreateOrder(checkoutId, "NganLuong", 0, OrderStatus.PendingPayment);

if (!orderCreateResult.Success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
</div>
</div>
</div>
<input name="checkoutId" value="" type="hidden"/>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Thanh toán</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<label>Ngân hàng nội địa, Visa, Master (Ngân lượng)</label>
@model Guid

<label>Ngân hàng nội địa, Visa, Master (Ngân lượng)</label>
<button type="button" id="nganluongPayment" class="btn btn-order">Chọn ngân hàng</button>

<script>
Expand All @@ -11,6 +13,12 @@
}).done(function (data) {
$('#shopModal').find('.modal-content').html(data);
$('#shopModal').modal('show');
const checkoutIdInput = $('#shopModal').find('.modal-content input[name=checkoutId]')
if (checkoutIdInput.length) {
checkoutIdInput[0].value = "@Model";
}
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
Expand All @@ -19,14 +20,15 @@ public PaypalExpressLandingViewComponent(IRepositoryWithTypedId<PaymentProvider,
_paymentProviderRepository = paymentProviderRepository;
}

public async Task<IViewComponentResult> InvokeAsync()
public async Task<IViewComponentResult> InvokeAsync(Guid checkoutId)
{
var paypalExpressProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.PaypalExpressProviderId);
var paypalExpressSetting = JsonConvert.DeserializeObject<PaypalExpressConfigForm>(paypalExpressProvider.AdditionalSettings);

var model = new PaypalExpressCheckoutForm();
model.Environment = paypalExpressSetting.Environment;
model.PaymentFee = paypalExpressSetting.PaymentFee;
model.CheckoutId = checkoutId;

return View(this.GetViewPath(), model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public class PaypalExpressController : Controller
}

[HttpPost("PaypalExpress/CreatePayment")]
public async Task<ActionResult> CreatePayment()
public async Task<ActionResult> CreatePayment(Guid checkoutId)
{
var hostingDomain = Request.Host.Value;
var accessToken = await GetAccessToken();
var currentUser = await _workContext.GetCurrentUser();
//TODO: pass checkout Id here
var cart = await _checkoutService.GetCheckoutDetails(Guid.Empty);

var cart = await _checkoutService.GetCheckoutDetails(checkoutId);
if(cart == null)
{
return NotFound();
Expand Down Expand Up @@ -121,10 +121,10 @@ public async Task<ActionResult> ExecutePayment(PaymentExecuteVm model)
{
var accessToken = await GetAccessToken();
var currentUser = await _workContext.GetCurrentUser();
//TODO: pass checkout Id here
var cart = await _checkoutService.GetCheckoutDetails(Guid.Empty);
var checkoutId = Guid.NewGuid();
var orderCreateResult = await _orderService.CreateOrder(checkoutId, "PaypalExpress", CalculatePaymentFee(cart.OrderTotal), OrderStatus.PendingPayment);

var cart = await _checkoutService.GetCheckoutDetails(model.CheckoutId);

var orderCreateResult = await _orderService.CreateOrder(model.CheckoutId, "PaypalExpress", CalculatePaymentFee(cart.OrderTotal), OrderStatus.PendingPayment);
if (!orderCreateResult.Success)
{
return BadRequest(orderCreateResult.Error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
payment: function (data, actions) {
return paypal.request.post('/PaypalExpress/CreatePayment')
return paypal.request.post('/PaypalExpress/CreatePayment', { checkoutId: "@Model.CheckoutId" })
.then(function (res) {
return res.paymentId;
})
Expand All @@ -32,7 +32,8 @@
onAuthorize: function (data, actions) {
return paypal.request.post('/PaypalExpress/ExecutePayment', {
paymentID: data.paymentID,
payerID: data.payerID
payerID: data.payerID,
checkoutId: "@Model.CheckoutId"
})
.then(function (res) {
window.location = "/checkout/success?orderId=" + res.orderId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace SimplCommerce.Module.PaymentPaypalExpress.ViewModels
using System;

namespace SimplCommerce.Module.PaymentPaypalExpress.ViewModels
{
public class PaymentExecuteVm
{
public string paymentID { get; set; }

public string payerID { get; set; }

public Guid CheckoutId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace SimplCommerce.Module.PaymentPaypalExpress.ViewModels
using System;

namespace SimplCommerce.Module.PaymentPaypalExpress.ViewModels
{
public class PaypalExpressCheckoutForm
{
public string Environment { get; set; }

public decimal PaymentFee { get; set; }

public Guid CheckoutId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public async Task<IViewComponentResult> InvokeAsync(Guid checkoutId)
model.PublicKey = stripeSetting.PublicKey;
model.Amount = (long)zeroDecimalAmount;
model.ISOCurrencyCode = regionInfo.ISOCurrencySymbol;
model.CheckoutId = checkoutId;

return View(this.GetViewPath(), model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ public class StripeController : Controller
_currencyService = currencyService;
}

public async Task<IActionResult> Charge(string stripeEmail, string stripeToken)
public async Task<IActionResult> Charge(string stripeEmail, string stripeToken, Guid checkoutId)
{
var stripeProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.StripeProviderId);
var stripeSetting = JsonConvert.DeserializeObject<StripeConfigForm>(stripeProvider.AdditionalSettings);
var stripeChargeService = new ChargeService(stripeSetting.PrivateKey);
var currentUser = await _workContext.GetCurrentUser();
//TODO: pass checkout Id here
var cart = await _checkoutService.GetCheckoutDetails(Guid.Empty);

var cart = await _checkoutService.GetCheckoutDetails(checkoutId);
if (cart == null)
{
return NotFound();
}

var checkoutId = Guid.NewGuid();
var orderCreationResult = await _orderService.CreateOrder(checkoutId, "Stripe", 0, OrderStatus.PendingPayment);
if(!orderCreationResult.Success)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SimplCommerce.Module.PaymentStripe.Areas.PaymentStripe.ViewModels
using System;

namespace SimplCommerce.Module.PaymentStripe.Areas.PaymentStripe.ViewModels
{
public class StripeCheckoutForm
{
Expand All @@ -7,5 +9,7 @@ public class StripeCheckoutForm
public long Amount { get; set; }

public string ISOCurrencyCode { get; set; }

public Guid CheckoutId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<label>Stripe</label>
<form asp-area="PaymentStripe" asp-controller="Stripe" asp-action="Charge" method="POST">
<input name="checkoutId" value="@Model.CheckoutId" type="hidden" />

<script src="//checkout.stripe.com/v2/checkout.js"
class="stripe-button"
data-key="@Model.PublicKey"
Expand Down

0 comments on commit 3f389c8

Please sign in to comment.