tp接入富友协议支付

本文档详细介绍了如何接入富友协议支付,包括在.env文件和application目录下的config.php文件中的基本配置,以及控制器里的具体操作步骤。

第一步:相关基本配置:

.env文件里:

[pay]
#product = true
#mchntcd = '0003320F2144842'
#secret  = 'zxppac23oeolb9gweugjxt1d8ntxpgkh'
version = '1.0'

application目录下config.php文件里:

'pay' => [
    'product'   => Env::get('pay.product',false),
    'mchntcd'   => Env::get('pay.product',false) ? Env::get('pay.mchntcd'):'0002900F0096235',
    'secret'    => Env::get('pay.product',false) ? Env::get('pay.secret'):'5old71wihg2tqjug9kkpxnhx9hiujoqj',
    'version'   => Env::get('pay.version'),
],

第二步:控制器里:

private $config = [];

private $client = null;

public function __construct()
{
    $this->config = config('pay');
    $this->config['domain'] = $this->config['product'] ? 'https://mpay.fuioupay.com/newpropay/':'http://www-1.fuioupay.com:18670/mobile_pay/newpropay/';
    $this->client = new \GuzzleHttp\Client();
    parent::__construct();
}
/**
 * 发送绑卡短信
 */
public function bindMsg()
{
    $postUrl = $this->config['domain'].'bindMsg.pay';
    $postFm  = $this->config['product'] ? 'https://mpay.fuioupay.com/findPay/cardBinQuery.pay':'http://www-1.fuioupay.com:18670/mobile_pay/findPay/cardBinQuery.pay';
    $mchntcd = $this->config['mchntcd'];
    $secret  = $this->config['secret'];
    $version = $this->config['version'];
    $uid     = input('uid');
    $date    = date('Ymd',time());
    $oid     = $date.date('His').mt_rand(1000,9999);
    $account = input('account');
    $cardno  = input('cardno');
    $idtype  = 0;
    $idcard  = input('idcard');
    $phone   = input('mobile');
    if(!$uid || !$account || !$cardno|| !$idcard|| !$phone){
        $this->error('参数缺失',null,201);
    }
    $binSign = md5($mchntcd.'|'.$cardno.'|'.$secret);
    $fm      = '<FM><MchntCd>'. $mchntcd .'</MchntCd> <Ono>'. $cardno .'</Ono> <Sign>'. $binSign .'</Sign></FM>';
    $resFm   = $this->getResult($postFm,$fm,true);
    if($resFm['Rcd'] != '0000'){
        $this->error($resFm['RDesc'],null,201);
    }
    $sign    = md5($version.'|'.$oid.'|'.$mchntcd.'|'.$uid.'|'.$account.'|'.$cardno.'|'.$idtype.'|'.$idcard.'|'.$phone.'|'.$secret);
    $apifms = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><REQUEST><VERSION>'. $version .'</VERSION><MCHNTCD>'. $mchntcd .'</MCHNTCD><USERID>'. $uid .'</USERID> <TRADEDATE>'. $date .'</TRADEDATE><MCHNTSSN>'. $oid .'</MCHNTSSN><ACCOUNT>'. $account .'</ACCOUNT><CARDNO>'. $cardno .'</CARDNO><IDTYPE>'. $idtype .'</IDTYPE><IDCARD>'. $idcard .'</IDCARD><MOBILENO>' .$phone. '</MOBILENO><SIGN>'. $sign .'</SIGN></REQUEST>';
    $res = $this->getResult($postUrl,$apifms);
    if($res['RESPONSECODE'] == '0000'){
        $this->success('成功',['date' => $date,'oid' => $oid,'uid' => $uid,'bank' => $resFm['Cnm']],200);
    }else{
        $this->error($res['RESPONSEMSG'],null,201);
    }
}
/**
 * 协议卡绑定
 */
public function bindCommit()
{
    $postUrl = $this->config['domain'].'bindCommit.pay';
    $mchntcd = $this->config['mchntcd'];
    $secret  = $this->config['secret'];
    $version = $this->config['version'];
    $uid     = $data['userid']   = input('uid');
    $date    = input('date');
    $oid     = input('mcntssn');
    $account = $data['account'] = input('account');
    $cardno  = $data['cardno']  = input('cardno');
    $idtype  = 0;
    $idcard  = $data['idcard']  = input('idcard');
    $phone   = $data['mobile']  = input('mobile');
    $code    = input('code');
    if(!$uid || !$account || !$cardno || !$idcard || !$phone || !$code){
        $this->error('参数缺失',null,201);
    }
    $sign    = md5($version.'|'.$oid.'|'.$mchntcd.'|'.$uid.'|'.$account.'|'.$cardno.'|'.$idtype.'|'.$idcard.'|'.$phone.'|'.$code.'|'.$secret);//签名
    $apifms  = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><REQUEST><VERSION>'. $version .'</VERSION><MCHNTCD>'. $mchntcd .'</MCHNTCD><USERID>'. $uid .'</USERID> <TRADEDATE>'. $date .'</TRADEDATE><MCHNTSSN>'. $oid .'</MCHNTSSN><ACCOUNT>'. $account .'</ACCOUNT><CARDNO>'. $cardno .'</CARDNO><IDCARD>'.$idcard.'</IDCARD><IDTYPE>'.$idtype.'</IDTYPE><MOBILENO>'.$phone.'</MOBILENO><MSGCODE>'.$code.'</MSGCODE><SIGN>'. $sign .'</SIGN></REQUEST>';
    $res = $this->getResult($postUrl,$apifms);
    if($res['RESPONSECODE'] == '0000'){
        $data['is_bind']    = 1;
        $data['mchntssn']   = $res['MCHNTSSN'];
        $data['protocolno'] = $res['PROTOCOLNO'];
        $data['backname']   = input('bank');
        if($bindcard = BindCard::create($data)){
            $this->success('绑卡成功',$bindcard->id,200);
        }else{
            $this->error('入库失败',null,201);
        }
    }else{
        $this->error($res['RESPONSEMSG'],null,201);
    }
}
/**
 * 协议卡解绑
 */
public function unbind()
{
    $postUrl  = $this->config['domain'].'unbind.pay';
    $mchntcd  = $this->config['mchntcd'];
    $secret   = $this->config['secret'];
    $version  = $this->config['version'];
    $uid      = input('uid');
    $bindcard = BindCard::get(input('bcid'));
    if($bindcard->userid != $uid){
        $this->error('无权限',null,201);
    }
    $protocol = $bindcard->protocolno;
    $sign     = md5($version.'|'.$mchntcd.'|'.$uid.'|'.$protocol.'|'.$secret);
    $apifms   = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><REQUEST><VERSION>'. $version .'</VERSION><MCHNTCD>'. $mchntcd .'</MCHNTCD><USERID>'. $uid .'</USERID><PROTOCOLNO>'. $protocol .'</PROTOCOLNO>><SIGN>'. $sign .'</SIGN></REQUEST>';
    $res = $this->getResult($postUrl,$apifms);
    if($res['RESPONSECODE'] == '0000'){
        $bindcard->is_bind = 2;
        if($bindcard -> save()){
            $this->success('解绑成功',null,200);
        }else{
            $this->error('更新数据失败',null,201);
        }
    }else{
        $this->error('解绑失败',null,201);
    }
}

/**
 * 协议支付
 */
public function order()
{
    $postUrl  = $this->config['domain'].'order.pay';
    $version  = $this->config['version'];
    $ip       = $this->request->ip();
    $mchntcd  = $this->config['mchntcd'];
    $secret   = $this->config['secret'];
    $type     = '03';
    $oid      = date('YmdHis').mt_rand(1000,9999);    //商户订单号
    $uid      = input('uid');                            //用户id
    $price    = input('price');                          //支付金额
    $bcid     = input('bcid');                              //协议卡id
    if(!$uid || !$price || !$bcid){
        $this->error('参数缺失',null,201);
    }
    $needMsg  = 0;                                            //是否需要发短信
    $notice   = 'http://im.ttyoumi.cn/api/pay/notice';        //回调url
    $protocol = BindCard::where('id',$bcid)->value('protocolno');
    $sign     = md5($type.'|'.$version.'|'.$mchntcd.'|'.$oid.'|'.$uid.'|'.$protocol.'|'.$price.'|'.$notice.'|'.$ip.'|'.$secret);//签名
    $apifms = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> <REQUEST><VERSION>'. $version .'</VERSION><USERIP>'. $ip .'</USERIP> <MCHNTCD>'. $mchntcd .'</MCHNTCD><TYPE>'. $type .'</TYPE> <MCHNTORDERID>'. $oid .'</MCHNTORDERID> <USERID>'. $uid .'</USERID><AMT>'. $price .'</AMT><PROTOCOLNO>'. $protocol .'</PROTOCOLNO> <NEEDSENDMSG>'. $needMsg .'</NEEDSENDMSG> <BACKURL>'. $notice .'</BACKURL><SIGNTP>MD5</SIGNTP> <SIGN>'. $sign .'</SIGN></REQUEST>';
    $res = $this->getResult($postUrl,$apifms);
    if($res['RESPONSECODE'] == '0000'){
        $data = [
            'userid'        => $uid,
            'version'       => $res['VERSION'],
            'type'          => $res['TYPE'],
            'mchntorderid'  => $res['MCHNTORDERID'],
            'orderid'       => $res['ORDERID'],
            'protocolno'    => $res['PROTOCOLNO'],
            'bankcard'      => $res['BANKCARD'],
            'amt'           => $res['AMT'],
            'signtp'        => $res['SIGNTP'],
            'responsemsg'   => $res['RESPONSEMSG']
        ];
        if($result = Repayment::create($data)){
            $this->success($res['RESPONSEMSG'],null,200);
        }else{
            $this->error('插入数据失败',null,202);
        }
    }else{
        $this->error($res['RESPONSEMSG'],null,201);
    }
}

/**
 * 支付回调
 */
public function notice()
{
    \think\Log::write(input(),'debug');
    $this->success();
}
/**
 * des加密,ecb
 * @param $str
 * @param $key
 * @return string
 */
public static function encrypt_base64($str,$key){
    $str = self::pkcs5_pad($str, 8);
    if (strlen($str) % 8) {
        $str = str_pad($str,strlen($str) + 8 - strlen($str) % 8, "\0");
    }
    $sign = openssl_encrypt($str,'DES-ECB' ,$key,OPENSSL_RAW_DATA);
    return base64_encode($sign);
}

/**
 * des解密
 * @param $str
 * @param $key
 * @return string
 */
public static function decrypt_base64($str,$key){
    $sign = openssl_decrypt(base64_decode($str),'DES-ECB' ,$key,OPENSSL_RAW_DATA);
    return $sign;
}
/**
 * 数据填充
 * @param $text
 * @param $blocksize
 * @return string
 */
private static function pkcs5_pad($text, $blocksize) {
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}

private function getResult($postUrl,$apifms,$fm = false)
{
    $secret = $this->config['secret'];
    if($fm){
        $response = $this->client->request('POST',$postUrl,['form_params' => ['FM'  => $apifms]]);
        $response = $response->getBody();
    }else{
        $apifms = self::encrypt_base64($apifms,$secret);
        $data = ['MCHNTCD' => $this->config['mchntcd'],'APIFMS' => $apifms];
        $response = $this->client->request('POST',$postUrl,['form_params' => $data]);
        $response = self::decrypt_base64($response->getBody(),$secret);
    }
    return $this->xmlToArray($response);
}

private function xmlToArray($xml)
{
    return json_decode(json_encode(simplexml_load_string($xml), true),true);
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值