guanhui07/dg-php-sdk

汇付天下斗拱SDK-PHP版

2.0.15 2025-01-08 06:43 UTC

This package is auto-updated.

Last update: 2025-01-08 07:08:18 UTC


README

为了提高客户接入的便捷性,本系统提供 SDK 方式介入,使用本 SDK 将极大的简化开发者的工作, 开发者将无需考虑通信、签名、验签等,只需要关注业务参数的输入。

下载地址

SDK下载

版本记录

SDK 包结构说明

BsPaySdk 目录下内容为待添加到项目中的文件;

BsPayDemo 目录下为示例项目,供接入时参考使用;

使用方法

composer require guanhui07/dg-php-sdk:dev-master

执行命令,使配置生效

composer dumpautoload

json BsPayConfig.json

{
  "************* Demo演示生成环境测试商户参数 *************":"",
  "sys_id":"6666000108854952",
  "product_id":"YYZY",    
  "rsa_merch_private_key":"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCxtfk3rjwdpBV81WBy5jIMcDLFdvHckhjGXkmWfaBn7euPRyetEhS4inpr7EvQ5KDUXNBPljI2NVhG/LEGZKvau1MW8j3t7dJ3gWafuVGsCiLJHU79sIRHf11nKOTykX5WxB/7MMwRnZsECuaZyCk7WPuSAlznqbDJdrZTzHhjQzMhjto1qD6+vc0OxyaBFlOY9piBtEfecsvD+6GfQ8exFqwzblJm9iZPYw02DaeUDLFO9Umn7i7gShlj/1Hh8nEM7YitpF/p26o+MC9LHWbIjgzjvNVhSRVmbvWys+3S11Zm/vux6Yzfk0H3fqrksAKSEkLEtEoYKS4wKjHdecztAgMBAAECggEACy1g4WmqCks5tsJM8K0d1L5x0w2qJK9js4ZWpop8Pk0ulbJqAm6ysvCyxnr0Qc0/eFvmFjtiKRqt1LksATTvwjAqB7Vww7hDlpSi+cTUKDfy/CdFwpsJlt2h6E0gKUmRYq+vO0NUcn8xMs3ktyNpxHvSRtqzMTbxEZrP2PFxWPzUKGNyk53FTlJ64YCoGQqWeGhA5LO6QLPHlAxIrvRf9B5dtXQr5XZXVqS9MwjtsRPvQPWiFXxlzvhJRcL/wXehcNextHzpMMgX/idB3H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etFRZw2TQrSc51wgtCkJi1/x8Wl7maPL1uH2+77JFlPv7H/F4Lr2I2LXgnllg6PtwOSw/qvGYInVVB4kL85VQl0/8ObyxBUdJ43I0z/u8hJb2gwujSudOGizbeqQXAYrwcNy+e+cjodpPy9unpJjBfa4Wz2eVLLvUYYKZKdRn6pZR2cQsMBvL30K4cFlZqlJ9iP2hTG3gaiZJ9JrjTigwki0g9pbTDXiPACfuF1nOeObvLD22zBbgn1kwgfsqoG67z7g84u2jvfUFCzX1JRgd0xfNorTRkS2RQIDAQAB"
}
BsPay::init( '/path/to/BsPayConfig.json', false);

usage

    use BsPaySdk\core\BsPayClient;
    use BsPaySdk\request\V2MerchantActivityAddRequest;

    $request = new V2MerchantActivityAddRequest();
    // 请求日期
    $request->setReqDate(date("Ymd"));
    // 请求流水号
    $request->setReqSeqId(date("YmdHis").mt_rand());
    // 汇付客户Id
    $request->setHuifuId("6666000103627938");
    // 营业执照图片
    $request->setBlPhoto("42204258-967e-373c-88d2-1afa4c7bb8ef");
    // 店内环境图片
    $request->setDhPhoto("42204258-967e-373c-88d2-1afa4c7bb8ef");
    // 手续费类型
    $request->setFeeType("7");
    // 整体门面图片(门头照)
    $request->setMmPhoto("42204258-967e-373c-88d2-1afa4c7bb8ef");
    // 收银台照片
    $request->setSytPhoto("42204258-967e-373c-88d2-1afa4c7bb8ef");
    // 支付通道
    $request->setPayWay("W");

    // 设置非必填字段
    $extendInfoMap = [];
    $request->setExtendInfo($extendInfoMap);

    // 3. 发起API调用
    $client = new BsPayClient();
    $result = $client->postRequest($request);
    if (!$result || $result->isError()) {  //失败处理
        var_dump($result -> getErrorInfo());
    } else {    //成功处理
        var_dump($result);
    }

2.3 完成系统参数的导入

 # 从文件导入商户系统参数
 BsPay::init(dirname(__FILE__). '/config/config_merch_default.json', false);
 
 # sdk也支持追加多套系统参数,并可以在调用接口时进行切换,适用于下辖多商户的系统接入
 BsPay::init(dirname(__FILE__). '/config/config_merch_2.json', false, "merchantKey2");
 BsPay::init(dirname(__FILE__). '/config/config_merch_3.json', false, "merchantKey3");
 ...

2.4 以上内容,建议参考 SDK Demo 中 loader.php 文件内的代码写法

3. SDK 调用方法(一):使用接口 request 实例调用,以商户业务开通接口为例

3.1 导入前一步实现的 SDK 初始化文件

# 导入初始化内容
require_once dirname(__FILE__) . "/loader.php";
require_once  dirname(__FILE__). "/../BsPaySdk/request/V2MerchantBusiOpenRequest.php";

3.2 根据接口文档说明,创建对应请求的参数数据体

 # 请求必填参数
 $request = new V2MerchantBusiOpenRequest();
 // 请求流水号
 $request->setReqSeqId(date("YmdHis").mt_rand());
 // 请求日期
 $request->setReqDate(date("Ymd"));
 // 汇付ID
 $request->setHuifuId("6666000104778898");
 // 渠道商汇付ID
 $request->setUpperHuifuId("6666000003080000");
 
 # 请求拓展参数 (可选)
 $extendInfoMap = getExtendInfos() ;
 $request->setExtendInfo($extendInfoMap);

3.3 调用接口

# 创建请求Client对象,调用接口
 $client = new BsPayClient();
 $result = $client->postRequest($request);

3.4 处理返回结果

# 成功/失败应答的处理
if (!$result || $result->isError()){  //失败处理
    var_dump($result -> getErrorInfo());
} else {    //成功处理
    var_dump($result);
}

3.5 以上内容,建议参考 SDK Demo 中 V2MerchantBusiOpenRequestDemo.php 文件内的代码写法

4. SDK 调用方法(二):使用集中传参方式调用,以商户业务开通接口为例

除基于接口 request 实例调用外,另提供一种集中传参方式,拓展适应不同需求;

4.1 导入前一步实现的 SDK 初始化文件

# 导入初始化内容
require_once dirname(__FILE__) . "/loader.php";
require_once  dirname(__FILE__). "/../BsPaySdk/request/V2MerchantBusiOpenRequest.php";

4.2 根据接口文档说明,创建对应请求的参数数据体

 # 请求实例
 $request = new V2MerchantBusiOpenRequest();
 
 // 请求参数,不区分必填和可选,按照 api 文档 data 参数结构依次传入
 $param = array(
     "funcCode" => $request->getFunctionCode(),
     "params" => array(
         "req_seq_id" => date("YmdHis").mt_rand(),
         "req_date" => date("Ymd"),
         "huifu_id" => "6666000104778898",
         "upper_huifu_id" => "6666000003080000",
         "balance_pay_config" => json_encode(array(
             "fee_rate" =>"2",
             "fee_fix_amt" =>"1",
         ),JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
     ),
 );

4.3 调用接口

# 创建请求Client对象,调用接口
 $client = new BsPayClient();
 $result = $client->postRequest($param);

4.4 处理返回结果

# 成功/失败应答的处理
if (!$result || $result->isError()){  //失败处理
    var_dump($result -> getErrorInfo());
} else {    //成功处理
    var_dump($result);
}

5 验签工具(BsPayTools.php)

  • 校验签名 - verifySign_sort

    (此方法为返回报文签名验签,将返回data 按字母顺序排序后组成json 字符串,依据 RSA2 算法使用公钥进行验签

    示例:

$data = array(
    'bank_code' =>'10000',
    'bank_message' =>'Success',
    'hf_seq_id' =>'002900TOP2B220511142822P984ac132ff400000',
    'huifu_id' =>'6666000108854952',
    'qr_code' =>'https://qr.alipay.com/bax04465kcijedllqhuq004b',
    'req_date' =>'20220511',
    'req_seq_id' =>'202205111428211506200',
    'resp_code' =>'00000100',
    'resp_desc' =>'下单成功',
    'trade_type' =>'A_NATIVE',
    'trans_amt' =>'1.00',
    'trans_stat' =>'P',
);
$sign = "OUotiU75VW7SdEwLIZX3gAqSgZk8hCjE7r01WQr8mDdm23B+zd58r8HNWvE9BWV+mTwZ2iAOSuht9SOGM+spSYFANa3VIqMZzGim3y4aZmptQTTptNcclocsWyocn78efdAuTcGvf5dhUc6/Ue1oYV+BVhphYPmkKUKfxpEvBEvw/vlpsCu0I0Dx/k7kN6IaxY6mODypFmDtnEaZbkGaxbh8yxH1lJDn5/91YfD6vpK+sRJXiVXLzDK13BPAjQ3RAlFUxHJ8LPJbWQQpABQ94Gd1TTc/bfOluqUwJJbofC7WZiIOW6MKsa9gL5Y6lmbqFcMBKfvexJ0SlRFLWvSkQg==";
$merConfig = BsPay::getConfig();
$result = BsPayTools::verifySign_sort($sign,$data,$merConfig->rsa_huifu_public_key);
  • 校验 webhook 返回报文签名 - verify_webhook_sign

    (此方法为异步回调验签方法,将返回 data 字符串与商户配置的key 组合后进行md5计算,返回的md5值与回调sign的比较结果

    示例:

$data = array(
    'bank_code' =>'10000',
    'bank_message' =>'Success',
    'hf_seq_id' =>'002900TOP2B220511142822P984ac132ff400000',
    'huifu_id' =>'6666000108854952',
    'qr_code' =>'https://qr.alipay.com/bax04465kcijedllqhuq004b',
    'req_date' =>'20220511',
    'req_seq_id' =>'202205111428211506200',
    'resp_code' =>'00000100',
    'resp_desc' =>'下单成功',
    'trade_type' =>'A_NATIVE',
    'trans_amt' =>'1.00',
    'trans_stat' =>'P',
);
$sign = 'dcc64089c44ea77cfde785de4cfa97ba';
$key = 'test_key';
$result = BsPayTools::verify_webhook_sign($sign,$data,$key);