码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

开发环境Delphi XE11.3

只有部分代码,做个笔记;这个只有调人脸识别(Windows SDK)代码,只有部分代码

 

{微信刷脸支付}
unit UWX_PayFaceSDK;

interface

uses
   System.SysUtils, System.DateUtils, System.JSON;

type

  mWX_PayFace_CMD_Type =(mWX_init, mWX_getRawdata, mWX_getCode, mWX_updatePayResult, mWX_stop, mWX_releas) ;  //

   function f_wx_payCallFaceService(reqBuf: PAnsiChar; reqSize: Cardinal; out pRespBuf: PAnsiChar; out pRespSize: PCardinal): integer; cdecl; external 'WxpayFaceSDK.dll' name 'wxpayCallFaceService';
   function f_wx_payReleaseResponse(out pRespBuf: PAnsiChar): integer; cdecl; external 'WxpayFaceSDK.dll' name 'wxpayReleaseResponse';

   function f_wx_CallpayFaceService(const AReq: string; out AResp: string): Integer;

   function f_wx_GetCMD_Type(const ACMDType: mWX_PayFace_CMD_Type): string;// 获取CMD类型
   function f_wx_GetErrorCode(const AErrorCode: string; out ErrorMsg: string): Boolean;// 获取错误码的解析
   function f_wx_JsonFixedPart(const ACMDType: string; Out AJson: string): Boolean;// 获取JSON的固定部分
   function f_TowJsonMergeToStr(const AJsonFront, AJsonBehind: string): string; //两个JSON字符串合并成一个

   function f_wx_initPayface(out AResp: string): Integer;//微信 初始化人脸支付   ---对人脸SDK进行初始化
   function f_wx_getPayfaceRawdata(out AResp: string): Integer;//微信 获取rawdata数据  ---获取rawdata数据
   function f_wx_getPayfaceCode(const AReqJson: string; out AResp: string): Integer;//微信 进行人脸识别(获取支付凭证)  ---开启人脸识别,获取支付凭证和用户信息
   function f_wx_updatePayfacePayResult(const AReqJson: string; out AResp: string): Integer;//微信 更新支付结果  ---商户侧确认支付结果后通知人脸SDK更新支付结果,用户确认支付结果后返回,刷脸支付界面关闭
   function f_wx_stopPayface(const AReqJson: string; out AResp: string): Integer;//微信 停止刷脸支付   ---商户侧发起取消刷脸支付(按需调用),刷脸支付界面会关闭,仅在人脸凭证/付款码face_code未返回前可用,face_code返回后不可停止
   function f_wx_release(out AResp: string): Integer;//微信 释放资源  ---释放人脸服务

implementation


const
  uVersionNo: string = '1';

function f_wx_CallpayFaceService(const AReq: string; out AResp: string): Integer;
var
  ReqAnsi: Ansistring;
  ReqRawByte: RawByteString;
  vResult, vReqLength: Integer;
  vOutResp: PAnsiChar;
  vOutRespSize: PCardinal;
  vRespAnsi: Ansistring;
  vRespRawByte: RawByteString;
  vFreeResp: PAnsiChar;
begin
  Result := -1;
  ReqRawByte:= Utf8Encode(AReq);
  SetString(ReqAnsi, PAnsiChar(ReqRawByte), Length(ReqRawByte));
  vReqLength := Length(ReqAnsi);
  vResult := f_wx_payCallFaceService(PAnsiChar(ReqAnsi), vReqLength, vOutResp, vOutRespSize);
  if vResult = 0 then
  begin
    vRespRawByte:= RawByteString(vOutResp);
    vRespAnsi := UTF8ToString(vRespRawByte);
    AResp := vRespAnsi;
    f_wx_payReleaseResponse(vFreeResp);
  end;
  Result := vResult;
end;

function f_wx_GetCMD_Type(const ACMDType: mWX_PayFace_CMD_Type): string;
begin
  case ACMDType of
    mWX_init: Result := 'initWxpayface';  //设备初始化
    mWX_getRawdata: Result := 'getWxpayfaceRawdata';//获取设备数据
    mWX_getCode: Result := 'getWxpayfaceCode';//刷脸,获取付款码
    mWX_updatePayResult: Result := 'updateWxpayfacePayResult'; //告诉设备支付结果
    mWX_stop: Result := 'stopWxpayface';//停止刷脸--这个没有用到
    mWX_releas: Result := 'releaseWxpayface'; //释放设备资源
  else
    Result := '';
  end;
end;

function f_wx_GetErrorCode(const AErrorCode: string; out ErrorMsg: string): Boolean;// 获取错误码的解析
begin
  Result := True;
  if AErrorCode = 'SUCCESS' then
    ErrorMsg := '接口成功'
  else if AErrorCode = 'ERROR' then
    ErrorMsg := '接口失败'
  else if AErrorCode = 'PARAM_ERROR' then
    ErrorMsg := '参数错误'
  else if AErrorCode = 'SYSTEMERROR' then
    ErrorMsg := '接口返回错误'
  else if AErrorCode = 'USER_CANCEL' then
    ErrorMsg := '用户退出了人脸识别'  //返回到结账流程
  else if AErrorCode = 'SCAN_PAYMENT' then
    ErrorMsg := '用户选择扫码支付' //进入扫码支付流程
  else
  begin
    Result := False;
    ErrorMsg := '未知错误码';
  end;
end;

function f_wx_JsonFixedPart(const ACMDType: string; Out AJson: string): Boolean;
var
	vJson: TJSONObject;
  vInt: Int64;
begin
  Result := False;
  vJson := TJSONObject.Create;
  try
    vInt := DateTimeToUnix(Now) - 60*60*8;
    vJson.AddPair('cmd', ACMDType);
    vJson.AddPair('version', uVersionNo);
    vJson.AddPair('now', vInt);
    AJson := vJson.ToString;
    Result := True;
  finally
    FreeAndNil(vJson);
  end;
end;

function f_TowJsonMergeToStr(const AJsonFront, AJsonBehind: string): string;
var
  vStr1, vStr2: string;
  vPos: Integer;
begin
  Result := '';
  if (AJsonFront = '') or (AJsonBehind = '') then
    Exit;
  vStr1 := Trim(AJsonFront);
  vStr2 := Trim(AJsonBehind);
  vPos := Length(vStr1);
  Delete(vStr1,vPos,1);
  Delete(vStr2,1,1);
  Result := vStr1 +',' + vStr2;
end;

function f_wx_initPayface(out AResp: string): Integer;
var
  vReq, vCMDType: string;
begin
  Result := -1;
  vCMDType := f_wx_GetCMD_Type(mWX_init);
  if vCMDType = '' then
    Exit;
  if f_wx_JsonFixedPart(vCMDType, vReq) then
    Result := f_wx_CallpayFaceService(vReq, AResp);
end;

function f_wx_getPayfaceRawdata(out AResp: string): Integer;
var
  vReq, vCMDType: string;
begin
  Result := -1;
  vCMDType := f_wx_GetCMD_Type(mWX_getRawdata);
  if vCMDType = '' then
    Exit;
  if f_wx_JsonFixedPart(vCMDType, vReq) then
    Result := f_wx_CallpayFaceService(vReq, AResp);
end;

function f_wx_getPayfaceCode(const AReqJson: string; out AResp: string): Integer;
var
  vFixedPartJson, vCMDType, vReqJson: string;
begin
  Result := -1;
  if AReqJson = '' then
    Exit;
   vCMDType := f_wx_GetCMD_Type(mWX_getCode);
  if vCMDType = '' then
    Exit;
  f_wx_JsonFixedPart(vCMDType, vFixedPartJson);
  if vFixedPartJson = '' then
    Exit;
  vReqJson := f_TowJsonMergeToStr(vFixedPartJson, AReqJson);
  if vReqJson = '' then
    Exit;
  Result := f_wx_CallpayFaceService(vReqJson, AResp);
{\"cmd\":\"getWxpayfaceCode\",\"version\":\"1\",\"now\":1540907996,
\"appid\":\"wx97debaxyzabcca2\",\"mch_id\":\"1281087510\",\"store_id\":\"IMG001\",
\"face_authtype\":\"FACEPAY\",\"authinfo\":"sdjfgjsdh",\"out_trade_no\":\"10302159564hzhw2\",
\"total_fee\":\"100\",\"face_code_type\":\"1\"}
end;

function f_wx_updatePayfacePayResult(const AReqJson: string; out AResp: string): Integer;
var
  vFixedPartJson, vCMDType, vReqJson: string;
begin
  Result := -1;
  if AReqJson = '' then
    Exit;
   vCMDType := f_wx_GetCMD_Type(mWX_updatePayResult);
  if vCMDType = '' then
    Exit;
  f_wx_JsonFixedPart(vCMDType, vFixedPartJson);
  if vFixedPartJson = '' then
    Exit;
  vReqJson := f_TowJsonMergeToStr(vFixedPartJson, AReqJson);
  if vReqJson = '' then
    Exit;
  Result := f_wx_CallpayFaceService(vReqJson, AResp);

{"cmd":\"updateWxpayfacePayResult\",\"version\":\"1\",\"now\":1540908003,\"appid\":\"wx97debaxyzabcca2\",
\"mch_id\":\"1281087510\",\"store_id\":\"IMG001\",\"payresult\":\"SUCCESS\",\"authinfo\":\"sdfsdfsdf"}
end;

function f_wx_stopPayface(const AReqJson: string; out AResp: string): Integer;
var
  vFixedPartJson, vCMDType, vReqJson: string;
begin
  Result := -1;
  if AReqJson = '' then
    Exit;
   vCMDType := f_wx_GetCMD_Type(mWX_stop);
  if vCMDType = '' then
    Exit;
  f_wx_JsonFixedPart(vCMDType, vFixedPartJson);
  if vFixedPartJson = '' then
    Exit;
  vReqJson := f_TowJsonMergeToStr(vFixedPartJson, AReqJson);
  if vReqJson = '' then
    Exit;
  Result := f_wx_CallpayFaceService(vReqJson, AResp);
{\"cmd\":\"stopWxpayface\",\"version\":\"1\",\"now\":1540908003,
\"appid\":\"wx97debaxyzabcca2\",\"mch_id\":\"1281087510\",\"authinfo\":\"gfhfgh"};
end;

function f_wx_release(out AResp: string): Integer;
var
  vReq, vCMDType: string;
begin
  Result := -1;
  vCMDType := f_wx_GetCMD_Type(mWX_releas);
  if vCMDType = '' then
    Exit;
  if f_wx_JsonFixedPart(vCMDType, vReq) then
    Result := f_wx_CallpayFaceService(vReq, AResp);
end;

end.

  

 

posted on 2024-12-05 16:49  码农的笔记  阅读(120)  评论(0)    收藏  举报