|
@@ -30,6 +30,9 @@ using Newtonsoft.Json;
|
|
|
using HengshanPaymentTerminal.Http.Response;
|
|
|
using HengshanPaymentTerminal.MessageEntity.Outgoing;
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
+using Org.BouncyCastle.Asn1.Ocsp;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
+using System.Net;
|
|
|
|
|
|
namespace HengshanPaymentTerminal
|
|
|
{
|
|
@@ -708,6 +711,48 @@ namespace HengshanPaymentTerminal
|
|
|
nozzleInfoList = MysqlDbContext.NozzleInfos.ToList().Select(n => new DetailsNozzleInfoOutput(n)).ToList();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 接收到MQTT
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="message"></param>
|
|
|
+ public async void OnReceiveMqttMessage(string message)
|
|
|
+ {
|
|
|
+ MqttRequest? mqttRequest = JsonConvert.DeserializeObject<MqttRequest>(message);
|
|
|
+ if (mqttRequest == null)
|
|
|
+ {
|
|
|
+ logger.Error($"mqtt message turn on object fail,message:{message}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ switch (mqttRequest.type)
|
|
|
+ {
|
|
|
+ case MQTT_TYPE.AUTHORIZATION:
|
|
|
+ {
|
|
|
+
|
|
|
+ MqttAuthorizationRequest? mqttAuthorizationRequest = JsonConvert.DeserializeObject<MqttAuthorizationRequest>(mqttRequest.data);
|
|
|
+ await SendAuthorizationAsync(mqttAuthorizationRequest);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case MQTT_TYPE.UNAUTHORIZATION:
|
|
|
+ {
|
|
|
+ MqttUnAhorizationRequest? mqttUnAhorizationRequest = JsonConvert.DeserializeObject<MqttUnAhorizationRequest>(mqttRequest.data);
|
|
|
+ await SendUnAuthorizartion(mqttUnAhorizationRequest);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case MQTT_TYPE.PAID:
|
|
|
+ {
|
|
|
+ MqttPaidRequest? mqttPaidRequest = JsonConvert.DeserializeObject<MqttPaidRequest>(mqttRequest.data);
|
|
|
+ await SendActuallyPaid(mqttPaidRequest);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case MQTT_TYPE.REFUND:
|
|
|
+ {
|
|
|
+ MqttRefundRequest? mqttRefundRequest = JsonConvert.DeserializeObject<MqttRefundRequest>(mqttRequest.data);
|
|
|
+ await OnRecieveOrderRefund(mqttRefundRequest);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 发送二维码信息给油机
|
|
|
/// </summary>
|
|
@@ -773,53 +818,68 @@ namespace HengshanPaymentTerminal
|
|
|
/// 发送实付金额给油机
|
|
|
/// </summary>
|
|
|
/// <param name="orderInfo"></param>
|
|
|
- public async void SendActuallyPaid(FccOrderInfo orderInfo)
|
|
|
+ public async Task SendActuallyPaid(MqttPaidRequest? request)
|
|
|
{
|
|
|
- //List<Byte> list = new List<Byte>();
|
|
|
- //byte[] commandAndNozzle = { 0x19, (byte)orderInfo.NozzleNum };
|
|
|
- //byte[] ttcBytes = NumberToByteArrayWithPadding(orderInfo.Ttc, 4);
|
|
|
-
|
|
|
- //byte[] amountPayableBytes = FormatDecimal(orderInfo.AmountPayable ?? orderInfo.Amount);
|
|
|
- //list.AddRange(commandAndNozzle); //添加命令字和枪号
|
|
|
- //list.AddRange(ttcBytes); //添加流水号
|
|
|
- //list.Add(0x21); //由fcc推送实付金额表示该订单是二维码小程序支付的
|
|
|
- //list.AddRange(amountPayableBytes); //添加实付金额
|
|
|
- ////添加3位交易金额1,3位交易金额2,2位优惠规则代码,10位卡应用号,4位消息鉴别码
|
|
|
- //list.AddRange(new byte[] { 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00 });
|
|
|
- //byte[] sendBytes = content2data(list.ToArray(), null);
|
|
|
-
|
|
|
-
|
|
|
- SendActuallyPaid sendActuallyPaid = new SendActuallyPaid(orderInfo.NozzleNum, orderInfo.Ttc, orderInfo.AmountPayable ?? orderInfo.Amount, getFrame(null));
|
|
|
- byte[] commandAndNozzle = { sendActuallyPaid.Handle, (byte)sendActuallyPaid.NozzleNum };
|
|
|
- await SendMessageToMaichine("发送实付金额", (request, response) =>
|
|
|
+ if (request == null)
|
|
|
{
|
|
|
- if (response.Handle == (byte)CommonMessage.Command.SEND_NEED_AMOUNT)
|
|
|
- {
|
|
|
- CommonAnswerBack commonAnswerBack = (CommonAnswerBack)response;
|
|
|
- return commonAnswerBack.Command == (byte)CommonMessage.Command.SEND_NEED_AMOUNT && commonAnswerBack.NozzleNum == sendActuallyPaid.NozzleNum;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }, sendActuallyPaid);
|
|
|
+ logger.Error($"mqtt get paid request is null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //通知云端当前已收到消息
|
|
|
+ OnGetPaidInfo onGetPaidInfo = new OnGetPaidInfo()
|
|
|
+ {
|
|
|
+ Id = request.Id,
|
|
|
+ Result = 1
|
|
|
+ };
|
|
|
+ await httpClientUtil.SendRecievePaidNotice(JsonConvert.SerializeObject(onGetPaidInfo));
|
|
|
+
|
|
|
+ FccOrderInfo? fccOrderInfo = MysqlDbContext.FccOrderInfos.FirstOrDefault(order => order.CloundOrderId == request.Id);
|
|
|
+ if (fccOrderInfo == null)
|
|
|
+ {
|
|
|
+ logger.Error($"[mqtt paid order notice]:can not find order by clounid:{request.Id}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fccOrderInfo.AmountPayable = request.ActualPaymentAmount;
|
|
|
+ fccOrderInfo.PaymentStatus = 1;
|
|
|
+ MysqlDbContext.SaveChanges();
|
|
|
+
|
|
|
+ //SendActuallyPaid sendActuallyPaid = new SendActuallyPaid(orderInfo.NozzleNum, orderInfo.Ttc, orderInfo.AmountPayable ?? orderInfo.Amount, getFrame(null));
|
|
|
+ //byte[] commandAndNozzle = { sendActuallyPaid.Handle, (byte)sendActuallyPaid.NozzleNum };
|
|
|
+ //await SendMessageToMaichine("发送实付金额", (request, response) =>
|
|
|
+ //{
|
|
|
+ // if (response.Handle == (byte)CommonMessage.Command.SEND_NEED_AMOUNT)
|
|
|
+ // {
|
|
|
+ // CommonAnswerBack commonAnswerBack = (CommonAnswerBack)response;
|
|
|
+ // return commonAnswerBack.Command == (byte)CommonMessage.Command.SEND_NEED_AMOUNT && commonAnswerBack.NozzleNum == sendActuallyPaid.NozzleNum;
|
|
|
+ // }
|
|
|
+ // return false;
|
|
|
+ //}, sendActuallyPaid);
|
|
|
//await SendMessageToMaichine("发送实付金额", BitConverter.ToString(commandAndNozzle).Replace("-", ""), sendActuallyPaid);
|
|
|
}
|
|
|
|
|
|
- public async Task<CommonMessage> SendAuthorization(MqttAuthorizationRequest request)
|
|
|
+ /// <summary>
|
|
|
+ /// 发送授权请求给油机
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task SendAuthorizationAsync(MqttAuthorizationRequest? request)
|
|
|
{
|
|
|
- //List<Byte> list = new List<Byte>();
|
|
|
- //byte[] commandAndNozzle = { 0x65, (byte)request.NozzleNum };
|
|
|
- //byte[] authorizationTimeBytes = ConvertDateTimeToByteArray(request.AuthorizationTime);
|
|
|
- ////将小数点后移两位,因为油机只支持两位小数点,这边传过去的3位字节转为int后取后两位为十分位和百分位
|
|
|
- //int value = (int)request.Value * 100;
|
|
|
- //byte[] valueBytes = NumberToByteArrayWithPadding(value, 3);
|
|
|
- //list.AddRange(commandAndNozzle);
|
|
|
- //list.AddRange(authorizationTimeBytes);
|
|
|
- //list.Add((byte)request.AuthorizationType);
|
|
|
- //list.AddRange(valueBytes);
|
|
|
- //byte[] sendBytes = content2data(list.ToArray(), null);
|
|
|
-
|
|
|
- SendAuthorization sendAuthorization = new SendAuthorization(request.NozzleNum, request.AuthorizationTime, request.AuthorizationType,request.Value, getFrame(null));
|
|
|
+ if(request == null)
|
|
|
+ {
|
|
|
+ logger.Error($"mqtt authorization request is null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加订单到数据库
|
|
|
+ DateTime authorizationTime = request.AuthorizationTime ?? DateTime.Now;
|
|
|
+ FccOrderInfo fccOrderInfo = request.ToComponent(authorizationTime);
|
|
|
+ MysqlDbContext.FccOrderInfos.Add(fccOrderInfo);
|
|
|
+
|
|
|
+ //发送授权申请到油机
|
|
|
+ SendAuthorization sendAuthorization = new SendAuthorization((int)request.NozzleId, authorizationTime, 1,request.OriginalAmount, getFrame(null));
|
|
|
byte[] commandAndNozzle = { sendAuthorization.Handle, (byte)sendAuthorization.NozzleNum };
|
|
|
- return await SendMessageToMaichine("发送授权请求", (request, response) =>
|
|
|
+ CommonMessage commonMessage = await SendMessageToMaichine("发送授权请求", (request, response) =>
|
|
|
{
|
|
|
if (response.Handle == (byte)CommonMessage.Command.ACCREDIT)
|
|
|
{
|
|
@@ -828,34 +888,155 @@ namespace HengshanPaymentTerminal
|
|
|
}
|
|
|
return false;
|
|
|
}, sendAuthorization);
|
|
|
- //return await SendMessageToMaichine("发送授权请求", BitConverter.ToString(commandAndNozzle).Replace("-", ""), sendAuthorization);
|
|
|
+
|
|
|
+ //发送授权结果给云端
|
|
|
+ string authorizationResultJson = string.Empty;
|
|
|
+ SendAuthorizationResult sendAuthorizationResult = new SendAuthorizationResult();
|
|
|
+ sendAuthorizationResult.NozzleId = request.NozzleId;
|
|
|
+
|
|
|
+ if (commonMessage.IsError)
|
|
|
+ {
|
|
|
+ ErrorMessage errorMessage = (ErrorMessage)commonMessage;
|
|
|
+ switch (errorMessage.TheErrorType)
|
|
|
+ {
|
|
|
+ case CommonMessage.ErrorType.DISCONNECT:
|
|
|
+ sendAuthorizationResult.OilMachineStatus = OilMachineStatus.Disconnected;
|
|
|
+ break;
|
|
|
+ case CommonMessage.ErrorType.TIMEOUT:
|
|
|
+ sendAuthorizationResult.OilMachineStatus = OilMachineStatus.AuthorizationTimeout;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ AuthorizationResponse authorization = (AuthorizationResponse)commonMessage;
|
|
|
+ if (authorization.Result == 0)
|
|
|
+ {
|
|
|
+ sendAuthorizationResult.OilMachineStatus = OilMachineStatus.Failed;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sendAuthorizationResult.OilMachineStatus = OilMachineStatus.Success;
|
|
|
+ sendAuthorizationResult.TransactionNumber = authorization.Ttc.ToString();
|
|
|
+ fccOrderInfo.Ttc = authorization.Result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpResponseMessage httpResponseMessage = await httpClientUtil.SendAuthorizationResult(JsonConvert.SerializeObject(sendAuthorizationResult));
|
|
|
+ logger.Info($"send authorization result response:{JsonConvert.SerializeObject(httpResponseMessage.Content)}");
|
|
|
+
|
|
|
+ //更新订单
|
|
|
+ MysqlDbContext.SaveChanges();
|
|
|
}
|
|
|
|
|
|
- public async Task<CommonMessage> SendUnAuthorizartion(MqttUnAhorizationRequest request)
|
|
|
+ /// <summary>
|
|
|
+ /// 发送取消授权请求给油机
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ public async Task SendUnAuthorizartion(MqttUnAhorizationRequest? request)
|
|
|
{
|
|
|
- //List<Byte> list = new List<Byte>();
|
|
|
- //byte[] commandAndNozzle = { 0x66, (byte)request.NozzleNum };
|
|
|
- //byte[] authorizationTimeBytes = ConvertDateTimeToByteArray(request.AuthorizationTime);
|
|
|
+ if (request == null)
|
|
|
+ {
|
|
|
+ logger.Error($"mqtt unauthorization request is null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- //byte[] ttcBytes = NumberToByteArrayWithPadding(request.Ttc, 4);
|
|
|
- //list.AddRange(commandAndNozzle);
|
|
|
- //list.AddRange(authorizationTimeBytes);
|
|
|
- //list.AddRange(ttcBytes);
|
|
|
- //byte[] sendBytes = content2data(list.ToArray(), null);
|
|
|
+ //从请求信息中获取流水号与授权时间,没有就到数据库查找
|
|
|
+ int ttc = 0;
|
|
|
+ DateTime authorizationTime = request.AuthorizationTime ?? DateTime.Now;
|
|
|
+ bool ttsIntResult = int.TryParse(request.TransactionNumber, out ttc);
|
|
|
+ if (request.AuthorizationTime == null || !ttsIntResult)
|
|
|
+ {
|
|
|
+ FccOrderInfo? fccOrderInfo = MysqlDbContext.FccOrderInfos.FirstOrDefault(order => order.CloundOrderId == request.Id);
|
|
|
+ if(fccOrderInfo != null)
|
|
|
+ {
|
|
|
+ ttc = fccOrderInfo.Ttc;
|
|
|
+ authorizationTime = fccOrderInfo.AuthorizationTime;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- SendUnAuthorization sendUnAuthorization = new SendUnAuthorization(request.NozzleNum, request.AuthorizationTime, request.Ttc, getFrame(null));
|
|
|
- byte[] commandAndNozzle = { sendUnAuthorization.Handle, (byte)sendUnAuthorization.NozzleNum };
|
|
|
+ SendUnAuthorizationResult sendUnAuthorizationResult = new SendUnAuthorizationResult();
|
|
|
+ sendUnAuthorizationResult.NozzleId = request.NozzleId;
|
|
|
+ sendUnAuthorizationResult.OilMachineStatus = OilMachineStatus.Success;
|
|
|
|
|
|
- return await SendMessageToMaichine("发送取消授权请求", (request, response) =>
|
|
|
+ if (ttc != 0)
|
|
|
{
|
|
|
- if (response.Handle == (byte)CommonMessage.Command.CANCEL_ACCREDIT)
|
|
|
+ SendUnAuthorization sendUnAuthorization = new SendUnAuthorization((int)request.NozzleId, authorizationTime, ttc, getFrame(null));
|
|
|
+ byte[] commandAndNozzle = { sendUnAuthorization.Handle, (byte)sendUnAuthorization.NozzleNum };
|
|
|
+
|
|
|
+ CommonMessage commonMessage = await SendMessageToMaichine("发送取消授权请求", (request, response) =>
|
|
|
+ {
|
|
|
+ if (response.Handle == (byte)CommonMessage.Command.CANCEL_ACCREDIT)
|
|
|
+ {
|
|
|
+ UnAhorizationResponse unauthorization = (UnAhorizationResponse)response;
|
|
|
+ return unauthorization.NozzleNum == sendUnAuthorization.NozzleNum;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }, sendUnAuthorization);
|
|
|
+ if (commonMessage.IsError)
|
|
|
{
|
|
|
- UnAhorizationResponse unauthorization = (UnAhorizationResponse)response;
|
|
|
- return unauthorization.NozzleNum == sendUnAuthorization.NozzleNum;
|
|
|
+ ErrorMessage errorMessage = (ErrorMessage)commonMessage;
|
|
|
+ switch (errorMessage.TheErrorType)
|
|
|
+ {
|
|
|
+ case CommonMessage.ErrorType.DISCONNECT:
|
|
|
+ sendUnAuthorizationResult.OilMachineStatus = OilMachineStatus.Disconnected;
|
|
|
+ break;
|
|
|
+ case CommonMessage.ErrorType.TIMEOUT:
|
|
|
+ sendUnAuthorizationResult.OilMachineStatus = OilMachineStatus.AuthorizationTimeout;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- return false;
|
|
|
- }, sendUnAuthorization);
|
|
|
- //return await SendMessageToMaichine("发送取消授权请求", BitConverter.ToString(commandAndNozzle).Replace("-", ""), sendUnAuthorization);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UnAhorizationResponse unAuthorization = (UnAhorizationResponse)commonMessage;
|
|
|
+ if (unAuthorization.Result == 0)
|
|
|
+ {
|
|
|
+ sendUnAuthorizationResult.OilMachineStatus = OilMachineStatus.Failed;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sendUnAuthorizationResult.OilMachineStatus = OilMachineStatus.Success;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sendUnAuthorizationResult.OilMachineStatus = OilMachineStatus.TransactionNumberNotFound;
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpResponseMessage httpResponseMessage = await httpClientUtil.SendUnAuthorizationResult(JsonConvert.SerializeObject(sendUnAuthorizationResult));
|
|
|
+ logger.Info($"send Unauthorization result response:{JsonConvert.SerializeObject(httpResponseMessage.Content)}");
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 接收到云端发送订单退款信息
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task OnRecieveOrderRefund(MqttRefundRequest? request)
|
|
|
+ {
|
|
|
+ if (request == null)
|
|
|
+ {
|
|
|
+ logger.Error($"mqtt OnRecieveOrderRefund request is null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //通知云端当前已收到消息
|
|
|
+ OnGetRefundInfo onGetRefundInfo = new OnGetRefundInfo()
|
|
|
+ {
|
|
|
+ Id = request.Id,
|
|
|
+ Result = 1
|
|
|
+ };
|
|
|
+ await httpClientUtil.SendRecieveRefundNotice(JsonConvert.SerializeObject(onGetRefundInfo));
|
|
|
+
|
|
|
+ FccOrderInfo? fccOrderInfo = MysqlDbContext.FccOrderInfos.FirstOrDefault(order => order.CloundOrderId == request.Id);
|
|
|
+ if (fccOrderInfo == null)
|
|
|
+ {
|
|
|
+ logger.Error($"[mqtt refund order notice]:can not find order by clounid:{request.Id}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fccOrderInfo.AmountPayable = request.ActualPaymentAmount;
|
|
|
+ fccOrderInfo.PaymentStatus = 2;
|
|
|
+ MysqlDbContext.SaveChanges();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//public void SetTcpClient(TcpClient? tcpClient, int? serverPort)
|