那些年咱們賺過的外快(POS(移動支付)接口開發)

老規矩上前戲了。在我寫博文"那些年咱們賺過的外快"先後算起來大大小小也接了些私活,此次是由於很久沒寫博客了,趁熱分享一下。最近回了離老家近的二線城市成都工做,收入那是降低不少啊,剛開始老婆還沒說什麼,隨着開始還房貸和債務,生活開始捉襟見肘了。哎,最近都在發愁怎麼增長收入!本身的想法是:一、爭取多作幾個安卓app出來發佈到各大市場,靠植入廣告賺點白菜錢。(還沒驗證過是否可行) 二、把以前積累好多年的行業管理軟件的需求整理成幾個軟件,或基於雲服務打造幾款共享軟件。(競爭很激烈啊,容易死在沙灘上和半途而廢) 三、網上找個兼職 四、接些私活。昨晚看了xiaotie鐵哥的博客<信念、思考、行動-談談程序員返回家鄉的創業問題>頗有些感受,但願藉此文從新和你們討論下這個話題。html

一、需求和接口文檔

 

 

說明:這是一個以前私活的延伸出來的小單。就是作一個接口程序並和以前的業務系統(部分外包給本人)集成。程序員

     

二、編碼的過程和遇到的問題

編碼時長:大概3小時。app

編碼工具:vs2010ide

遇到的問題:C#調用VC的動態庫,外部引用DLL參數類型對應的問題。函數

函數原型:(接口:int  Abmcs(char *request, char *response);)工具

初版寫法:post

 

[DllImport("XGD_DLL.dll", CharSet = CharSet.Ansi)]
unsafe public static extern int Abmcs(string request, out StringBuilder response);ui

結果報內存不能寫什麼的,vc的出參 char *應該是個指針,一時不知道用什麼類型去對應,之前看別人寫StringBuilder 去接就能夠了。編碼

哎,仍是基礎很差啊,用String什麼的都試了,仍是不行,後來就想到用指針了,顯然這是C#不推薦的作法,偶號稱老鳥竟然沒在C#裏用過指針,估計不少朋友都要看不下去了,url

就這水平要接私活,還敢稱老鳥!

 

第二版寫法:

[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);
        }
    }
}

  

  

好了,處處結束,文章仍是太缺養分了(終於有點自知者明瞭)。可是不知道爲何這麼晚還沒睡意,但願今天的辛勤工做能迎來人生的安慰獎吧。

相關文章
相關標籤/搜索