public async Task <IActionResult> UnifiePayment(int id, [FromQuery] int businessId, [FromQuery] int userId, [FromServices] AppData appData) { var business = await Service.GetAsync <Business>(businessId); var user = await Service.GetAsync <User>(userId); var order = Service.GetOrderIncludeProduct(id); var option = new WxUnifiePayment { appid = business.PayServerAppId, mch_id = business.PayServerMchId, sub_appid = business.AppId, sub_mch_id = business.MchId, sub_openid = user.OpenId, out_trade_no = order.OrderCode, total_fee = (int)Math.Round(order.Price.Value * 100, 0), key = business.PayServerKey, notify_url = appData.PaySuccessUrl, spbill_create_ip = appData.HostIpAddress }; if (appData.RunMode == "test" || business.ID == 1) { option.total_fee = 1; } option.Generator(); var xml = string.Empty; using (MemoryStream stream = new MemoryStream()) { UtilHelper.XmlSerializeInternal(stream, option); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { xml = reader.ReadToEnd(); } } using (var hc = new HttpClient()) { var sc = new StringContent(xml); var response = await hc.PostAsync(unifieUrl, sc); var content = await response.Content.ReadAsStringAsync(); var ret = UtilHelper.ReadXml <WxUnifieResult>(content); var result = new JsonData(); if (ret.return_code == "FAIL") { result.Msg = ret.return_msg; } else if (ret.result_code == "FAIL") { result.Msg = ret.err_code_des; } else { result.Success = true; var payment = new WxPayment { appId = business.AppId, package = "prepay_id=" + ret.prepay_id, key = business.PayServerKey }; // 保存支付标识码 order.PrepayId = ret.prepay_id; Service.Commit(); payment.Generator(); result.Data = payment; } return(Json(result)); } }
public async Task <IActionResult> UnifiedOrder([FromQuery] int id, [FromBody] ChargeRecord record, [FromServices] AppData appData) { var member = await Service.GetAsync <WxMember>(id); var business = await Service.GetAsync <Business>(member.BusinessId); record.BusinessId = business.ID; record.Code = $"{DateTime.Now:yyyyMMddHHmmss}0{UtilHelper.RandNum(8)}"; record.RelativeId = member.ID; await Service.AddAsync(record); var option = new WxUnifiePayment { appid = business.PayServerAppId, mch_id = business.PayServerMchId, sub_appid = business.WeChatAppId, sub_mch_id = business.MchId, device_info = "WEB", body = $"{business.Name}-会员充值", attach = record.ID.ToString(), out_trade_no = record.Code, total_fee = record.Amount, key = business.PayServerKey, notify_url = appData.Domain + "/api/card/paySuccess", spbill_create_ip = appData.HostIpAddress, sub_openid = record.OpenId }; Log.Debug(option.notify_url); if (appData.RunMode == "test" || business.ID == 1) { option.total_fee = 1; } option.Generator(); var xml = string.Empty; using (var stream = new MemoryStream()) { UtilHelper.XmlSerializeInternal(stream, option); stream.Position = 0; using (var reader = new StreamReader(stream)) { xml = reader.ReadToEnd(); } } var ret = await WxHelper.UnifiedOrderAsync(xml); var result = new JsonData(); if (ret.return_code == "FAIL") { result.Msg = ret.return_msg; } else if (ret.result_code == "FAIL") { result.Msg = ret.err_code_des; } else { result.Success = true; var payment = new WxPayment { appId = business.WeChatAppId, package = "prepay_id=" + ret.prepay_id, key = business.PayServerKey }; // 保存支付标识码 record.PrepayId = ret.prepay_id; Service.Commit(); payment.Generator(); result.Data = payment; } return(Json(result)); }