本文主要是介绍POS机的接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
联迪的POS机
读取付款方式
private void Form_MisPos_Load(object sender, EventArgs e)
{
List<Trans> list = MisPos.GetTransList();
int col = 0;
int y = 15;
int colcount = 3;
Panel p = panel3;
int pw = p.Width;
foreach (Trans t in list)
{
Button btn = new System.Windows.Forms.Button();
int x = col * (pw / 3) + 10;
btn.Location = new System.Drawing.Point(x, y);
btn.Name = "btn_" + t.Code;
btn.Size = new System.Drawing.Size(pw / 3 - 20, 28);
btn.Text = t.Text;
btn.UseVisualStyleBackColor = true;
btn.Click += new System.EventHandler(this.btn_Click);
p.Controls.Add(btn);
col++;
if (col >= colcount)
{
col = 0;
y = y + 32;
}
}
}
交易
private void do_BankTrans(string trans)
{
InputData indata = new InputData();
indata.posid = "111";//[8]; /* 收银机号(8字节,左对齐,不足部分补空格)*/
indata.trans = trans;//[2]; /* 交易类型: '00'消费'01'撤销'02'退货'03'查余
indata.amount = InputData.Float2Amount(999.99F);//[12]; /* 金额(12字节,无小数点,左补0,单位:分)*/
indata.old_date = InputData.Date2Str(DateTime.Now);//[8]; /* 原交易日期(8 字节,yyyymmdd 格式,隔日退货时用)*/
indata.old_reference = "";//[12]; /* 原交易参考号*/
indata.old_trace = "";//[6]; /* 流水号(6 字节,右对齐,左补 0,退货或重打印等用)*/
indata.old_batch = "";//[6]; /*57~62 位 原批次号*/
indata.old_auth = "";//[6]; /*63~68 位 原授权码*/
indata.old_terno = "";//[8]; /*69~76 位 原交易终端号*/
indata.szFenqiNum = "";//[2]; /*77~78 位 分期数03,06,09,12,18,24,36,42,48,60*/
indata.szServiceNum = "";//[2];/*79~80 位 享受服务人数*/
indata.szGoodsNo = "";//[12]; /*81~92 位 商品项目编码*/
indata.trk2 = "";//[37]; /* 二磁道数据(37字节,左对齐,不足部分补空格)*/
indata.trk3 = "";//[104]; /* 三磁道数据(104字节, 左对齐,不足部分补空格)*/
indata.lrc = InputData.getlrc_Random();//[3]; /* 交易校验数据(3位从 0~9的随机字符)*/
indata.szOrderTrace = "";//[20]; // 6/16/2015 新增 收银流水(订单)号 左对齐 不足补空格
indata.szPrefer = "";//[50]; // 优惠券左对齐 不足补空格
indata.szRsv = "";//[300]; // 保留字段 在此输入二维码的条码数据 左对齐 不足补空格
OutPutData outdata = null;
int i = MisPos.do_BankTrans(indata, out outdata);
MessageBox.Show(outdata.resp_chin);
}
类代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace POSTEST
{public class Trans{public string Code = "";public string Text = "";}public class InputData{public string posid = "";//[8]; /* 收银机号(8字节,左对齐,不足部分补空格)*/ public string operid = "";//[8]; /* 操作员号(8字节,左对齐,不足部分补空格)*/ public string trans = "";//[2]; /* 交易类型: '00'消费'01'撤销'02'退货'03'查余 public string amount = "";//[12]; /* 金额(12字节,无小数点,左补0,单位:分)*/ public string old_date = "";//[8]; /* 原交易日期(8 字节,yyyymmdd 格式,隔日退货时用)*/ public string old_reference = "";//[12]; /* 原交易参考号*/ public string old_trace = "";//[6]; /* 流水号(6 字节,右对齐,左补 0,退货或重打印等用)*/ public string old_batch = "";//[6]; /*57~62 位 原批次号*/ public string old_auth = "";//[6]; /*63~68 位 原授权码*/ public string old_terno = "";//[8]; /*69~76 位 原交易终端号*/ public string szFenqiNum = "";//[2]; /*77~78 位 分期数03,06,09,12,18,24,36,42,48,60*/ public string szServiceNum = "";//[2];/*79~80 位 享受服务人数*/ public string szGoodsNo = "";//[12]; /*81~92 位 商品项目编码*/ public string trk2 = "";//[37]; /* 二磁道数据(37字节,左对齐,不足部分补空格)*/ public string trk3 = "";//[104]; /* 三磁道数据(104字节, 左对齐,不足部分补空格)*/ public string lrc = "";//[3]; /* 交易校验数据(3位从 0~9的随机字符)*/ public string szOrderTrace = "";//[20]; // 6/16/2015 新增 收银流水(订单)号 左对齐 不足补空格 public string szPrefer = "";//[50]; // 优惠券左对齐 不足补空格 public string szRsv = "";//[300]; // 保留字段 在此输入二维码的条码数据 左对齐 不足补空格public byte[] getdata(){List<byte> list = new List<byte>();addbyte(list, posid, 8, ' ');addbyte(list, operid, 8, ' ');addbyte(list, trans, 2, ' ');addbyte(list, amount, 12, ' ');addbyte(list, old_date, 8, ' ');addbyte(list, old_reference, 12, ' ');addbyte(list, old_trace, 6, ' ');addbyte(list, old_batch, 6, ' ');addbyte(list, old_auth, 6, ' ');addbyte(list, old_terno, 8, ' ');addbyte(list, szFenqiNum, 2, ' ');addbyte(list, szServiceNum, 2, ' ');addbyte(list, szGoodsNo, 12, ' ');addbyte(list, trk2, 37, ' ');addbyte(list, trk3, 104, ' ');addbyte(list, lrc, 3, ' ');addbyte(list, szOrderTrace, 20, ' ');addbyte(list, szPrefer, 50, ' ');addbyte(list, szRsv, 300, ' ');return list.ToArray();}public void addbyte(List<byte> list,string v,int len ,char c ){string s = v.PadRight(len, c);Encoding encoding = Encoding.GetEncoding("gb2312");byte[] bs =encoding.GetBytes(s);for (int i = 0; i < len; i++){list.Add(bs[i]);}}public static string getlrc_Random(){Random r = new Random(DateTime.Now.Millisecond);return r.Next(10).ToString() + r.Next(10).ToString() + r.Next(10).ToString();}public static string Float2Amount(float v){int intv = (int)(v * 100 + 0.001);string s = intv.ToString();s = s.PadLeft(12, '0');return s;}public static string Date2Str(DateTime dt){string s = dt.ToString("yyyyMMdd");return s;}}public class OutPutData{public string resp_code = "";//[2]; /*1~2 位 返回码 (2 字节,"00"交易成功,其他失败)*/ public string bank_code = "";//[4]; /*3~6 位 银行行号(4 字节)*/ public string card_no = "";//[30]; /*7~36 位 卡号:622280*********4860 */ public string expr = "";//[4]; /*37~40位 有效期 (4 字节) */ public string amount = "";//[12]; /*141~52位 金额(12字节,无小数点,左补 0,单位:分)*/ public string trace = "";//[6]; /*53~58位 流水号 (6字节,左对齐)*/ public string refer = "";//[12]; /*59~70位 交易参考号*/ public string auth = "";//[6]; /*71~76位 授权号*/ public string batch = "";//[6]; /*77~82位 批次号*/ public string date = "";//[8+6]; /*83~96位 交易日期(8字节,yyyymmddhhmmss格式)*/ public string userno = "";//[15]; /*97~111位 商户号*/ public string terno = "";//[8]; /*112~119位 终端号*/ public string old_terno = "";//[8]; /*120~127位 原终端号*/ public string resp_chin = "";//[50]; /* 错误说明(左对齐,不足部分补空格)*/ public string OrderNo = "";//[50]; /* 二维码交易订单订单号 */ public string trans = "";//[2]; /* 交易类型 与请求结构中交易类型的说明一样*/ public string ChannelType = "";//[2]; /* 00 银行卡01微信 02支付宝 03 银联二维码 */ public string lrc = "";//[3]; /* 交易数据校验码(3 字节)*/ public void readdata(byte[] data){int i = 0;resp_code = readstr(data, ref i, 2);bank_code = readstr(data, ref i, 4);card_no = readstr(data, ref i, 30);expr = readstr(data, ref i, 4);amount = readstr(data, ref i, 12);trace = readstr(data, ref i, 6);refer = readstr(data, ref i, 12);auth = readstr(data, ref i, 6);batch = readstr(data, ref i, 6);date = readstr(data, ref i, 8 + 6);userno = readstr(data, ref i, 15);terno = readstr(data, ref i, 8);old_terno = readstr(data, ref i, 8);resp_chin = readstr(data, ref i, 50);OrderNo = readstr(data, ref i, 50);trans = readstr(data, ref i, 2);ChannelType = readstr(data, ref i, 2);lrc = readstr(data, ref i, 3); }public string readstr(byte[] data,ref int pos,int len){Encoding encoding = Encoding.GetEncoding("gb2312");string s = encoding.GetString(data, pos, len);pos = pos + len;return s;}}public class MisPos{[DllImport("MisPos.dll", EntryPoint = "BankTrans", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int BankTrans(ref byte indata, ref byte outdata);public static int do_BankTrans(InputData indata, out OutPutData outdata){int result = 0;byte[] inbs = new byte[2048];for (var i = 0; i < inbs.Length; i++)inbs[i] = 0;byte[] outbs = new byte[2048];for (var i = 0; i < outbs.Length; i++)outbs[i] = 0;byte[] bs = indata.getdata();for (var i = 0; i < bs.Length; i++)inbs[i] = bs[i];result = BankTrans(ref inbs[0], ref outbs[0]);outdata = new OutPutData();outdata.readdata(outbs);return result;}public static List<Trans> GetTransList(){List<Trans> result = new List<Trans>();result.Add(new Trans() { Code = "00", Text = "消费" });result.Add(new Trans() { Code = "01", Text = "撤销" });result.Add(new Trans() { Code = "02", Text = "退货" });result.Add(new Trans() { Code = "03", Text = "查余" });result.Add(new Trans() { Code = "04", Text = "重打印" });result.Add(new Trans() { Code = "05", Text = "签到" });result.Add(new Trans() { Code = "06", Text = "结算" });result.Add(new Trans() { Code = "08", Text = "小费" });result.Add(new Trans() { Code = "09", Text = "预授权" });result.Add(new Trans() { Code = "10", Text = "预授权追加" });result.Add(new Trans() { Code = "11", Text = "预授权完成" });result.Add(new Trans() { Code = "12", Text = "预授权撤销" });result.Add(new Trans() { Code = "13", Text = "预授权完成撤销" });result.Add(new Trans() { Code = "14", Text = "商场分期" });result.Add(new Trans() { Code = "15", Text = "专用分期" });result.Add(new Trans() { Code = "16", Text = "分期撤销" });result.Add(new Trans() { Code = "18", Text = "分期额度查询" });result.Add(new Trans() { Code = "19", Text = "汽车卡洗车" });result.Add(new Trans() { Code = "20", Text = "快速支付" });result.Add(new Trans() { Code = "21", Text = "指定账户圈存" });result.Add(new Trans() { Code = "22", Text = "电子现金退货" });result.Add(new Trans() { Code = "23", Text = "电子现金查余" });result.Add(new Trans() { Code = "24", Text = "积分兑换消费" });result.Add(new Trans() { Code = "25", Text = "积分兑换撤销" });result.Add(new Trans() { Code = "26", Text = "积分查询" });result.Add(new Trans() { Code = "28", Text = "权益积分查询" });result.Add(new Trans() { Code = "29", Text = "权益积分消费" });result.Add(new Trans() { Code = "30", Text = "权益积分撤销" });result.Add(new Trans() { Code = "32", Text = "卡转出转账" });result.Add(new Trans() { Code = "33", Text = "财务报销" });result.Add(new Trans() { Code = "34", Text = "财务还款" });result.Add(new Trans() { Code = "60", Text = "聚合支付被扫 " });result.Add(new Trans() { Code = "61", Text = "聚合支付主扫" });result.Add(new Trans() { Code = "62", Text = "聚合支付退货 " });result.Add(new Trans() { Code = "72", Text = "聚合支付异常订单查询" });result.Add(new Trans() { Code = "90", Text = "惠兜圈优惠立减银行卡" });result.Add(new Trans() { Code = "91", Text = "惠兜圈优惠立减微信/支付宝被扫" });result.Add(new Trans() { Code = "92", Text = "惠兜圈优惠立减支付宝主扫" });result.Add(new Trans() { Code = "93", Text = "惠兜圈优惠立减微信主扫" });result.Add(new Trans() { Code = "94", Text = "惠兜圈立减优惠银联二维码" });result.Add(new Trans() { Code = "95", Text = "云闪付优惠立减交易" });result.Add(new Trans() { Code = "96", Text = "商场分期优惠立减" });return result;}}
}
这篇关于POS机的接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!