482 lines
20 KiB
C#
482 lines
20 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.Odbc;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Windows.Forms;
|
|
using ZXing;
|
|
using ZXing.Common;
|
|
using ZXing.QrCode;
|
|
|
|
namespace InvoicingTool
|
|
{
|
|
public partial class ScanPay : Form
|
|
{
|
|
static QualityHelper _QualityHelper = new QualityHelper();
|
|
Timer _PayQuery = new Timer();
|
|
Timer _Close = new Timer();
|
|
Timer _OutTimer = new Timer();
|
|
EncodingOptions options = null;
|
|
BarcodeWriter writer = null;
|
|
DateTime? _OutTime = null;
|
|
//string _PayServiceUrl = "http://192.168.10.110:7080/EShangTechService/Service.asmx";
|
|
//string _PayServiceUrl = "http://" + _QualityHelper.dbip + ":" + _QualityHelper.serviceport + "/Service.asmx";
|
|
//string _PayServiceUrl = "http://192.168.11.190:7080/Service.asmx";
|
|
|
|
string _PayServiceUrl = "http://cloud.eshangtech.com:7080/Service.asmx";
|
|
string pay_code { get; set; }
|
|
string pay_url { get; set; }
|
|
string trade_no { get; set; }
|
|
string trade_num { get; set; }
|
|
string trade_score { get; set; }
|
|
string result_code { get; set; }
|
|
string result_desc { get; set; }
|
|
string ticket_code { get; set; }
|
|
string pay_type { get; set; }
|
|
string pay_price { get; set; }
|
|
string Result { get; set; }
|
|
bool pay_close { get; set; }
|
|
int _time = 10;
|
|
int _PayTime = 0;
|
|
/// <summary>
|
|
/// 采购扫码支付
|
|
/// </summary>
|
|
/// <param name="OrderCode">订单号</param>
|
|
/// <param name="OrderAmount">订单金额</param>
|
|
/// <param name="OrderBalance">积分抵扣</param>
|
|
/// <param name="OrderPrice">实际应付</param>
|
|
/// <param name="PayUrl">支付地址</param>
|
|
/// <param name="TradeNum">支付单号</param>
|
|
/// <param name="PayType">支付类型</param>
|
|
public ScanPay(string OrderCode, string OrderAmount, string OrderBalance, string OrderPrice)
|
|
{
|
|
InitializeComponent();
|
|
pay_code = OrderCode;
|
|
pay_price = OrderPrice;
|
|
_OutTime = OrderOutTime(OrderCode);
|
|
lblOrderCode.Text = pay_code;
|
|
lblAmount.Text = OrderAmount;
|
|
lblBalance.Text = OrderBalance;
|
|
lblPayment.Text = pay_price;
|
|
pay_close = false;
|
|
_PayQuery.Tick += new EventHandler(PayQuery_Tick);
|
|
_PayQuery.Interval = 1 * 1000;
|
|
_Close.Tick += new EventHandler(MainClose_Tick);
|
|
_Close.Interval = 1 * 1000;
|
|
_OutTimer.Tick += new EventHandler(_OutTimer_Tick);
|
|
_OutTimer.Interval = 1 * 500;
|
|
_OutTimer.Start();
|
|
lblResult.Text = "";
|
|
lblShop.Text = _QualityHelper.shopname;
|
|
//_PayQuery.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 采购扫码支付
|
|
/// </summary>
|
|
/// <param name="OrderCode">订单号</param>
|
|
/// <param name="OrderAmount">订单金额</param>
|
|
/// <param name="OrderBalance">积分抵扣</param>
|
|
/// <param name="OrderPrice">实际应付</param>
|
|
/// <param name="PayUrl">支付地址</param>
|
|
/// <param name="TradeNum">支付单号</param>
|
|
/// <param name="PayType">支付类型</param>
|
|
public ScanPay(string OrderCode, string OrderAmount, string OrderBalance,
|
|
string OrderPrice, string PayUrl, string TradeNum, string PayType)
|
|
{
|
|
InitializeComponent();
|
|
pay_code = OrderCode;
|
|
pay_price = OrderPrice;
|
|
pay_type = PayType;
|
|
trade_num = TradeNum;
|
|
lblOrderCode.Text = pay_code;
|
|
lblAmount.Text = OrderAmount;
|
|
lblBalance.Text = OrderBalance;
|
|
lblPayment.Text = pay_price;
|
|
pay_close = false;
|
|
_PayQuery.Tick += new EventHandler(PayQuery_Tick);
|
|
_PayQuery.Interval = 3 * 1000;
|
|
options = new QrCodeEncodingOptions
|
|
{
|
|
DisableECI = true,
|
|
CharacterSet = "UTF-8",
|
|
Width = picQRCode.Height,
|
|
Height = picQRCode.Width
|
|
};
|
|
writer = new BarcodeWriter();
|
|
writer.Format = BarcodeFormat.QR_CODE;
|
|
writer.Options = options;
|
|
Bitmap bitmap = writer.Write(PayUrl);
|
|
picQRCode.Image = bitmap;
|
|
if (pay_type.ToUpper() == "ALIPAY")
|
|
{
|
|
lblPayType.Text = "支付宝扫一扫支付货款";
|
|
}
|
|
else
|
|
{
|
|
lblPayType.Text = "微信扫一扫支付货款";
|
|
}
|
|
lblResult.Text = "等待支付中......";
|
|
_PayQuery.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 采购支付
|
|
/// </summary>
|
|
/// <param name="Orderjson">采购订单信息</param>
|
|
/// <param name="Payjson">支付订单信息</param>
|
|
/// <param name="PayType">支付类型</param>
|
|
public ScanPay(string Orderjson, string Payjson, string PayType)
|
|
{
|
|
InitializeComponent();
|
|
JObject _UploadJOject = JObject.Parse(Orderjson);
|
|
pay_code = _UploadJOject["ResultObject"]["FirstParameter"].ToString();
|
|
pay_price = _UploadJOject["ResultObject"]["FourthParameter"].ToString();
|
|
pay_type = PayType;
|
|
lblOrderCode.Text = pay_code;
|
|
lblAmount.Text = _UploadJOject["ResultObject"]["SecondParameter"].ToString();
|
|
lblBalance.Text = _UploadJOject["ResultObject"]["ThirdParameter"].ToString();
|
|
lblPayment.Text = pay_price;
|
|
JObject _JObject = JObject.Parse(Payjson);
|
|
pay_url = _JObject["result_desc"].ToString();
|
|
QRCodeAudit(Payjson);
|
|
_PayQuery.Tick += new EventHandler(PayQuery_Tick);
|
|
_PayQuery.Interval = 3 * 1000;
|
|
}
|
|
private DateTime? OrderOutTime(string OrderCode = "")
|
|
{
|
|
if (OrderCode != "")
|
|
{
|
|
string _SelectSql = "select reserve_date from t_purchase where purchase_desc = '" + OrderCode + "'";
|
|
try
|
|
{
|
|
return DateTime.Parse(DBHelper.QueryOdbc(_SelectSql).Tables[0].Rows[0]["reserve_date"].ToString()).AddMinutes(25);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
private void MainClose_Tick(object sender, EventArgs e)
|
|
{
|
|
_time--;
|
|
if (_time < 0)
|
|
{
|
|
_Close.Stop();
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
lblMessage.Text = "付款成功,窗口将在(" + _time + ")秒后自动关闭";
|
|
}
|
|
|
|
private void _OutTimer_Tick(object sender, EventArgs e)
|
|
{
|
|
if (_OutTime != null)
|
|
{
|
|
TimeSpan _TimeSpan = _OutTime.Value - DateTime.Now;
|
|
if (_TimeSpan.TotalSeconds < 0)
|
|
{
|
|
_OutTimer.Stop();
|
|
_PayQuery.Stop();
|
|
picQRCode.Image = Properties.Resources.支付失败;
|
|
btnALIPAY.Enabled = false;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝禁;
|
|
btnWECHRT.Enabled = false;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付禁;
|
|
lblResult.Text = "支付超时,订单已关闭";
|
|
lblOutTime.Text = "";
|
|
lblMessage.Text = "当前订单因支付超时已被系统自动关闭,请重新发起订单申请。";
|
|
pay_close = true;
|
|
}
|
|
else
|
|
{
|
|
lblOutTime.Text = "剩余支付时间:" + _TimeSpan.Minutes.ToString().PadLeft(2, '0') + ":" + _TimeSpan.Seconds.ToString().PadLeft(2, '0');
|
|
}
|
|
}
|
|
}
|
|
|
|
private void PayQuery_Tick(object sender, EventArgs e)
|
|
{
|
|
if (_OutTime != null)
|
|
{
|
|
TimeSpan _TimeSpan = _OutTime.Value - DateTime.Now;
|
|
if ((int)_TimeSpan.TotalSeconds < _PayTime)
|
|
{
|
|
_PayTime = (int)_TimeSpan.TotalSeconds;
|
|
}
|
|
}
|
|
if (_PayTime >= 0)
|
|
{
|
|
lblResult.Text = "等待支付中...(" + _PayTime + "秒)";
|
|
btnALIPAY.Enabled = false;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝禁;
|
|
btnWECHRT.Enabled = false;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付禁;
|
|
}
|
|
else
|
|
{
|
|
lblResult.Text = "等待支付中......";
|
|
btnALIPAY.Enabled = true;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝;
|
|
btnWECHRT.Enabled = true;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付;
|
|
}
|
|
if (_PayTime % 5 == 0)
|
|
{
|
|
string _Result = "";
|
|
string[] strargs = { pay_code, trade_num, pay_price, pay_type, "1" };
|
|
try
|
|
{
|
|
_Result = WSHelper.InvokeWebService(_PayServiceUrl, "querypay_new", strargs).ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_Result = "";
|
|
}
|
|
if (_Result != "")
|
|
{
|
|
Result = _Result;
|
|
// { "trade_no":"","trade_num":"OA139815067412867988","result_code":"101","result_desc":":(未支付)"}
|
|
JObject _JObject = JObject.Parse(_Result);
|
|
trade_no = _JObject["trade_no"].ToString();
|
|
trade_num = _JObject["trade_num"].ToString();
|
|
trade_score = _JObject["trade_score"].ToString();
|
|
result_code = _JObject["result_code"].ToString();
|
|
result_desc = _JObject["result_desc"].ToString();
|
|
if (result_code == "100")
|
|
{
|
|
lblResult.Text = "支付成功";
|
|
lblPoints.Text = trade_score;
|
|
picQRCode.Image = Properties.Resources.支付成功;
|
|
string strUpdateSql = "update t_purchase_paycheck set trade_no = '" + trade_no + "',trade_num = '" + trade_num +
|
|
"',result_code = '" + result_code + "',result_desc = '" + result_desc + "',json_string = '" + _Result +
|
|
"' where purchasepay_code = '" + pay_code + "'";
|
|
try
|
|
{
|
|
DBHelper.ExecuteSqlTran(strUpdateSql);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteServiceLog("校验移动支付结果:" + ex.Message);
|
|
}
|
|
_PayQuery.Stop();
|
|
_OutTimer.Stop();
|
|
lblOutTime.Text = "";
|
|
lblMessage.Text = "付款成功,窗口将在(10)秒后自动关闭";
|
|
_Close.Start();
|
|
}
|
|
}
|
|
}
|
|
_PayTime--;
|
|
}
|
|
|
|
private void QRCodeAudit(string payurl)
|
|
{
|
|
string _Result = payurl;
|
|
try
|
|
{
|
|
if (_Result != "")
|
|
{
|
|
JObject _JObject = JObject.Parse(_Result);
|
|
trade_no = _JObject["trade_no"].ToString();
|
|
trade_num = _JObject["trade_num"].ToString();
|
|
result_desc = _JObject["result_desc"].ToString();
|
|
options = new QrCodeEncodingOptions
|
|
{
|
|
DisableECI = true,
|
|
CharacterSet = "UTF-8",
|
|
Width = picQRCode.Height,
|
|
Height = picQRCode.Width
|
|
};
|
|
writer = new BarcodeWriter();
|
|
writer.Format = BarcodeFormat.QR_CODE;
|
|
writer.Options = options;
|
|
Bitmap bitmap = writer.Write(result_desc);
|
|
picQRCode.Image = bitmap;
|
|
if (pay_type.ToUpper() == "ALIPAY")
|
|
{
|
|
lblMessage.Text = "支付宝交易请求成功,请扫描二维码进行支付";
|
|
lblPayType.Text = "支付宝扫一扫支付货款";
|
|
}
|
|
else
|
|
{
|
|
lblMessage.Text = "微信支付交易请求成功,请扫描二维码进行支付";
|
|
lblPayType.Text = "微信扫一扫支付货款";
|
|
}
|
|
lblResult.Text = "等待支付中......";
|
|
_PayQuery.Start();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteServiceLog("校验移动支付结果:" + ex.Message);
|
|
_Result = "";
|
|
}
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
_PayQuery.Stop();
|
|
Close();
|
|
}
|
|
|
|
private void ScanAudit_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Escape)
|
|
{
|
|
_PayQuery.Stop();
|
|
Close();
|
|
}
|
|
}
|
|
|
|
private void ScanPay_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
if (result_code == "100")
|
|
{
|
|
lblResult.Text = "支付成功";
|
|
string strUpdateSql = "update t_purchase_paycheck set trade_no = '" + trade_no + "',trade_num = '" + trade_num +
|
|
"',result_code = '" + result_code + "',result_desc = '" + result_desc + "',json_string = '" + Result +
|
|
"' where purchasepay_code = '" + pay_code + "'";
|
|
try
|
|
{
|
|
DBHelper.ExecuteSqlTran(strUpdateSql);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteServiceLog("校验移动支付结果:" + ex.Message);
|
|
}
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
else
|
|
{
|
|
if (!pay_close)
|
|
{
|
|
if (MessageBox.Show("支付未完成,确认取消本次支付请求吗?\n 取消后本次付款二维码请勿付款!", "系统提示",
|
|
MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
|
|
{
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
}
|
|
_PayQuery.Stop();
|
|
}
|
|
private void btnALIPAY_Click(object sender, EventArgs e)
|
|
{
|
|
if (_PayTime <= 0)
|
|
{
|
|
lblMessage.Text = "正在发起支付宝交易请求,请稍候......";
|
|
_PayQuery.Stop();
|
|
_PayTime = 60;
|
|
btnALIPAY.Enabled = false;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝禁;
|
|
btnWECHRT.Enabled = false;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付禁;
|
|
pay_type = "ALIPAY";
|
|
Pay(pay_code, pay_price, "ALIPAY");
|
|
}
|
|
}
|
|
|
|
private void btnWECHRT_Click(object sender, EventArgs e)
|
|
{
|
|
if (_PayTime <= 0)
|
|
{
|
|
lblMessage.Text = "正在发起微信支付交易请求,请稍候......";
|
|
_PayQuery.Stop();
|
|
_PayTime = 60;
|
|
btnALIPAY.Enabled = false;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝禁;
|
|
btnWECHRT.Enabled = false;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付禁;
|
|
pay_type = "WECHAT";
|
|
Pay(pay_code, pay_price, "WECHAT");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 发起支付请求
|
|
/// </summary>
|
|
/// <param name="Ordercode">系统订单号</param>
|
|
/// <param name="Amount">支付金额</param>
|
|
/// <param name="PayType">支付类型</param>
|
|
private void Pay(string Ordercode, string Amount, string PayType)
|
|
{
|
|
string[] strargs = { Ordercode, Amount, PayType };
|
|
string _result_code = "";
|
|
try
|
|
{
|
|
string _Result = WSHelper.InvokeWebService(_PayServiceUrl, "createpay_customerscan", strargs).ToString();
|
|
if (_Result != "")
|
|
{
|
|
// { "trade_no":"","trade_num":"OA139815067412867988","result_code":"101","result_desc":":(未支付)"}
|
|
JObject _JObject = JObject.Parse(_Result);
|
|
trade_no = _JObject["trade_no"].ToString();
|
|
trade_num = _JObject["trade_num"].ToString();
|
|
_result_code = _JObject["result_code"].ToString();
|
|
result_desc = _JObject["result_desc"].ToString();
|
|
if (_result_code == "100")
|
|
{
|
|
string updatesql = "update t_purchase_paycheck set purchasepay_type = '" + PayType +
|
|
"',order_result = '" + _result_code + "',order_payurl = '" + result_desc +
|
|
"',trade_num = '" + trade_num + "' where purchasepay_code = '" + Ordercode + "'";
|
|
try
|
|
{
|
|
DBHelper.ExecuteSqlTran(updatesql);
|
|
}
|
|
catch (Exception ex)
|
|
{ }
|
|
QRCodeAudit(_Result);
|
|
//_PayQuery.Start();
|
|
}
|
|
else if (_result_code == "202")
|
|
{
|
|
lblMessage.Text = "交易失败,订单付款超时已关闭,请重新提交订单。";
|
|
_PayTime = 0;
|
|
btnALIPAY.Enabled = false;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝禁;
|
|
btnWECHRT.Enabled = false;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付禁;
|
|
pay_close = true;
|
|
}
|
|
else
|
|
{
|
|
lblMessage.Text = "交易失败,请重试";
|
|
_PayTime = 0;
|
|
btnALIPAY.Enabled = true;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝;
|
|
btnWECHRT.Enabled = true;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lblMessage.Text = "交易失败,请重试";
|
|
_PayTime = 0;
|
|
btnALIPAY.Enabled = true;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝;
|
|
btnWECHRT.Enabled = true;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteServiceLog(ex.Message);
|
|
lblMessage.Text = "交易失败,请重试";
|
|
_PayTime = 0;
|
|
btnALIPAY.Enabled = true;
|
|
btnALIPAY.BackgroundImage = Properties.Resources.支付宝;
|
|
btnWECHRT.Enabled = true;
|
|
btnWECHRT.BackgroundImage = Properties.Resources.微信支付;
|
|
}
|
|
}
|
|
}
|
|
}
|