那些年我们赚过的外快(POS(移动支付)接口开发)
老规矩上前戏了。在我写博文"那些年我们赚过的外快"前后算起来大大小小也接了些私活,这次是因为好久没写博客了,趁热分享一下。最近回了离老家近的二线城市成都工作,收入那是下降很多啊,刚开始老婆还没说什么,随着开始还房贷和债务,生活开始捉襟见肘了。哎,最近都在发愁怎么增加收入!自己的想法是:1、争取多做几个安卓app出来发布到各大市场,靠植入广告赚点白菜钱。(还没验证过是否可行) 2、把之前积累好多年的行业管理软件的需求整理成几个软件,或基于云服务打造几款共享软件。(竞争很激烈啊,容易死在沙滩上和半途而废) 3、网上找个兼职 4、接些私活。昨晚看了xiaotie铁哥的博客<信念、思考、行动-谈谈程序员返回家乡的创业问题>很有些感觉,希望借此文重新和大家讨论下这个话题。
1、需求和接口文档





说明:这是一个之前私活的延伸出来的小单。就是做一个接口程序并和之前的业务系统(部分外包给本人)集成。
2、编码的过程和遇到的问题
编码时长:大概3小时。
编码工具:vs2010
遇到的问题:C#调用VC的动态库,外部引用DLL参数类型对应的问题。
函数原型:(接口:int Abmcs(char *request, char *response);)
第一版写法:
[DllImport("XGD_DLL.dll", CharSet = CharSet.Ansi)]
unsafe public static extern int Abmcs(string request, out StringBuilder response);
结果报内存不能写什么的,vc的出参 char *应该是个指针,一时不知道用什么类型去对应,以前看别人写StringBuilder 去接就可以了。
哎,还是基础不好啊,用String什么的都试了,还是不行,后来就想到用指针了,显然这是C#不推荐的做法,偶号称老鸟居然没在C#里用过指针,估计很多朋友都要看不下去了,
就这水平要接私活,还敢称老鸟!
第二版写法:
[DllImport("XGD_DLL.dll", CharSet = CharSet.Ansi)]
unsafe public static extern int Abmcs(string request, byte* response);
string requestTxt = request.GetRequestString();
byte[] buffer = new byte[144];
unsafe
{
fixed (byte* array = buffer)
{
Abmcs(requestTxt, array);
return new BankResponseEntity(Encoding.Default.GetString(buffer));
}
}
通过!对于要求“知其然就行了,可以不知其然”的ctrl+v大法深深崇拜的我很满足的笑了。
编码过程:
1)整理思路,根据文档整理出来类图(脑图,没画出来滴)。
2)动手写,然后遇到问题一番百度(最近Google不能访问啊),终于趟完一个坑,搞定。
成果物:


下面上点代码吧:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MisposInterfaceLib { /// <summary> /// 交易请求实体 /// </summary> public class PosRequestEntity { protected string _TransactionTypeFlag; public PosRequestEntity(string transactionType) { _TransactionTypeFlag = transactionType; } public string Value1 { get; set; } public string Value2 { get; set; } public string Value3 { get; set; } public string Value4 { get; set; } public string Value5 { get; set; } /// <summary> /// 获取请求文本 /// </summary> /// <returns></returns> public string GetRequestString() { return string.Join("|", new string[] { _TransactionTypeFlag, Value1, Value2, Value3, Value4, Value5, "" }); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MisposInterfaceLib { /// <summary> /// POS机回复实体类 /// </summary> public class BankResponseEntity { protected string _ResultResponse; public BankResponseEntity(string resultResponse) { _ResultResponse = resultResponse; SplitResponse(); } protected void SplitResponse() { string[] tempArray = _ResultResponse.Split("|".ToCharArray()); if (tempArray.Length > 10) { ResultCode = tempArray[0]; 授权码 = tempArray[1]; 卡号 = tempArray[2]; 金额 = tempArray[3]; 系统参考号 = tempArray[4]; 有效日期 = tempArray[5]; 交易日期 = tempArray[6]; 交易时间 = tempArray[7]; MessageContext = tempArray[8]; 商户编号 = tempArray[9]; 终端号 = tempArray[10]; } } /// <summary> /// 回复代码 00表示成功 /// </summary> public string ResultCode { get; set; } public string 授权码 { get; set; } public string 卡号 { get; set; } public string 金额 { get; set; } public string 系统参考号 { get; set; } public string 有效日期 { get; set; } public string 交易日期 { get; set; } public string 交易时间 { get; set; } public string MessageContext { get; set; } public string 商户编号 { get; set; } public string 终端号 { get; set; } /// <summary> /// 交易请求是否成功 /// </summary> public bool TransactionResultValue { get { return ResultCode.Equals("00"); } } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MisposInterfaceLib
{
/// <summary>
/// POS交易业务类
/// </summary>
public class MisPosTransaction : IMisposTransaction
{
[DllImport("XGD_DLL.dll", CharSet = CharSet.Ansi)]
unsafe public static extern int Abmcs(string request, byte* response);
/// <summary>
/// 像POS机发起一个交易请求
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public BankResponseEntity SendTransactionRequest(PosRequestEntity request)
{
string requestTxt = request.GetRequestString();
byte[] buffer = new byte[144];
unsafe
{
fixed (byte* array = buffer)
{
Abmcs(requestTxt, array);
return new BankResponseEntity(Encoding.Default.GetString(buffer));
}
}
}
}
}
调用接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MisposInterfaceLib;
namespace miposinterface
{
class Program
{
static void Main(string[] args)
{
IMisposTransaction ConsumeTransaction = new MisPosTransaction();
PosRequestEntity signInRequest = new PosRequestEntity(TransactionType.ConsumeFlag)
{
Value1 = "3098234.98",
Value2 = "111111",
Value3 = "222222",
Value4 = "123456",
Value5 = "333333"
};
var result = ConsumeTransaction.SendTransactionRequest(signInRequest);
Console.WriteLine(result.MessageContext);
}
}
}

好了,到处结束,文章还是太缺营养了(终于有点自知者明了)。但是不知道为什么这么晚还没睡意,希望今天的辛勤工作能迎来人生的安慰奖吧。
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/3801762.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/

浙公网安备 33010602011771号