PHPicbc ⼯商银⾏开放平台聚合⽀付,⼆维码扫码⽀付对接步骤
前⾔ 最近因为⼯作原因对接⼯商银⾏聚合⽀付接⼝和⼆维码扫码⽀付接⼝,⽹上php教程太少,因此记录⼀下对接步骤。 程序开发之前要做的准备⼯作
1、阅读相关接⼝⽂档,熟悉⼯⾏⽀付接⼝调⽤流程,接⼝⽂档地址如下:。附:,
2、准备相关的参数,通⽤参数为:
appid:⼯商银⾏appid
mer_id:⼯商银⾏商户账号,商户线下档案编号,特约商户12位,特约部门15位
store_code:e⽣活档案编号
icbc_pulic_key:⽹关公钥
private_key:应⽤私钥
public_key:应⽤公钥
encryptKey:AES加密密钥
2.1、相关参数来源
appid,mer_id,store_code,icbc_pulic_key皆为⼯⾏⼯作⼈员提供,
private_key应⽤公私钥对可点击下载后⾃⼰⽣成。
Windows环境,以⽣成RSA算法为例:
进⼊到bin⽬录,双击keygen_rsa.bat⽂件,⽣成⼀对RSA公钥和密钥。
Linux环境,以⽣成RSA算法为例:
切换到bin⽬录,运⾏ ./keygen_rsa.sh命令,⽣成⼀对RSA公钥和密钥。
⽣成公私钥对后将公钥给⼯作⼈员配置。
注意还必须提供encryptKey(AES加密密钥)配置,⽤于算法的加解密。
3、和⼯作⼈员申请开通相关接⼝,⼯⾏是分接⼝开放的,接⼝必须配置到你的账户下,才能进⾏调⽤。
4、下载⼯⾏的
注意事项:⾥⾯要求安装php_infosec扩展,此扩展是为了完成ca验证进⾏安装的,聚合⽀付和⼆维码扫码⽀付接⼝⼀般⽤不到ca验证,只⽤RSA2和AES的话应该⽤不到,因为都是公开算法。
5、代码⽰例,使⽤代码类的时候,主要要先将你的SDK⽂件引⼊类中。<?php /** * Created by PhpStorm. * User: lv  * Date: 2020/3/10 * Time: 13:37 *//** * 功能说明:⾃定义⼯商银⾏⽀付接⼊类 */class  IcbcPay {    function  __construct ()    {        //引⼊⽂件        require_once (ROOT_PATH  . 'data/extend/icbc/icbc-api-sdk-cop-php/DefaultIcbcClient.php');        require_once (ROOT_PATH  . 'data/extend/icbc/icbc-api-sdk-cop-php/UiIcbcClient.php');    }    public  function  index ()    {        // 防⽌默认⽬录错误    }    /**
* 基本设置读取
1
2
3
4
5
6
7
8
9
10
11
12
中国工商银行app
13
14
15
16
17
18
19
20
21
22
23
24
25
26
* 基本设置读取    *    * @return unknown      */    public  function  getIcbcpayConfig ()    {        // ⼯商
银⾏appid (颁发的必填参数)        $icbc_config ['appid'] = '';        // ⼯商银⾏商户账号(商户线下档案编号(特约商户12位,特约部门15位))        $icbc_config ['mer_id'] = '';        // e ⽣活档案编号        $icbc_config ['store_code'] = '';        // 接⼝号,⽬前仅⽀持上送1.0.0.1        $icbc_config ['interface_version'] = '1.0.0.1';        // 第三⽅应⽤ID ;商户在内接⼊时必送,此处设定为的appid        $icbc_config ['tp_app_id'] = '';        // ⽹关公钥        $icbc_config ['icbc_pulic_key'] = '';        // 应⽤私钥        $icbc_config ['private_key'] = '';        // 应⽤私钥        $icbc_config ['encrypt_key'] = '';        return  $icbc_config ;    }    /**    * @param $msg_id //消息通讯唯⼀编号,每次调⽤独⽴⽣成,APP 级唯⼀    * @param $tp_open_id //第三⽅⽤户标识;商户在/⽀付宝⽣活号内接⼊时必送,上送⽤户在商户appid 下的唯⼀标识。    * @param $out_trade_no //商户订单号;需保证商户系统唯⼀    * @param string $tran_type //交易类型 OfflinePay-线下⽀付,OnlinePay-线上⽀付    * @param $goods_body //商品描述  芬达    * @param $goods_detail //商品详情  {‘good_name’:’芬达橙味300ml 罐装’,’good_id’:’1001’,’good_num’:’1’}    * @param $order_amount // 总⾦额(单位:分)    * @param $return_url // ⽀付成功回显页⾯。⽀付成功后,客户端引导跳转⾄该页⾯显⽰    * @param $notify_url // ⽀付结果通知地址;    * @param $mer_hint // 商家提⽰。⽬前暂⽆处理,后续可⽤于在交易页⾯回显给客户    * @param $attach // 附加数据。商户可上送定制信息    * @return string      * 当前接⼝参数类型全部为字符串类型,调⽤时注意!    */    public  function  setAPV2Pay ($msg_id , $tp_open_id , $out_trade_no , $tran_type  = 'OnlinePay', $goods_body , $go
ods_detail , $order_amount , $return_url    {        $icbc_config  = $this ->getIcbcpayConfig ();        //第三⽅应⽤ID ;商户在内接⼊时必送,上送分配的公众账号ID ;商户通过⽀付宝⽣活号接⼊时必送,上送⽀付宝分配的应⽤ID 。        $tp_app_id  = $icbc_config ['tp_app_id'];        //分期期数。⽬前仅⽀持1-不分期,注意必须是字符串类型        $install_times  = '1';        //通知类型,表⽰在交易处理完成后把交易结果通知商户的处理模式。 取值“HS”:在交易完成后将通知信息,主动发送给商户,发送地址为notify_url 指定地        $notify_type  = 'HS';        //结果发送类型,通知⽅式为HS 时有效。取值“0”:⽆论⽀付成功或者失败,银⾏都向商户发送交易通知信息;取值“1”,银⾏只向商户发送交易成功的通知        $result_type  = '0';        $content  = array (            "interface_version" => (string )$icbc_config ['interface_version'],
"mer_id"            => (string )$icbc_config ['mer_id'],
262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
"mer_id"            => (string )$icbc_config ['mer_id'],            "tp_app_id"        => (string )$tp_app_id ,            "tp_open_id"        => (string )$tp_open_id ,            "out_trade_no"      => (string )$out_trade_no ,            "tran_type"        => (string )$tran_type ,            "order_date"        => (string )date ("YmdHis", $_SERVER ['REQUEST_TIME']),            "end_time"          => (string )date ("YmdHis", $_SERVER ['R
EQUEST_TIME'] + 300),  // order_date 之后5分钟            "goods_body"        => (string )$goods_body ,            "goods_detail"      => (string )$goods_detail ,            "order_amount"      => (string )bcmul ($order_amount , 100), //总⾦额(单位分)            "spbill_create_ip"  => (string )$_SERVER ['REMOTE_ADDR'],            "install_times"    => (string )$install_times ,            "return_url"        => (string )$return_url , // ⽀付回显            "notify_url"        => (string )$notify_url , // ⽀付结果通知            "notify_type"      => (string )$notify_type ,            "result_type"      => (string )$result_type ,        );        if  (!empty ($attach )) $content ['attach'] = $attach ;        if  (!empty ($mer_hint )) $content ['mer_hint'] = $mer_hint ;        //接⼝调⽤地址        $serviceUrl  = 'gw.open.icbc/ui/aggregate/payment/request/V2';        //调⽤参数合集        $request  = array (            "serviceUrl"    => $serviceUrl ,            "method"        => 'POST',// 请求⽅法,只能是POST 或GET            "isNeedEncrypt" => true ,// 是否需要加密            "extraParams"  => null ,//其他参数,⽤数组类型array            "biz_content"  => $content ,        );        //组成form 表单提交        $resp  = $this ->icbcClientForm ($icbc_config , $request , $msg_id );        return  $resp ;    }    /**    * ⼯商银⾏获取⽀付⼆维码    * 当前接⼝参数类型全部为字符串类型,调⽤时注意。    */    public  function  getPayQrCode ($msg_id , $out_trade_no , $pay_expire , $order_amt , $notify_url , $attach )    {        $icbc_config  = $this ->getIcbcpayConfig ();        //商户是否开启通知接⼝ 0-否;1-是;⾮1按0处理,注意必须是字符串类型        $notify_flag  = '1';        //扫码后是否需要跳转分⾏,0:否,1:是;⾮1按
0处理,注意必须是字符串类型        $sp_flag  = '0';        $content  = array (            'mer_id'            => (string )$icbc_config ['mer_id'],            'out_trade_no'      => (string )$out_trade_no ,            'store_code'        => (string )$icbc_config ['store_code'],//e ⽣活档案编号            'order_amt'        => (string )bcmul ($order_amt , 100), //总⾦额(单位分)            'trade_date'        => (string )date ("Ymd", $_SERVER ['REQUEST_TIME']),  //订单⽣成⽇期            'trade_time'        => (string )date ("His", $_SERVER ['REQUEST_TIME']),  //订单⽣成时间            'tporder_create_ip' => (string )$_SERVER ['REMOTE_ADDR'],            'notify_url'        => (string )$notify_url , // ⽀付结果通知            'notify_flag'      => (string )$notify_flag ,
'sp_flag'          => (string )$sp_flag ,
9293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
'sp_flag'          => (string )$sp_flag ,            'pay_expire'        => (string )$pay_expire ,//⼆维码过期时间        );        if  (!empty ($attach )) $content ['attach'] = $attach ;        //接⼝调⽤地址        $serviceUrl  = 'gw.open.icbc/api/qrcode/V2/generate';        //调⽤参数合集        $request  = array (            "serviceUrl"    => $serviceUrl ,            "method"        => 'POST',// 请求⽅法,只能是POST 或GET            "isNeedEncrypt" => false ,// 是否需要加密            "extraParams"  => null ,//其他参数,⽤数
组类型array            "biz_content"  => $content ,        );        //进⾏curl 提交,并获取返回值        $resp  = $this ->icbcClientCurl ($icbc_config , $request , $msg_id );        return  $resp ;    }    /**    * 建⽴请求,提交信息,聚合⽀付 构造form 表单    * @param $icbc_config      * @param $json_content      * @param $msg_id      */    public  function  icbcClientForm ($icbc_config , $request , $msg_id )    {        //APP 的编号,应⽤在API 开放平台注册时⽣成        $appId  = $icbc_config ['appid'];        //应⽤私钥        $privateKey  = $icbc_config ['private_key'];        //签名类型,CA-⼯⾏颁发的证书认证,RSA-RSAWithSha1,RSA2-RSAWithSha256,缺省为RSA2,        $signType  = \IcbcConstants ::$SIGN_TYPE_RSA2;        //字符集 ,缺省为UTF-8        $charset  = 'UTF-8';        //请求参数格式,仅⽀持json        $format  = 'json';        //⽹关公钥        $icbcPulicKey  = $icbc_config ['icbc_pulic_key'];        //AES 加密密钥,缺省为空        $encryptKey  = $icbc_config ['encrypt_key'];        //加密⽅式,本接⼝参数需进⾏AES 加密        $encryptType  = 'AES';        //采⽤ca 认证⽅式时,需上送证书,使⽤此签名算法的时候,php 需要安装扩展php_infosec.dll ,⼯⾏sdk 提供的是php7.0以上的版本        $ca  = '';        //当签名类型为CA 时,通过该字段上送证书密码,缺省为空        $password  = '';        //初始化链接请求构造        $client  = new  \UiIcbcClient ($appId , $privateKey , $signType , $charset , $format ,            $icbcPulicKey , $encryptKey , $encryptType , $ca , $password );
157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
$icbcPulicKey , $encryptKey , $encryptType , $ca , $password );        //请求接⼝下单        $resp  = $client ->buildPostForm ($request , $msg_id , '');        return  $resp ;    }    /**    * 建⽴请求,提交信息,构造curl 请求    * @param $icbc_config      * @param $json_content      * @param $msg_id      */    public  function  icbcClientCurl ($icbc_config , $request , $msg_id )    {        //APP 的编号,应⽤在API 开放平台注册时⽣成        $appId  = $icbc_config ['appid'];        //应⽤私钥        $privateKey  = $icbc_config ['private_key'];        //签名类型,CA-⼯⾏颁发的证书认证,RSA-RSAWithSha1,RSA2-RSAWithSha256,缺省为RSA2,        $signType  = \IcbcConstants ::$SIGN_TYPE_RSA2;        //字符集 ,缺省为UTF-8        $charset  = 'UTF-8';        //请求参数格式,仅⽀持json        $format  = 'json';        //⽹关公钥        $icbcPulicKey  = $icbc_config ['icbc_pulic_key'];        //AES 加密密钥,缺省为空        $encryptKey  = $icbc_config ['encrypt_key'];        //加密⽅式,本接⼝参数需进⾏AES 加密        $encryptType  = 'AES';        //采⽤ca 认证⽅式时,需上送证书,使⽤此签名算法的时候,php 需要安装扩展php_infosec.dll ,⼯⾏sdk 提供的是php7.0以上的版本        $ca  = '';        //当签名类型为CA 时,通过该字段上送证书密码,缺省为空        $password  = '';        //初始化链接请求构造 此处注意,⼯⾏官⽹sdk ⽂件中提供的【IcbcSignature::verify 】⽅法缺失参数,需要补上$password 参数,否则⽆法调⽤成功。        $client  = new  \DefaultIcbcClient ($appId , $privateKey , $signType , $charset , $format ,            $icbcPulicKey , $encryptKey , $encryptType , $ca , $password );        //请求接⼝下单        $resp  = $client ->execute ($request , $msg_id , '');        $resp  = $resp  ? json_decode ($resp ,
true ) : '';        return  $resp ;    }    /**    * * 回调签名验证    * @param $postArr  ⼯⾏返回参数    * @param $notify_url  回调url      * @return int|void      * @throws Exception      */    public  function  icbcCheckSign ($postArr , $notify_url )222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287