public static IEnumerable <KeyValuePair <string, string> > AsEnumerable(this QueryOrderResult model) { return(new Dictionary <string, string> { ["Result"] = model.Result, ["Message"] = model.Message, ["AppId"] = model.AppId, ["OrderId"] = model.OrderId, ["Status"] = model.Status, ["NonceStr"] = model.NonceStr, ["LastUpdated"] = model.LastUpdated }.AsEnumerable()); }
public async Task <IActionResult> Index(Guid id) { System.Console.WriteLine($"Id:{id}"); var order = await context.Orders.SingleOrDefaultAsync(x => x.Id == id); if (order == null) { throw new Exception("No order found."); } if (order.Status != OrderStatus.Preparing) { //throw new Exception("Order has been processed."); return(RedirectToAction("Result", "Payment", new { Id = id })); } try { var receipt = await paymentService.PlaceNativeOrder(order.Description, order.OrderId, order.Amount, "http://pay.gz-eport.com/WxPay/WxPayCallback"); order.Channel = "wxpay"; order.DateIssued = DateTime.Now; ViewBag.Url = receipt.QRCodeUrl; ViewBag.Id = id; await context.SaveChangesAsync(); return(View()); } catch (WxPayLib.WxPayBusinessException ex) { if (ex.ErrorCode == "ORDERPAID") { // Order paid, but server hasn't received callback yet. // So explicitly query order status. var status = await paymentService.QueryOrder(order.ChannelOrderId, order.OrderId); UpdateOrderStatus(status, ref order); await context.SaveChangesAsync(); return(RedirectToAction("Result", "Payment", new { Id = order.Id })); } else { if (order.NotifyUrl != null) { // TODO: 微信报告订单号重复 var result = new GZCDCPay.Models.QueryOrderResult() { Result = "FAIL", Message = ex.ErrorCode, AppId = order.AppId, OrderId = order.OrderId, NonceStr = signatureService.GenerateNonceStr(), LastUpdated = DateTime.Now.ToUniversalTime().AddHours(8).ToString("yyyyMMddHHmmss") }; result.Signature = signatureService.CalculateSignature(result.AsEnumerable(), order.AppId); // ********** this should enqueue, but not send immediately await notifyService.NotifyAsync(order.NotifyUrl, result); // ********* } order.Status = OrderStatus.Errored; context.SaveChanges(); return(RedirectToAction("Result", "Payment", new { Id = order.Id })); } } }