其他分享
首页 > 其他分享> > Delphi_调用DLL方法[创建对象下面的方法]

Delphi_调用DLL方法[创建对象下面的方法]

作者:互联网

unit PayIntf_CallDLL;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TUpData = class
    PosNumber: string;	        //收银机号
    StoreNumber: string;	//门店号
    operator: string;     	//操作员号
    CardNumber: string;	        //卡号/付款码/优惠券码
    CardType: string;	        //卡种代码(银行卡01,万商通联02……)
    TransType: string;	        //交易类型
    Amount: string;         	//金额(12位,分为单位,不足左补0)
    OldTraceNumber: string;	//原始流水号(6位,不足左补0)
    AuthNumber: string;	        //授权码(6位,不足左补0)
    HostserialNumber: string;	//原系统参考号(12位,不足左补0)
    TransDate: string;	        //原交易日期(4位,MMDD)
    ExpireDate: string;	        //有效期(4位,MMDD)
    Memo: string;	        //其他信息
    TransCheck: string;	        //交易唯一标识
    BusinessId: string;	        //业务编码,用于多业务模式
    TransId: string;    	//原交易单号
    ProductNumber: string;	//产品编码    万商卡附加KEY
    preDef_0: string;   	//自定义域1   自定义附加KEY
    preDef_1: string;   	//自定义域2
    preDef_9: string;	        //自定义域9
    preDef_A: string;	        //自定义域10
    preDef_B: string;	        //自定义域11
    preDef_J: string;	        //自定义域20
  end;

  TDownData = class
    Tips: string;       	//小费
    Total: string;	        //总计
    Amount: string;     	//金额
    BalanceAmount: string;	//余额
    PosTraceNumber: string;	//流水号
    OldTraceNumber: string;	//原始流水号
    ExpireDate: string;	        //有效期
    BatchNumber: string;	//批次号
    MerchantNumber: string;	//商户号
    MerchantName: string;	//商户名
    TerminalNumber: string;	//终端号
    HostSerialNumber: string;	//系统参考号
    AuthNumber: string;	        //授权码
    RejCode: string;	        //返回码
    IssNumber: string;	        //发卡行号
    IssName: string;	        //发卡行名称
    CardNumber: string;	        //卡号
    TransDate: string;	        //交易日期
    TransTime: string;	        //交易时间
    RejCodeExplain: string;	//返回码解释
    CardBack: string;	        //卡片回收标志
    Memo: string;	        //备注
    TransCheck: string;	        //交易唯一标识
    IsOffline: string;	        //脱机标识:0-联机、1-脱机
    CUPS: string;       	//卡组织
    TransId: string;	        //原交易单号
    ProductNumber: string;	//产品号码
    ProductName: string;	//产品名称
    preDef_0: string;	        //自定义域1   自定义附加KEY
    preDef_1: string;	        //自定义域2
    preDef_9: string;	        //自定义域9
    preDef_A: string;	        //自定义域10
    preDef_B: string;	        //自定义域11
    preDef_J: string;	        //自定义域20
  end;

const
  CARD_YHK = '01'; //银行卡
  CARD_SCAN = '02'; //扫码: 微信/支付宝
  FILENAME = 'C:\ALLINPAY\ALLINPAY\AllInPay.dll';

procedure MyWriteLog(const mStr: string);

function Obj2Json(nType:integer; UpData:TUpData; Response: Variant): string;

function FormatData(var UpData: TUpData; var DownData: TDownData): boolean;

function ExchangeMoney(Value: string; Ext:integer): string;

function SetRigthZero(sVaule: string; Len:integer): string;

function CheckValid(UpData: TUpData; DownData: TDownData): boolean;

function Param_Input(UpData: TUpData; var Request: Variant): boolean;

function Param_OutPut(var DownData: TDownData; var Response: Variant): boolean;

function PayIntf_MisByTL(UpData: TUpData; var DownData: TDownData; var sUp,sDown:string): boolean;

function Call_InputMis(CardType,TransType,Amount,PosNumber,Operator,Memo:string; var sUp,sDwon:string): boolean;

function TransProcess(request: Variant; response: Variant):LongInt ;  stdcall; external 'C:\ALLINPAY\ALLINPAY\AllInPay.dll';

implementation

uses
  ComObj,SuperObject;

function Call_InputMis(CardType,TransType,Amount,PosNumber,Operator,Memo:string; var sUp,sDwon:string): boolean;
var
  sMsg: string;
  UpData: TUpData;
  DownData: TDownData;
  jo: ISuperObject;
begin
  {**
    *   M — 必须有数据	传入
    *   C — 如果有数据则传入(推荐)
    *   TransType: 1签到  2消费  3消费撤销  4隔日退货
    *-------------------------------------------------------------------
    *	                签到/查询余额	消费	撤 销	退 货
    *  CardType         	M       M	M	M
    *  TransType        	M	M	M	M
    *  Amount	        	M	M	M
    *  OldTraceNumber	        		M
    *  HostSerialNumber	        			M
    *  TransDate	        			M
    *  AuthNumber
    *  CardNumber       			C	C
    *  ExpireDate       			C	C
    *  Memo	        	C	C	C
    *  StoreNumber      		C	C	C
    *  PosNumber        		C	C	C
    *  Operator	        	C	C	C
    *  TransCheck       	C	C	C	C
    *--------------------------------------------------------------------}
  Result := False;
  UpData := TUpData.Create;
  DownData := TDownData.Create;
  try
    try
      UpData.CardType := Trim(CardType);
      UpData.TransType := Trim(TransType);
      UpData.Amount := Trim(Amount);
      UpData.PosNumber := PosNumber;
      UpData.Operator := Operator;
      UpData.Memo := Memo;
      if (UpData.TransType = '3') or (UpData.TransType = '4') then
      begin
        jo := SO(sDwon);   //<---读取Json进行解析
        if UpData.TransType = '3' then    //TransType: 1签到  2消费  3消费撤销  4隔日退货
        begin
          if UpData.CardType = '01' then
          begin
            if jo['OldTraceNumber'] <> nil then
              UpData.OldTraceNumber := jo['OldTraceNumber'].AsString;
          end;
          if UpData.CardType = '100' then
          begin
            if jo['TransId'] <> nil then
              UpData.TransId := jo['TransId'].AsString;
          end;
        end;
        
        if UpData.TransType = '4' then    //TransType: 1签到  2消费  3消费撤销  4隔日退货
        begin
          if jo['HostserialNumber'] <> nil then
            UpData.HostserialNumber := jo['HostserialNumber'].AsString;
          if jo['TransDate'] <> nil then
            UpData.TransDate := jo['TransDate'].AsString;
        end;
      end;

      //调用支付函数
      if not PayIntf_MisByTL(UpData, DownData, sUp, sDwon) then   //返回交易成功的Json,需保存到数据库;
      begin
        Exit;
      end;

      Result := True;
    finally
      FreeAndNil(UpData);
      FreeAndNil(DownData);
    end;
  except
    on e: exception do
    begin
      sMsg := '支付接口交易异常[0]:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function PayIntf_MisByTL(UpData: TUpData; var DownData: TDownData; var sUp,sDown:string): boolean;
var
  Mis, Request, Response: Variant;
  sMsg: string;
  lv_nRet: LongInt;
begin
  {**
   *  1、对UpData、DownData根据规则检查参数必要性和规范性
   *  2、创建3个对象CreateOleObject: MisPos, RequestData, ResponseData
   *  3、赋值 RequestData, ResponseData  <----传入----[UpData、DownData]
   *  4、调用对象MisPos中函数原型如下:
   *     Function TransProcess(Request As RequestData, Response As ResponseData) As Long
   *  5、赋值 DownData  <----传入----[ResponseData]
   *  6、释放3个对象
   *  7、根据返回的DownData判分析是否成功,并返回Ture/Flase
  **}
  Result := False;
  sMsg := '';
  sUp := '';
  sDown := '';
  lv_nRet := -1;
  try
    //格式化数据  例如:000000000001 1分
    if not FormatData(UpData, DownData) then
    begin
      Exit;
    end;
    
    //检查传入参数的规范性
    if not CheckValid(UpData, DownData) then
    begin
      Exit;
    end;
    MyWriteLog('1');
    try
      try
        //创建对象
        Mis := CreateOleObject('ALLINPAY.MisPos');
        Request := CreateOleObject('ALLINPAY.RequestData');
        Response := CreateOleObject('ALLINPAY.ResponseData');
      except
        on e: exception do
        begin
          sMsg := '创建对象异常:' + #13#10 + e.Message;
          MyWriteLog(sMsg);
          Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
          Exit;
        end;
      end;
      MyWriteLog('2');
      //赋值传入的参数
      if not Param_Input(UpData, Request) then
      begin
        Exit;
      end;

      //调用对象函数接口
      sUp := Obj2Json(1, UpData, Response);
      MyWriteLog('提交数据:' + sUp);  //记录日志
      lv_nRet := Mis.TransProcess(Request, Response);
      if lv_nRet <> 0 then  //调用函数 0:成功 非0:失败
      begin
        sMsg := '调用接口失败:' + IntToStr(lv_nRet);
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      sDown := Obj2Json(2, UpData, Response);
      MyWriteLog('返回数据:' + sDown);  //记录日志
      
      //赋值返回传输
      if not Param_OutPut(DownData, Response) then
      begin
        Exit;
      end;
      MyWriteLog('3');
      //在调用函数成功情况下,进一步判断业务是否成功
      if DownData.RejCode <> '00' then   //RejCode  00:成功 非00:失败
      begin
        sMsg := 'Mis交易失败!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      MyWriteLog('5');
      if DownData.RejCode = '00' then    //00:成功
      begin
        sMsg := '返回码:' + DownData.RejCode;
        sMsg := sMsg + ' 返回码解释:' + DownData.RejCodeExplain;
        sMsg := sMsg + ' 流水号:' + DownData.PosTraceNumber;
        sMsg := sMsg + ' 交易唯一标识:' + DownData.TransCheck;
        sMsg := sMsg + ' 返回码解释:' + DownData.RejCodeExplain;

        MyWriteLog(sMsg);
        sDown := Obj2Json(2, UpData, Response);
        Result := True;
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      end;
      MyWriteLog('6');
    finally
      Mis := Unassigned;
      Request := Unassigned;
      Response := Unassigned;
    end;
  except
    on e: exception do
    begin
      sMsg := '支付接口交易异常[1]:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function FormatData(var UpData: TUpData; var DownData: TDownData): boolean;
var
  sMsg: string;
begin
  Result := False;
  sMsg := '';
  try
    UpData.Amount := ExchangeMoney(UpData.Amount, 2);  //小数点保留2位,然后转换为分
    UpData.Amount := SetRigthZero(UpData.Amount, 12);  //金额(12位,分为单位,不足左补0)
    UpData.OldTraceNumber := SetRigthZero(UpData.OldTraceNumber, 6);  //原始流水号(6位,不足左补0)
    UpData.AuthNumber := SetRigthZero(UpData.AuthNumber, 6);  //授权码(6位,不足左补0)
    UpData.HostserialNumber := SetRigthZero(UpData.HostserialNumber, 12);  //原系统参考号(12位,不足左补0)
    UpData.TransDate := SetRigthZero(UpData.TransDate, 4);    //原交易日期(4位,MMDD)
    UpData.ExpireDate := SetRigthZero(UpData.ExpireDate, 4);    //有效期(4位,MMDD)
    Result := True;
  except
    on e: exception do
    begin
      sMsg := '格式化参数异常:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function CheckValid(UpData: TUpData; DownData: TDownData): boolean;
var
  sMsg: string;
begin
  {**
    *   M — 必须有数据	传入
    *   C — 如果有数据则传入(推荐)
    *   TransType: 1签到  2消费  3消费撤销  4隔日退货
    *-------------------------------------------------------------------
    *	                签到/查询余额	消费	撤 销	退 货
    *  CardType         	M       M	M	M
    *  TransType        	M	M	M	M
    *  Amount	        	M	M	M
    *  OldTraceNumber	        		M
    *  HostSerialNumber	        			M
    *  TransDate	        			M
    *  AuthNumber
    *  CardNumber       			C	C
    *  ExpireDate       			C	C
    *  Memo	        	C	C	C
    *  StoreNumber      		C	C	C
    *  PosNumber        		C	C	C
    *  Operator	        	C	C	C
    *  TransCheck       	C	C	C	C
    *--------------------------------------------------------------------
  **}
  Result := False;
  sMsg := '';
  try
    if not FileExists(FILENAME) then     //接口文件不存在
    begin
      sMsg := '通联支付接口文件[' + FILENAME + ']不存在!';
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;

    if (UpData.CardType <> '01') and (UpData.CardType <> '100') then     // 01银行卡  100扫码:微信/支付宝
    begin
      sMsg := '非银行卡或微信支付宝的卡类型!';
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;

    if (UpData.TransType <> '1') and (UpData.TransType <> '2')    // 1签到/查询  2消费  3消费撤销  4隔日退货
      and (UpData.TransType <> '3') and (UpData.TransType <> '4') then
    begin
      sMsg := '非正常交易的交易类型!';
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;

    if (UpData.TransType = '2') or (UpData.TransType = '3') then
    begin
      if UpData.Amount = '' then
      begin
        sMsg := '当交易类型为消费/撤销/退货的时候,不允许金额为空!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      if StrToIntDef(UpData.Amount, 0) = 0 then
      begin
        sMsg := '不允许交易金额零!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      if Length(UpData.Amount) <> 12 then
      begin
        sMsg := '非12位数的交易金额!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
    end;

    if UpData.TransType = '3' then
    begin
      if UpData.OldTraceNumber = '' then
      begin
        sMsg := '当交易类型为撤销的时候,不允许原始流水号为空!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      if Length(UpData.OldTraceNumber) <> 6 then
      begin
        sMsg := '非6位数的原始流水号!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
    end;

    if UpData.TransType = '4' then
    begin
      if UpData.HostserialNumber = '' then    //原系统参考号(12位,不足左补0)
      begin
        sMsg := '当交易类型为退货的时候,不允许原系统参考号为空!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      if Length(UpData.HostserialNumber) <> 12 then
      begin
        sMsg := '非12位数的原系统参考号!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      if UpData.TransDate = '' then          //原交易日期(4位,MMDD)
      begin
        sMsg := '当交易类型为退货的时候,不允许原交易日期为空!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      if Length(UpData.TransDate) <> 4 then
      begin
        sMsg := '非4位数的原交易日期!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      try
        sMsg := '2000' + UpData.TransDate + '01';
        VarToDateTime(sMsg);
      except
        sMsg := '非法的原交易日期[' + sMsg + ']!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
    end;

    if UpData.AuthNumber <> '' then
    begin
      if Length(UpData.AuthNumber) <> 6 then
      begin
        sMsg := '非6位数的授权码!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
    end;

    if (UpData.ExpireDate <> '') and (StrToIntDef(UpData.ExpireDate,0) <> 0) then
    begin
      if Length(UpData.ExpireDate) <> 4 then
      begin
        sMsg := '非4位数的有效期!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
      try
        sMsg := '2000' + UpData.ExpireDate + '01 00:00:01';
        VarToDateTime(sMsg);
      except
        sMsg := '非法的有效期[' + sMsg + ']!';
        MyWriteLog(sMsg);
        Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
        Exit;
      end;
    end;

    Result := True;
  except
    on e: exception do
    begin
      sMsg := '检查传入参数异常:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function Param_Input(UpData: TUpData; var Request: Variant): boolean;
var
  sMsg: string;
begin
  Result := False;
  try
    //请求
    Request.PutValue('PosNumber', UpData.PosNumber);	        //收银机号
    Request.PutValue('StoreNumber', UpData.StoreNumber);  	//门店号
    Request.PutValue('operator', UpData.operator);        	//操作员号
    Request.PutValue('CardNumber', UpData.CardNumber);	        //卡号/付款码/优惠券码
    Request.PutValue('CardType', UpData.CardType);	        //卡种代码(银行卡01,万商通联02……)
    Request.PutValue('TransType', UpData.TransType);	        //交易类型
    Request.PutValue('Amount', UpData.Amount);         	        //金额(12位,分为单位,不足左补0)
    Request.PutValue('OldTraceNumber', UpData.OldTraceNumber);	//原始流水号(6位,不足左补0)
    Request.PutValue('AuthNumber', UpData.AuthNumber);	        //授权码(6位,不足左补0)
    Request.PutValue('HostserialNumber', UpData.HostserialNumber);	//原系统参考号(12位,不足左补0)
    Request.PutValue('TransDate', UpData.TransDate);	        //原交易日期(4位,MMDD)
    Request.PutValue('ExpireDate', UpData.ExpireDate);	        //有效期(4位,MMDD)
    Request.PutValue('Memo', UpData.Memo);	                 //其他信息
    Request.PutValue('TransCheck', UpData.TransCheck);	        //交易唯一标识
    Request.PutValue('BusinessId', UpData.BusinessId);	        //业务编码,用于多业务模式
    Request.PutValue('TransId', UpData.TransId);          	//原交易单号
    Request.PutValue('ProductNumber', UpData.ProductNumber);	//产品编码    万商卡附加KEY
    Request.PutValue('preDef_0', UpData.preDef_0);        	//自定义域1   自定义附加KEY
    Request.PutValue('preDef_1', UpData.preDef_1);        	//自定义域2
    Request.PutValue('preDef_9', UpData.preDef_9);	        //自定义域9
    Request.PutValue('preDef_A', UpData.preDef_A);	        //自定义域10
    Request.PutValue('preDef_B', UpData.preDef_B);	        //自定义域11
    Request.PutValue('preDef_J', UpData.preDef_J);	        //自定义域20
    Result := True;
  except
    on e: exception do
    begin
      sMsg := '传递参数异常[发送]:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function Param_OutPut(var DownData: TDownData; var Response: Variant): boolean;
var
  sMsg: string;
begin
  Result := False;
  try
    //返回
    DownData.Tips := Response.GetValue('Tips');             	//小费
    DownData.Total := Response.GetValue('Total');	                //总计
    DownData.Amount := Response.GetValue('Amount');         	//金额
    DownData.BalanceAmount := Response.GetValue('BalanceAmount');	//余额
    DownData.PosTraceNumber := Response.GetValue('PosTraceNumber');	//流水号
    DownData.OldTraceNumber := Response.GetValue('OldTraceNumber');	//原始流水号
    DownData.ExpireDate := Response.GetValue('ExpireDate');	        //有效期
    DownData.BatchNumber := Response.GetValue('BatchNumber');	//批次号
    DownData.MerchantNumber := Response.GetValue('MerchantNumber');	//商户号
    DownData.MerchantName := Response.GetValue('MerchantName');	//商户名
    DownData.TerminalNumber := Response.GetValue('TerminalNumber');	//终端号
    DownData.HostSerialNumber := Response.GetValue('HostSerialNumber');	//系统参考号
    DownData.AuthNumber := Response.GetValue('AuthNumber');	        //授权码
    DownData.RejCode := Response.GetValue('RejCode');	        //返回码
    DownData.IssNumber := Response.GetValue('IssNumber');	        //发卡行号
    DownData.IssName := Response.GetValue('IssName');	        //发卡行名称
    DownData.CardNumber := Response.GetValue('CardNumber');	        //卡号
    DownData.TransDate := Response.GetValue('TransDate');	        //交易日期
    DownData.TransTime := Response.GetValue('TransTime');	        //交易时间
    DownData.RejCodeExplain := Response.GetValue('RejCodeExplain');	//返回码解释
    DownData.CardBack := Response.GetValue('CardBack');	        //卡片回收标志
    DownData.Memo := Response.GetValue('Memo');	                //备注
    DownData.TransCheck := Response.GetValue('TransCheck');	        //交易唯一标识
    DownData.IsOffline := Response.GetValue('IsOffline');	        //脱机标识:0-联机、1-脱机
    DownData.CUPS := Response.GetValue('CUPS');             	//卡组织
    DownData.TransId := Response.GetValue('TransId');	        //原交易单号
    DownData.ProductNumber := Response.GetValue('ProductNumber');	//产品号码
    DownData.ProductName := Response.GetValue('ProductName');	//产品名称
    DownData.preDef_0 := Response.GetValue('preDef_0');	        //自定义域1   自定义附加KEY
    DownData.preDef_1 := Response.GetValue('preDef_1');	        //自定义域2
    DownData.preDef_9 := Response.GetValue('preDef_9');	        //自定义域9
    DownData.preDef_A := Response.GetValue('preDef_A');	        //自定义域10
    DownData.preDef_B := Response.GetValue('preDef_B');	        //自定义域11
    DownData.preDef_J := Response.GetValue('preDef_J');	        //自定义域20

    Result := True;
  except
    on e: exception do
    begin
      sMsg := '传递参数异常[返回]:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function SetRigthZero(sVaule: string; Len:integer): string;
var
  i: integer;
  Value: string;
  Temp,sMsg: string;
begin
  Result := '0';
  try
    Value := Trim(sVaule);
    if length(Value) = 0 then
    begin
      Value := '0';
    end;
    
    for i := 1 to Len-Length(Value) do
    begin
      Temp := Temp + '0';
    end;
    Result := Temp + Value;    //格式化长度为Len位数
  except
    on e:exception do
    begin
      sMsg := '字段' + Value + '左补零异常:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function ExchangeMoney(Value: string; Ext:integer): string;
var
  S,Temp,sMsg: string;
  i: integer;
begin
  Result := '0';
  try
    for i := 1 to Ext do
    begin
      Temp := Temp + '0';
    end;
    Temp := '0.' + Temp;
    S := Value;
    S := FormatFloat(Temp, StrToFloatDef(S, 0));  //保留小数点Ext位
    S := FloatToStr(StrToFloat(S) * 100);  //转换为分
    Result := S;
  except
    on e:exception do
    begin
      sMsg := '金额转换异常:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

function Obj2Json(nType:integer; UpData:TUpData; Response: Variant): string;
var
  Temp,sMsg: string;
begin
  Result := '对象转换Json错!';
  try
    if nType = 1 then  //请求
    begin
      Temp := '{';
      Temp := Temp + '"PosNumber":"' + UpData.PosNumber + '",';
      Temp := Temp + '"StoreNumber":"' + UpData.StoreNumber + '",';
      Temp := Temp + '"operator":"' + UpData.operator + '",';
      Temp := Temp + '"CardNumber":"' + UpData.CardNumber + '",';
      Temp := Temp + '"CardType":"' + UpData.CardType + '",';
      Temp := Temp + '"TransType":"' + UpData.TransType + '",';
      Temp := Temp + '"Amount":"' + UpData.Amount + '",';
      Temp := Temp + '"OldTraceNumber":"' + UpData.OldTraceNumber + '",';
      Temp := Temp + '"AuthNumber":"' + UpData.AuthNumber + '",';
      Temp := Temp + '"HostserialNumber":"' + UpData.HostserialNumber + '",';
      Temp := Temp + '"TransDate":"' + UpData.TransDate + '",';
      Temp := Temp + '"ExpireDate":"' + UpData.ExpireDate + '",';
      Temp := Temp + '"Memo":"' + UpData.Memo + '",';
      Temp := Temp + '"TransCheck":"' + UpData.TransCheck + '",';
      Temp := Temp + '"BusinessId":"' + UpData.BusinessId + '",';
      Temp := Temp + '"TransId":"' + UpData.TransId + '",';
      Temp := Temp + '"ProductNumber":"' + UpData.ProductNumber + '",';
      Temp := Temp + '"preDef_0":"' + UpData.preDef_0 + '",';
      Temp := Temp + '"preDef_1":"' + UpData.preDef_1 + '",';
      Temp := Temp + '"preDef_9":"' + UpData.preDef_9 + '",';
      Temp := Temp + '"preDef_A":"' + UpData.preDef_A + '",';
      Temp := Temp + '"preDef_B":"' + UpData.preDef_B + '",';
      Temp := Temp + '"preDef_J":"' + UpData.preDef_J + '"';
      Temp := Temp + '}';
    end else begin  //返回
      Temp := '{';
      Temp := Temp + '"Tips":"' + Response.GetValue('Tips') + '",';
      Temp := Temp + '"Total":"' + Response.GetValue('Total') + '",';
      Temp := Temp + '"Amount":"' + Response.GetValue('Amount') + '",';
      Temp := Temp + '"BalanceAmount":"' + Response.GetValue('BalanceAmount') + '",';
      Temp := Temp + '"PosTraceNumber":"' + Response.GetValue('PosTraceNumber') + '",';
      Temp := Temp + '"OldTraceNumber":"' + Response.GetValue('OldTraceNumber') + '",';
      Temp := Temp + '"ExpireDate":"' + Response.GetValue('ExpireDate') + '",';
      Temp := Temp + '"BatchNumber":"' + Response.GetValue('BatchNumber') + '",';
      Temp := Temp + '"MerchantNumber":"' + Response.GetValue('MerchantNumber') + '",';
      Temp := Temp + '"MerchantName":"' + Response.GetValue('MerchantName') + '",';
      Temp := Temp + '"TerminalNumber":"' + Response.GetValue('TerminalNumber') + '",';
      Temp := Temp + '"HostSerialNumber":"' + Response.GetValue('HostSerialNumber') + '",';
      Temp := Temp + '"AuthNumber":"' + Response.GetValue('AuthNumber') + '",';
      Temp := Temp + '"IssNumber":"' + Response.GetValue('IssNumber') + '",';
      Temp := Temp + '"IssNumber":"' + Response.GetValue('IssNumber') + '",';
      Temp := Temp + '"IssName":"' + Response.GetValue('IssName') + '",';
      Temp := Temp + '"CardNumber":"' + Response.GetValue('CardNumber') + '",';
      Temp := Temp + '"TransDate":"' + Response.GetValue('TransDate') + '",';
      Temp := Temp + '"TransTime":"' + Response.GetValue('TransTime') + '",';
      Temp := Temp + '"RejCodeExplain":"' + Response.GetValue('RejCodeExplain') + '",';
      Temp := Temp + '"CardBack":"' + Response.GetValue('CardBack') + '",';
      Temp := Temp + '"Memo":"' + Response.GetValue('Memo') + '",';
      Temp := Temp + '"TransCheck":"' + Response.GetValue('TransCheck') + '",';
      Temp := Temp + '"IsOffline":"' + Response.GetValue('IsOffline') + '",';
      Temp := Temp + '"CUPS":"' + Response.GetValue('CUPS') + '",';
      Temp := Temp + '"TransId":"' + Response.GetValue('TransId') + '",';
      Temp := Temp + '"ProductNumber":"' + Response.GetValue('ProductNumber') + '",';
      Temp := Temp + '"ProductName":"' + Response.GetValue('ProductName') + '",';
      Temp := Temp + '"preDef_0":"' + Response.GetValue('preDef_0') + '",';
      Temp := Temp + '"preDef_1":"' + Response.GetValue('preDef_1') + '",';
      Temp := Temp + '"preDef_9":"' + Response.GetValue('preDef_9') + '",';
      Temp := Temp + '"preDef_A":"' + Response.GetValue('preDef_A') + '",';
      Temp := Temp + '"preDef_B":"' + Response.GetValue('preDef_B') + '",';
      Temp := Temp + '"preDef_J":"' + Response.GetValue('preDef_J') + '"';
      Temp := Temp + '}';
    end;
    Result := Temp;
  except
    on e:exception do
    begin
      sMsg := '对象转换Json异常:' + #13#10 + e.Message;
      MyWriteLog(sMsg);
      Application.MessageBox(Pchar(sMsg), '提示', MB_OK);
      Exit;
    end;
  end;
end;

procedure MyWriteLog(const mStr:string);
var
  f: textfile;
  myDir,myFileName: string;
  FileHandle:Integer;
  LogType,LogDate,ModuleID: String;
begin
  LogType := 'INFO';
  LogDate := FormatDateTime('YYYY-MM-DD hh:nn:ss zzz', Now);
  ModuleID := '';
//  if not (CanLogFile in FLogFlags) then exit;
  //------写入文件部分的实现--开始
  myDir := ExtractFilePath(Paramstr(0));
  //确定文件名称
  myFileName := FormatDateTime('"PayIntf"yyyymmdd".log"', Date);
  //如果可执行目录下不存在log目录创建之
  if not DirectoryExists(myDir + '\log') then CreateDir(myDir + '\log');
  //如果当日日志文件不存在,则创建文件并释放句柄
  if not FileExists(myDir + '\log\' + myFileName) then
  begin
    FileHandle:=FileCreate(myDir + '\log\' + myFileName);//创建文件
    FileClose(FileHandle);//释放句柄
  end;
  try//try...except...statements
    AssignFile(f, myDir + '\log\' + myFileName);
    Append(f);
    Writeln(f, '$$'+Format('%6s',[LogType])+'$$ '+Format('%12s',[ModuleID])+'$$'+LogDate + chr(9) + mStr);
    Flush(f);
    CloseFile(f);
    //-----写入文件部分的实现--结束
  except//try...except...statements
  end;//try...except...statements
end;



end.

  

标签:end,string,Temp,UpData,Delphi,创建对象,sMsg,DLL,Response
来源: https://www.cnblogs.com/studycode/p/11124254.html