那些年我們賺過的外快(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");
            }
        }
    }
}
POS機返回消息實體定義
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);
        }
    }
}

  

  

好了,到處結束,文章還是太缺營養了(終於有點自知者明了)。但是不知道為什么這么晚還沒睡意,希望今天的辛勤工作能迎來人生的安慰獎吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM