云支付整合Tp3.2.3代码整理

时间:2022-09-25 21:25:03

  可能比较少的项目用会用到云支付,我之前有个项目就用这个支付,相对于微信支付和支付宝支付他名气太小了,但是很多支付宝或者微信审核不下来就回选择这种支付方式,总体开发比较简单。云支付官网是这么说的“云支付是国内最大的第三方支付接口整合平台,它集成了支付宝,微信,银联支付接口,网银支付等国内著名第三方支付平台,只需几行代码就接入所有支付接口,让用户支付一步到位。通过使用云支付提供的产品服务,让网上收款更方便。”,下面开始运用到项目中:
1、话不多说第一步先看看官方的支付核心包,还是比较简单,总感觉这东西不太安全,这个核心包放置在"Core\Library\Vendor\Yunpay"下:

<?php
// +----------------------------------------------------------------------
// | 云支付核心包
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2016 http://www.bieanju.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bieanju <bieanju@163.com>
// | CreateTime : 2016
// +----------------------------------------------------------------------
namespace Vendor\Yunpay;
class YunPay{
    
    public $key;
    public $pid;
        
    public function __construct(){
        $this->key = trim(C("yunpay_config.key"));
        $this->pid = trim(C("yunpay_config.partner"));
    } 
    /* 云支付核心处理函数 */
    public function md5Verify($i1, $i2,$i3) {
        $prestr = $i1 . $i2.$this->pid.$this->key;
        $mysgin = md5($prestr);
    
        if($mysgin == $i3) {
            return true;
        }
        else {
            return false;
        }
    }
    
    public function i2e($parameter,$subm){
        foreach ($parameter as $pars) {
            $myparameter.=$pars;
        }
        $sign=md5($myparameter.'i2eapi'.$this->key);
        $mycodess="<form name='yunsubmit' action='http://pay.yunpay.net.cn/i2eorder/yunpay/' accept-charset='utf-8' method='get'><input type='hidden' name='body' value='".$parameter['body']."'/><input type='hidden' name='out_trade_no' value='".$parameter['out_trade_no']."'/><input type='hidden' name='partner' value='".$parameter['partner']."'/><input type='hidden' name='seller_email' value='".$parameter['seller_email']."'/><input type='hidden' name='subject' value='".$parameter['subject']."'/><input type='hidden' name='total_fee' value='".$parameter['total_fee']."'/><input type='hidden' name='nourl' value='".$parameter['nourl']."'/><input type='hidden' name='reurl' value='".$parameter['reurl']."'/><input type='hidden' name='orurl' value='".$parameter['orurl']."'/><input type='hidden' name='orimg' value='".$parameter['orimg']."'/><input type='hidden' name='sign' value='".$sign."'/></form><script>document.forms['yunsubmit'].submit();</script>";
        return $mycodess;
    }
    
    public function hifun($string,$operation){
        $key=md5('hifun2013');
        $key_length=strlen($this->key);
        $string=$operation=='D'?base64_decode($string):substr(md5($string.$this->key),0,8).$string;
        $string_length=strlen($string);
        $rndkey=$box=array();
        $result='';
        for($i=0;$i<=255;$i++)
        {
            $rndkey[$i]=ord($this->key[$i%$key_length]);
            $box[$i]=$i;
        }
        for($j=$i=0;$i<256;$i++)
        {
            $j=($j+$box[$i]+$rndkey[$i])%256;
            $tmp=$box[$i];
            $box[$i]=$box[$j];
            $box[$j]=$tmp;
        }
        for($a=$j=$i=0;$i<$string_length;$i++)
        {
        $a=($a+1)%256;
            $j=($j+$box[$a])%256;
            $tmp=$box[$a];
            $box[$a]=$box[$j];
            $box[$j]=$tmp;
            $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
        }
        if($operation=='D')
        {
        if(substr($result,0,8)==substr(md5(substr($result,8).$this->key),0,8))
            return substr($result,8);
            else
            return'';
        }else{
        return str_replace('=','',base64_encode($result));
        }
    }
}

?>

2、第二步来看看实际的应用中的业务逻辑处理:

  2.1、配置参数:

/* yunpay config */ 
        'yunpay_config' => array(
            'partner' => '960xxxx',
            'key' => 'xxxxxxxx' ,
            'seller_email'=>'xx@xx.com'
        ),      
        'yunpay' => array(
            'notify' => pay_url."index.php/User/YunPay/notifyUrl",
            'return' => pay_url."index.php/User/YunPay/returnUrl"
        )

  2.2、同步,异步通知业务逻辑处理(此处需要注意的是异步通知方法处理,开发中由于类继承了基础父类结果导致异步通知不到,因为做了是否登录的验证异步就通知不到,所以需要注意这个支付类不用任何的其他验证。):

<?php
// +----------------------------------------------------------------------
// | 云支付[订单处理]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2016 http://www.bieanju.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bieanju <bieanju@163.com>
// | CreateTime : 2016
// +----------------------------------------------------------------------
namespace User\Controller;
use Vendor\Yunpay\YunPay; class YunPayController extends Controller{ /** * 支付成员变量 * @Author Bieanju **/ public $orderId,$out_trade_no,$trade_no,$yunprice; public function _initialize() { parent::_initialize(); header("Content-Type:text/html; charset=utf-8"); } /** * 云支付 * @Author Bieanju **/ public function yunpay(){ $model = D("Order"); $where['id'] = $this->orderId ; $order = $model->where($where)->field('number,buy_sn,pay_amount,buy_status')->find(); $out_trade_no = $order['buy_sn'];//商户网站订单系统中唯一订单号,必填 $subject = "支付[订单编号:".$out_trade_no."]支付测试--别安驹";//必填 $total_fee = $order['pay_amount'];//必填 需为整数 $body = $subject; $parameter = array( "partner" => trim(C("yunpay_config.partner")), "seller_email" => C("yunpay_config.seller_email"), "out_trade_no" => $out_trade_no, "subject" => $subject, "total_fee" => $total_fee, "body" => $body, "nourl" => C("yunpay.notify"), "reurl" => C("yunpay.return"), "orurl" => "", "orimg" => "" ); $YunPayObj = new YunPay(); $html_text = $YunPayObj->i2e($parameter, "支付进行中..."); echo $html_text; } /** * 同步通知 * @Author Bieanju **/ public function returnUrl(){ //计算得出通知验证结果 $YunPayObj = new YunPay(); $yunNotify = $YunPayObj->md5Verify($_REQUEST['i1'],$_REQUEST['i2'],$_REQUEST['i3']); if($yunNotify) { $this->out_trade_no = $_REQUEST['i2']; $this->trade_no = $_REQUEST['i4']; $this->yunprice=$_REQUEST['i1']; self::afterPayLogic() && self::payLog(); } else { echo "验证失败"; } } /** * 异步通知 * @Author Bieanju **/ public function notifyUrl(){ $YunPayObj = new YunPay(); //计算得出通知验证结果 $yunNotify = $YunPayObj->md5Verify($_REQUEST['i1'],$_REQUEST['i2'],$_REQUEST['i3']); if($yunNotify) { echo "success"; }else { echo "fail"; } } /** * 支付完成更新数据 * @Author Bieanju **/ private function afterPayLogic(){ $model = D("Order");$where['buy_sn'] = $this->out_trade_no; $orderInfo = $model->where($where)->getField('sale_sn'); $result = $model->where(array("sale_sn"=>$orderInfo))->setField('status',2);
     return $result !== false ? true : false;
} /** * 日志记录 * @Author Bieanju **/ private function payLog(){ $model = D("MemberPayLog"); /* data post */ $data['uid'] = $this->userid; $data['pay_no'] = $this->out_trade_no; $data['type'] = 0; $data['create_time'] = time(); $data['amount'] = $this->yunprice; $data['trade_no'] = $this->trade_no; /* end */ return $model->add($data) ? true : false; } } ?>

Ok,云支付成功,如果返回“错误代码:00002993”表示当前的域名未授权,请去云支付官方授权当前域名后即可,是不是很简单呢!