tijian_tieying/web/dccdc/Controllers/Charge3Controller.cs
2025-02-20 12:14:39 +08:00

600 lines
25 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Com.Alipay;
using Com.Alipay.Business;
using Com.Alipay.Domain;
using Com.Alipay.Model;
using dccdc.BLL;
using dccdc.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using WxPayAPI;
using static dccdc.Controllers.DictionariesController;
namespace dccdc.Controllers
{
public class Charge3Controller : Controller
{
// GET: Charge3
public ActionResult Index()
{
ViewBag.bm = (Session["loginUser"] as Models.ERPUser).Department;
ViewBag.jfbm = new SelectList(new ERPBuMenBll().GetAllList(""), "bumenname", "bumenname");
ViewBag.jffs = new SelectList(new List<Droplist> { new Droplist { Value = "现金", Title = "现金" }, new Droplist { Value = "转账", Title = "转账" }, new Droplist { Value = "微信", Title = "微信" }, new Droplist { Value = "支付宝", Title = "支付宝" }, new Droplist { Value = "医保卡", Title = "医保卡" } }, "Value", "Title");
return View();
}
public ActionResult getSFLBData(string start, string end, string jfbm, string where, string print)
{
if (string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end))
return Json(null);
var bll = new chargeBll();
if (string.IsNullOrEmpty(print))
print = "未打印";
var result = bll.GetAllList2(start, end, jfbm, where, print);
return Json(new { Rows = result });
}
public string getSFLBDataCount()
{
var bll = new invoice_printBll();
var result = bll.GetPersonCount((Session["loginUser"] as Models.ERPUser).TrueName);
StringBuilder result1 = new StringBuilder();
foreach (string key in result.Keys)
{
result1.Append(key + ":【" + result[key] + "】 ");
}
return result1.ToString();
}
public ActionResult getSFLB2Data(string chargeid)
{
if (string.IsNullOrEmpty(chargeid))
return Json(null);
var bll = new chargeBll();
var result = bll.GetAllList2List(chargeid);
//合并打印项目
var result2 = result.Where(t => (t.pid == null || t.pid <= 0)).ToList();
foreach (charge_detailModel model in result)
{
if (model.pid > 0)
{
foreach (charge_detailModel model2 in result2)
{
if (model2.id == model.pid)
{
model2.money += model.money;
break;
}
}
}
}
return Json(new { Rows = result2 });
}
public string getInvoice(string sort)
{
var bll = new chargeBll();
var result = bll.getInvoice((Session["loginUser"] as Models.ERPUser).TrueName, sort);
return result;
}
public string getzfstate(string id)
{
var bll = new chargeBll();
var cm = bll.getModel(id);
int state;
var wxresult = Query(cm.zfid, out state);
Log.Debug("MicroPay", wxresult.ToXml());
if(state==1)
{
bll.UpdateZfzt(cm.zfid);
return Newtonsoft.Json.JsonConvert.SerializeObject(new { State = 1, Message = "订单支付成功!" });
}
else if(state==2)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new { State = 2, Message = "订单支付中请稍后再查询!" });
}
else
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new { State = 0, Message = "订单支付失败请重新支付!" });
}
}
public ActionResult opPrint(string fpdid, string fplx, string fpbh, string jfid, string jfry, string jfdw, string jffs, string jfbm, string jfje, string bz, string zfm, string items)
{
if (string.IsNullOrEmpty(fpbh) || string.IsNullOrEmpty(jfry))
return Json(null);
var bll = new chargeBll();
var cm = bll.getModel(jfid);
cm.method = jffs;
if (cm.status == "未缴费" && (cm.method == "微信" || cm.method == "支付宝"))
{
if (string.IsNullOrEmpty(cm.zfid))
{
cm.zfid = WxPayAPI.WxPayApi.GenerateOutTradeNo();
bll.UpdateZfCode(cm.id.ToString(), cm.zfid);
}
if (cm.method == "微信")
{
WxPayData data = new WxPayData();
data.SetValue("auth_code", zfm);//授权码
data.SetValue("body", cm.person + cm.id);//商品描述
data.SetValue("total_fee", (int)(decimal.Parse(jfje) * 100));//总金额
data.SetValue("out_trade_no", cm.zfid);//产生随机的商户订单号
WxPayData wxresult = WxPayApi.Micropay(data, 10); //提交被扫支付,接收返回结果
//如果提交被扫支付接口调用失败,则抛异常
if (!wxresult.IsSet("return_code")
|| wxresult.GetValue("return_code").ToString() == "FAIL")
{
string returnMsg = wxresult.IsSet("return_msg") ? wxresult.GetValue("return_msg").ToString() : "";
Log.Error("MicroPay", "Micropay API interface call failure, result : " + wxresult.ToXml());
//throw new WxPayException("Micropay API interface call failure, return_msg : " + returnMsg);
return Json(new { State = 0, Message = "Micropay API interface call failure, return_msg : " + returnMsg });
}
//签名验证
wxresult.CheckSign();
Log.Debug("MicroPay", "Micropay response check sign success");
//刷卡支付直接成功
if (wxresult.GetValue("return_code").ToString() == "SUCCESS" &&
wxresult.GetValue("result_code").ToString() == "SUCCESS")
{
Log.Debug("MicroPay", "Micropay business success, result : " + wxresult.ToXml());
//return result.ToPrintStr();
bll.UpdateZfzt(cm.id.ToString());
goto aa;
}
/******************************************************************
* 剩下的都是接口调用成功,业务失败的情况
* ****************************************************************/
//1业务结果明确失败
if (wxresult.GetValue("err_code").ToString() != "USERPAYING" &&
wxresult.GetValue("err_code").ToString() != "SYSTEMERROR")
{
Log.Error("MicroPay", "micropay API interface call success, business failure, result : " + wxresult.ToXml());
cm.zfid = WxPayAPI.WxPayApi.GenerateOutTradeNo();
bll.UpdateZfCode(cm.id.ToString(), cm.zfid);
//return result.ToPrintStr();
return Json(new { State = 0, Message = "micropay API interface call success, business failure, result : " + wxresult.ToXml() });
}
//2不能确定是否失败需查单
//用商户订单号去查单
string out_trade_no = data.GetValue("out_trade_no").ToString();
Thread.Sleep(5000);
//确认支付是否成功,每隔一段时间查询一次订单共查询10次
int queryTimes = 10;//查询次数计数器
while (queryTimes-- > 0)
{
int succResult = 0;//查询结果
WxPayData queryResult = Query(out_trade_no, out succResult);
//如果需要继续查询则等待2s后继续
if (succResult == 2)
{
Thread.Sleep(3000);
continue;
}
//查询成功,返回订单查询接口返回的数据
else if (succResult == 1)
{
Log.Debug("MicroPay", "Mircopay success, return order query result : " + queryResult.ToXml());
bll.UpdateZfzt(cm.id.ToString());
goto aa;
}
//订单交易失败直接返回刷卡支付接口返回的结果失败原因会在err_code中描述
else
{
Log.Error("MicroPay", "Micropay failure, return micropay result : " + wxresult.ToXml());
return Json(new { State = 0, Message = "Micropay failure, return micropay result : " + wxresult.ToXml() });
}
}
//确认失败,则撤销订单
Log.Error("MicroPay", "Micropay failure, Reverse order is processing...");
Log.Error("MicroPay", "Reverse order failure"+ wxresult.ToXml());
cm.zfid = WxPayAPI.WxPayApi.GenerateOutTradeNo();
bll.UpdateZfCode(cm.id.ToString(), cm.zfid);
return Json(new { State = 0, Message = "Reverse order failure" + wxresult.ToXml() });
}
else
{
IAlipayTradeService serviceClient = F2FBiz.CreateClientInstance(Com.Alipay.Config.serverUrl, Com.Alipay.Config.appId, Com.Alipay.Config.merchant_private_key, Com.Alipay.Config.version,
Com.Alipay.Config.sign_type, Com.Alipay.Config.alipay_public_key, Com.Alipay.Config.charset);
AlipayTradePayContentBuilder builder = BuildPayContent(jfry, jfje, cm.zfid, zfm, cm.id, (Session["loginUser"] as Models.ERPUser), items);
string out_trade_no = builder.out_trade_no;
AlipayF2FPayResult payResult = serviceClient.tradePay(builder);
switch (payResult.Status)
{
case ResultEnum.SUCCESS:
bll.UpdateZfzt(cm.zfid, "支付宝");
//DoSuccessProcess(payResult);
goto aa;
case ResultEnum.FAILED:
//DoFailedProcess(payResult);
return Json(new { State = 0, Message = payResult.response });
case ResultEnum.UNKNOWN:
return Json(new { State = 0, Message = payResult.response });
}
}
}
aa:
var result = bll.opPrint(fpdid, fplx, fpbh, jfid, jfry, jfdw, jffs, jfbm, jfje, bz, items, (Session["loginUser"] as Models.ERPUser).TrueName, zfm);// Session["loginUser"] as Models.ERPUser
return Json(result);
}
public string bdfp(string id,string nfph)
{
Models.ERPUser user = Session["loginUser"] as Models.ERPUser;
var dal = new DAL.chargeDal();
return Newtonsoft.Json.JsonConvert.SerializeObject(dal.bdfp(id,nfph,user.ID,user.TrueName));
}
/// <summary>
/// 构造支付请求数据
/// </summary>
/// <returns>请求数据集</returns>
private AlipayTradePayContentBuilder BuildPayContent(string jfry, string jfje, string zfid, string zfm, int id, Models.ERPUser u, string items)
{
//线上联调时,请输入真实的外部订单号。
string out_trade_no = zfid;
//扫码枪扫描到的用户手机钱包中的付款条码
AlipayTradePayContentBuilder builder = new AlipayTradePayContentBuilder();
//收款账号
builder.seller_id = Com.Alipay.Config.pid;
//订单编号
builder.out_trade_no = out_trade_no;
//支付场景,无需修改
builder.scene = "bar_code";
//支付授权码,付款码
builder.auth_code = zfm;
//订单总金额
builder.total_amount = jfje;
//参与优惠计算的金额
//builder.discountable_amount = "";
//不参与优惠计算的金额
//builder.undiscountable_amount = "";
//订单名称
builder.subject = jfry + id;
//自定义超时时间
builder.timeout_express = "2m";
//订单描述
builder.body = "";
//门店编号,很重要的参数,可以用作之后的营销
builder.store_id = "";
//操作员编号,很重要的参数,可以用作之后的营销
builder.operator_id = u.ID.ToString();
//传入商品信息详情
List<GoodsInfo> gList = new List<GoodsInfo>();
foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
string[] itemss = item.Split('|');
if (itemss.Length == 4)
{
GoodsInfo goods = new GoodsInfo();
goods.goods_id = itemss[0];
goods.goods_name = itemss[1];
goods.price = itemss[2];
goods.quantity = itemss[3];
gList.Add(goods);
/*
m = new charge_project_detailModel();
m.invoice_print_id = invoiceid;
m.charge_project = itemss[1];
m.unit_price = Convert.ToDecimal(itemss[2]);
m.charge_num = Convert.ToInt32(itemss[3]);
m.charge_price = Convert.ToDecimal(itemss[2]) * Convert.ToInt32(itemss[3]);
models.Add(m);*/
}
}
builder.goods_detail = gList;
//系统商接入可以填此参数用作返佣
//ExtendParams exParam = new ExtendParams();
//exParam.sysServiceProviderId = "20880000000000";
//builder.extendParams = exParam;
return builder;
}
public static WxPayData Query(string out_trade_no, out int succCode)
{
WxPayData queryOrderInput = new WxPayData();
queryOrderInput.SetValue("out_trade_no", out_trade_no);
WxPayData result = WxPayApi.OrderQuery(queryOrderInput);
if (result.GetValue("return_code").ToString() == "SUCCESS"
&& result.GetValue("result_code").ToString() == "SUCCESS")
{
//支付成功
if (result.GetValue("trade_state").ToString() == "SUCCESS")
{
succCode = 1;
return result;
}
//用户支付中,需要继续查询
else if (result.GetValue("trade_state").ToString() == "USERPAYING")
{
succCode = 2;
return result;
}
}
//if()
if(result.IsSet("trade_state")&&result.GetValue("trade_state").ToString()== "NOTPAY")
{
succCode = 0;
return result;
}
//如果返回错误码为“此交易订单号不存在”则直接认定失败
if (result.IsSet("err_code") && result.GetValue("err_code").ToString() == "ORDERNOTEXIST")
{
succCode = 0;
}
else
{
//如果是系统错误,则后续继续
succCode = 2;
}
return result;
}
[AllowAnonymous]
//打印用
public JsonResult getOne(string id)
{
if (string.IsNullOrEmpty(id))
return Json(null);
var bll = new invoice_printBll();
var result = bll.GetAllList(id);
result[0].moneyupper = MoneyToUpper(result[0].invoice_price.ToString());
return Json(result, JsonRequestBehavior.AllowGet);
}
[AllowAnonymous]
public JsonResult getDetail(string id)
{
if (string.IsNullOrEmpty(id))
return Json(null);
var bll = new invoice_printBll();
var result = bll.GetDetailList(id);
return Json(result, JsonRequestBehavior.AllowGet);
}
//补打
public ActionResult Charge3Message()
{
ViewBag.jfbm = new SelectList(new ERPBuMenBll().GetAllList(""), "bumenname", "bumenname", (Session["loginUser"] as Models.ERPUser).Department);
return View();
}
public string getMessageData(string start, string end, string jfbm, string where)
{
string truename = (Session["loginUser"] as Models.ERPUser).TrueName;
if (string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end))
return Newtonsoft.Json.JsonConvert.SerializeObject(null);
var bll = new invoice_printBll();
var result = bll.GetAllList2(start, end, jfbm, where, truename);
return Newtonsoft.Json.JsonConvert.SerializeObject(new { Rows = result });
}
//补打2
public ActionResult Charge3Message2()
{
ViewBag.jfbm = new SelectList(new ERPBuMenBll().GetAllList(""), "bumenname", "bumenname", (Session["loginUser"] as Models.ERPUser).Department);
ViewBag.sfr = new SelectList(new invoice_printBll().GetPerson(), "", "", (Session["loginUser"] as Models.ERPUser).Department);
return View();
}
public string getMessageData2(string start, string end, string jfbm, string where, string hstart, string hend)
{
var bll = new invoice_printBll();
var result = bll.GetAllList2(start, end, jfbm, where, "",hstart,hend);
return Newtonsoft.Json.JsonConvert.SerializeObject(new { Rows = result });
}
//作废发票
public ActionResult cancelInvoice(string id)
{
if (string.IsNullOrEmpty(id))
return Json(null);
var bll = new invoice_printBll();
var result = bll.cancelInvoice(id, Session["loginUser"] as Models.ERPUser);
return Json(result);
}
//金额转大写
public static string MoneyToUpper(string strAmount)
{
string functionReturnValue = null;
bool IsNegative = false; // 是否是负数
if (strAmount.Trim().Substring(0, 1) == "-")
{
// 是负数则先转为正数
strAmount = strAmount.Trim().Remove(0, 1);
IsNegative = true;
}
string strLower = null;
string strUpart = null;
string strUpper = null;
int iTemp = 0;
// 保留两位小数 123.489→123.49  123.4→123.4
strAmount = Math.Round(double.Parse(strAmount), 2).ToString();
if (strAmount.IndexOf(".") > 0)
{
if (strAmount.IndexOf(".") == strAmount.Length - 2)
{
strAmount = strAmount + "0";
}
}
else
{
strAmount = strAmount + ".00";
}
strLower = strAmount;
iTemp = 1;
strUpper = "";
while (iTemp <= strLower.Length)
{
switch (strLower.Substring(strLower.Length - iTemp, 1))
{
case ".":
strUpart = "元";
break;
case "0":
strUpart = "零";
break;
case "1":
strUpart = "壹";
break;
case "2":
strUpart = "贰";
break;
case "3":
strUpart = "叁";
break;
case "4":
strUpart = "肆";
break;
case "5":
strUpart = "伍";
break;
case "6":
strUpart = "陆";
break;
case "7":
strUpart = "柒";
break;
case "8":
strUpart = "捌";
break;
case "9":
strUpart = "玖";
break;
}
switch (iTemp)
{
case 1:
strUpart = strUpart + "分";
break;
case 2:
strUpart = strUpart + "角";
break;
case 3:
strUpart = strUpart + "";
break;
case 4:
strUpart = strUpart + "";
break;
case 5:
strUpart = strUpart + "拾";
break;
case 6:
strUpart = strUpart + "佰";
break;
case 7:
strUpart = strUpart + "仟";
break;
case 8:
strUpart = strUpart + "万";
break;
case 9:
strUpart = strUpart + "拾";
break;
case 10:
strUpart = strUpart + "佰";
break;
case 11:
strUpart = strUpart + "仟";
break;
case 12:
strUpart = strUpart + "亿";
break;
case 13:
strUpart = strUpart + "拾";
break;
case 14:
strUpart = strUpart + "佰";
break;
case 15:
strUpart = strUpart + "仟";
break;
case 16:
strUpart = strUpart + "万";
break;
default:
strUpart = strUpart + "";
break;
}
strUpper = strUpart + strUpper;
iTemp = iTemp + 1;
}
strUpper = strUpper.Replace("零拾", "零");
strUpper = strUpper.Replace("零佰", "零");
strUpper = strUpper.Replace("零仟", "零");
strUpper = strUpper.Replace("零零零", "零");
strUpper = strUpper.Replace("零零", "零");
strUpper = strUpper.Replace("零角零分", "整");
strUpper = strUpper.Replace("零分", "整");
strUpper = strUpper.Replace("零角", "零");
strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
strUpper = strUpper.Replace("亿零万零圆", "亿圆");
strUpper = strUpper.Replace("零亿零万", "亿");
strUpper = strUpper.Replace("零万零圆", "万圆");
strUpper = strUpper.Replace("零亿", "亿");
strUpper = strUpper.Replace("零万", "万");
strUpper = strUpper.Replace("零圆", "圆");
strUpper = strUpper.Replace("零零", "零");
// 对壹圆以下的金额的处理
if (strUpper.Substring(0, 1) == "圆")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
}
if (strUpper.Substring(0, 1) == "零")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
}
if (strUpper.Substring(0, 1) == "角")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
}
if (strUpper.Substring(0, 1) == "分")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
}
if (strUpper.Substring(0, 1) == "整")
{
strUpper = "零圆整";
}
functionReturnValue = strUpper;
if (IsNegative == true)
{
return "负" + functionReturnValue;
}
else
{
return functionReturnValue;
}
}
}
}