PHP code example of guanhui07 / dg-php-sdk

1. Go to this page and download the library: Download guanhui07/dg-php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

guanhui07 / dg-php-sdk example snippets


BsPay::init( '/path/to/BsPayConfig.json', false);


    use BsPaySdk\core\BsPayClient;
    use BsPaySdk\request\V2TradePaymentJspayRequest;

    $requestParams = new V2TradePaymentJspayRequest();
    // 请求日期
    $requestParams->setReqDate(date("Ymd"));
    // 请求流水号
//        $requestParams->setReqSeqId(date("YmdHis") . mt_rand());
    $requestParams->setReqSeqId($orderNo);
    // 商户号
    $requestParams->setHuifuId(self::HUIFU_ID);
    // 商品描述
    $requestParams->setGoodsDesc($title);
    // 交易类型 	T_JSAPI: 微信公众号
    //T_MINIAPP: 微信小程序
    //A_JSAPI: 支付宝JS
    //A_NATIVE: 支付宝正扫
    //U_NATIVE: 银联正扫
    //U_JSAPI: 银联JS
    //D_NATIVE: 数字人民币正扫
    //T_H5:微信直连H5支付
    //T_APP:微信APP支付
    //T_NATIVE:微信正扫
    $requestParams->setTradeType($tradeType);
    // 交易金额 单位元
    $requestParams->setTransAmt((string)$amount);

    // 设置非必填字段
    $extendInfoMap = $this->getExtendInfos();
    $requestParams->setExtendInfo($extendInfoMap);

    $client = new BsPayClient();
    $result = $client->postRequest($requestParams);
    if (!$result || $result->isError()) {  //失败处理
        var_dump($result->getErrorInfo());
        throw new CoreException('支付发起失败');
    }

    //成功处理
    $result = $resultObj->getRspDatas();


    # 从文件导入商户系统参数
    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");
    ...
   

   # 导入初始化内容
   LE__). "/../BsPaySdk/request/V2MerchantBusiOpenRequest.php";
   

    # 请求必填参数
    $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);
   

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

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

   # 导入初始化内容
   LE__). "/../BsPaySdk/request/V2MerchantBusiOpenRequest.php";
   

    # 请求实例
    $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),
        ),
    );
   

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

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

$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);

$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);

composer 

    "autoload": {
        ......
        "classmap": ["vendor/guanhui07/dg-php-sdk/BsPaySdk"]
    },