微信支付之APP支付

 提示:转载请注明原文链接

 本文永久链接:https://360us.net/article/23.html

这里讲的是微信开放平台移动应用里面的微信支付功能。

开放平台的微信支付和公众号的微信支付是不一样的。


然后是我下面的代码仅仅是做了基本的消费功能。


基本流程如下:
1、获取access token。
2、提交预支付订单信息,拿到prepayid
3、组成包括prepayid在内的相关信息数据返回给客户端发起支付请求。


在提交预支付订单之前需要先获取到access token才行。
这个token是公用的有效期是7200秒。
具体获取方法可以看下面的WechatAppPay类。
去拿这个access token不是凭公众号的appid和secret去拿而是用微信开放平台移动应用的appid和secret去拿的。

这里我写了一些个专门做微信支付的操作的类,有需要的可以拿去参考。
下面的代码我都实际测试通过了。开放平台文档版本是V1.7。
WechatPayBase类,主要就是一些基础公用功能的方法,代码清单如下:

<?php
namespace common\services\WechatPay;

/**
* @link http://www.360us.net/ 
* @author dyllen_zhong@qq.com
*/
class WechatPayBase
{
    /**
     * 取成功响应
     * @return string
     */
    public function getSucessXml()
    {
        $xml = '<xml>';
        $xml .= '<return_code><![CDATA[SUCCESS]]></return_code>';
        $xml .= '<return_msg><![CDATA[OK]]></return_msg>';
        $xml .= '</xml>';
        return $xml;
    }
    
    /**
     * 取失败响应
     * @return string
     */
    public function getFailXml()
    {
        $xml = '<xml>';
        $xml .= '<return_code><![CDATA[FAIL]]></return_code>';
        $xml .= '<return_msg><![CDATA[OK]]></return_msg>';
        $xml .= '</xml>';
        return $xml;
    }
    
    /**
     * 数组转成xml字符串
     *
     * @param array $arr
     * @return string
     */
    protected function arrayToXml($arr)
    {
        $xml = '<xml>';
        foreach($arr as $key => $value) {
            $xml .= "<{$key}>";
            $xml .= "<![CDATA[{$value}]]>";
            $xml .= "</{$key}>";
        }
        $xml .= '</xml>';
    
        return $xml;
    }
    
    /**
     * xml 转换成数组
     * @param string $xml
     * @return array
     */
    protected function xmlToArray($xml)
    {
        $xmlObj = simplexml_load_string(
                $xml,
                'SimpleXMLIterator',   //可迭代对象
                LIBXML_NOCDATA
        );
    
        $arr = [];
        $xmlObj->rewind(); //指针指向第一个元素
        while (1) {
            if( ! is_object($xmlObj->current()) )
            {
                break;
            }
            $arr[$xmlObj->key()] = $xmlObj->current()->__toString();
            $xmlObj->next(); //指向下一个元素
        }
    
        return $arr;
    }
    
    /**
     * 数组转成字符串
     * 
     * @param array $arr
     * @return string
     */
    protected  function arrayToString($arr)
    {
        $str = '';
        foreach($arr as $key => $value) {
            $str .= "{$key}={$value}&";
        }
    
        return substr($str, 0, strlen($str)-1);
    }
    
    /**
     * 通过POST方法请求URL
     * @param string $url
     * @param array|string $data post的数据
     *
     * @return mixed
     */
    protected function postUrl($url, $data) {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //忽略证书验证
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($curl);
        return $result;
    }
    
    /**
     * 通过GET方法请求URL
     * @param string $url
     *
     * @return mixed
     */
    protected function getUrl($url) {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //忽略证书验证
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
    
    /**
     * 获取随机字符串
     * @return string 不长于32位
     */
    protected function getRandomStr()
    {
        return substr( rand(10, 999).strrev(uniqid()), 0, 15 );
    }
    
    /**
     * MD5签名
     *
     * @param string $str 待签名字符串
     * @return string 生成的签名
     */
    protected function signMd5($str)
    {
        return md5($str);
    }
    
    /*
     * 过滤待签名数据,sign和空值不参加签名
     *
     * @return array
     */
    protected function filter($params)
    {
        $tmpParams = [];
        foreach ($params as $key => $value) {
            if( $key != 'sign' && ! empty($value) ) {
                $tmpParams[$key] = $value;
            }
        }
    
        return $tmpParams;
    }
}


下面是WechatAppPay类,这个类就是只负责微信APP支付的相关功能,它继承自WechatPayBase
我的想法的,如果还有其他支付类型,比如公众号JS发起支付,那么还可以新建一个WechatJsPay的类,同样也是继承自WechatPayBase,依次扩展。
WechatAppPay的代码清单如下:

<?php
namespace common\services\WechatPay;

/**
* @link http://www.360us.net/ 
* @author dyllen_zhong@qq.com
*/
class WechatAppPay extends WechatPayBase
{
    //package参数
    public $package = [];
    
    //异步通知参数
    public $notify = [];
    
    //推送预支付订单参数
    protected $config = [];
    
    //存储access token和获取时间的文件
    protected $file;
    
    //access token
    protected $accessToken;
    
    //取access token的url
    const ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
    
    //生成预支付订单提交地址
    const POST_ORDER_URL = 'https://api.weixin.qq.com/pay/genprepay?access_token=%s';
    
    public function __construct()
    {
        $this->file = __DIR__ . '/payAccessToken.txt';
    }
    
    /**
     * 创建APP支付最终返回参数
     * @throws \Exception
     * @return multitype:string NULL
     */
    public function createAppPayData()
    {
        $this->generateConfig();
        
        $prepayid = $this->getPrepayid();
        
        try{
            $array = [
                'appid' => $this->appid,
                'appkey' => $this->paySignkey,
                'noncestr' => $this->getRandomStr(),
                'package' => 'Sign=WXPay',
                'partnerid' => $this->partnerId,
                'prepayid' => $prepayid,
                'timestamp' => (string)time(),
            ];
            
            $array['sign'] = $this->sha1Sign($array);
            unset($array['appkey']);
        } catch(\Exception $e) {
            throw new \Exception($e->getMessage());
        }
        
        return $array;
    }
    
    /**
     * 验证支付成功后的通知参数
     * 
     * @throws \Exception
     * @return boolean
     */
    public function verifyNotify()
    {
        try{
            $staySignStr = $this->notify;
            unset($staySignStr['sign']);
            $sign = $this->signData($staySignStr);
            
            return $this->notify['sign'] === $sign;
        } catch(\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    
    /**
     * 魔术方法,给添加支付参数进来
     * 
     * @param string $name  参数名
     * @param string $value  参数值
     */
    public function __set($name, $value)
    {
        $this->$name = $value;
    }
    
    /**
     * 设置access token
     * @param string $token
     * @throws \Exception
     * @return boolean
     */
    public function setAccessToken()
    {
        try{
            if(!file_exists($this->file) || !is_file($this->file)) {
                $f = fopen($this->file, 'a');
                fclose($f);
            }
            $content = file_get_contents($this->file);
            if(!empty($content)) {
                $info = json_decode($content, true);
                if( time() - $info['getTime'] < 7150 ) {
                    $this->accessToken = $info['accessToken'];
                    return true;
                }
            }
            
            //文件内容为空或access token已失效,重新获取
            $this->outputAccessTokenToFile();
        } catch(\Exception $e) {
            throw new \Exception($e->getMessage());
        }
        
        return true;
    }
    
    /**
     * 写入access token 到文件
     * @throws \Exception
     * @return boolean
     */
    protected function outputAccessTokenToFile()
    {
        try{
            $f = fopen($this->file, 'wb');
            $token = [
                'accessToken' => $this->getAccessToken(),
                'getTime' => time(),
            ];
            flock($f, LOCK_EX);
            fwrite($f, json_encode($token));
            flock($f, LOCK_UN);
            fclose($f);
            
            $this->accessToken = $token['accessToken'];
        } catch(\Exception $e) {
            throw new \Exception($e->getMessage());
        }
        
        return true;
    }
    
    /**
     * 取access token
     * 
     * @throws \Exception
     * @return string
     */
    protected function getAccessToken()
    {
        $url = sprintf(self::ACCESS_TOKEN_URL, $this->appid, $this->appSecret);
        $result = json_decode( $this->getUrl($url), true );
        
        if(isset($result['errcode'])) {
            throw new \Exception("get access token failed:{$result['errmsg']}");
        }
        
        return $result['access_token'];
    }
    
    /**
     * 取预支付会话标识
     * 
     * @throws \Exception
     * @return string
     */
    protected function getPrepayid()
    {
        $data = json_encode($this->config);
        $url = sprintf(self::POST_ORDER_URL, $this->accessToken);
        $result = json_decode( $this->postUrl($url, $data), true );
        
        if( isset($result['errcode']) && $result['errcode'] != 0 ) {
            throw new \Exception($result['errmsg']);
        }
        
        if( !isset($result['prepayid']) ) {
            throw new \Exception('get prepayid failed, url request error.');
        }
        
        return $result['prepayid'];
    }
    
    /**
     * 组装预支付参数
     * 
     * @throws \Exception
     */
    protected function generateConfig()
    {
        try{
            $this->config = [
                    'appid' => $this->appid,
                    'traceid' => $this->traceid,
                    'noncestr' => $this->getRandomStr(),
                    'timestamp' => time(),
                    'package' => $this->generatePackage(),
                    'sign_method' => $this->sign_method,
            ];
            $this->config['app_signature'] = $this->generateSign();
        } catch(\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    
    /**
     * 生成package字段
     * 
     * 生成规则:
     * 1、生成sign的值signValue
     * 2、对package参数再次拼接成查询字符串,值需要进行urlencode
     * 3、将sign=signValue拼接到2生成的字符串后面得到最终的package字符串
     * 
     * 第2步urlencode空格需要编码成%20而不是+
     * 
     * RFC 1738会把 空格编码成+
     * RFC 3986会把空格编码成%20
     * 
     * @return string
     */
    protected function generatePackage()
    {
        $this->package['sign'] = $this->signData($this->package);
        
        return http_build_query($this->package, '', '&', PHP_QUERY_RFC3986);
    }
    
    /**
     * 生成签名
     * 
     * @return string
     */
    protected function generateSign()
    {
        $signArray = [
            'appid' => $this->appid,
            'appkey' => $this->paySignkey,
            'noncestr' => $this->config['noncestr'],
            'package' => $this->config['package'],
            'timestamp' => $this->config['timestamp'],
            'traceid' => $this->traceid,
        ];
        return $this->sha1Sign($signArray);
    }
    
    /**
     * 签名数据
     * 
     * 生成规则:
     * 1、字典排序,拼接成查询字符串格式,不需要urlencode
     * 2、上一步得到的字符串最后拼接上key=paternerKey
     * 3、MD5哈希字符串并转换成大写得到sign的值signValue
     * 
     * @param array $data 待签名数据
     * @return string 最终签名结果
     */
    protected function signData($data)
    {
        ksort($data);
        $str = $this->arrayToString($data);
        $str .= "&key={$this->partnerKey}";
        return strtoupper( $this->signMd5($str) );
    }
    
    /**
     * sha1签名
     * 签名规则
     * 1、字典排序
     * 2、拼接查询字符串
     * 3、sha1运算
     * 
     * @param array $arr
     * @return string
     */
    protected function sha1Sign($arr)
    {
        ksort($arr);
        
        return sha1( $this->arrayToString($arr) );
    }

}



下面是用上面的两个类来做APP支付的后台服务器操作。

//相关变量
$partnerId = '1234567890'; //财付通商户号
$partnerKey => '11b9fa282cvf9d35e5dhj9is7c1d4f54'; //财付通partnerKey
$appId => 'wxd930ea5d5a258f4f'; //开放平台移动App Id
$appSecret => 'edfdrgfhzdfdghfjuhlkjghl'; //开放平台移动App Secret
$paySignKey => 'sdfdgffdhgfjhkjlkyu';  //微信下发的邮件里面有
$order = '201503034758525df748d1'; //商户自己的订单号
$price = 10; //金额

//下面开始
$weixinPay = new WechatAppPay(); 

//设置相关参数
$weixinPay->appid = $appId;
$weixinPay->appSecret = $appSecret;
$weixinPay->traceid = $order;
$weixinPay->sign_method = 'sha1';  //这个目前固定
$weixinPay->paySignkey = $paySignKey;
$weixinPay->partnerId = $partnerId;
$weixinPay->partnerKey = $partnerKey;
            
$weixinPay->package = [
    'bank_type' => 'WX', //银行通道类型
    'body' => '会员充值', //商品描述
    'partner' => $partnerId,//商户号
    'out_trade_no' => $order, //订单号
    'total_fee' => (string)($price * 100), //总金额,单位分
    'fee_type' => '1', //支付币种,
    'notify_url' => 'http://www.example.com/notify', //通知Url
    'spbill_create_ip' => '192.168.100.65', //终端IP
    'input_charset' => 'UTF-8', //传入参数的编码类型
];

$weixinPay->setAccessToken(); //传入配置之后才可以获取access token

$return = $weixinPay->createAppPayData();



上面最终结果变量$return是数组,内容形式如下:

"appid":"wxd930ea5d5a258f4f",
"noncestr":"e7d161ac8d8a76529d39d9f5b4249ccb",
"package":"Sign=WXpay";
"partnerid":"1900000109"
"prepayid":"1101000000140429eb40476f8896f4c9",
"sign":"7ffecb600d7157c5aa49810d2d8f28bc2811827b",
"timestamp":"1399514976"

上面的最终内容是返回给APP发起客户端微信支付请求的参数。


下面的支付成功之后的通知处理过程:

$get = $_GET;
$weixinPay = new WechatAppPay();
$weixinPay->notify = $get;
$weixinPay->partnerKey = $partnerKey;
if( !$weixinPay->verifyNotify() ) {
    return 'fail';
}

if($get['trade_state'] == 0) {
    //业务处理流程....
}
return success;


成功处理通知应该返回success文本,否则返回fail或其他内容。
如果没有返回success微信会重复发送通知。


2015/4/20 提示:

开放平台移动应用APP支付的文档链接已经跳转到商户平台的文档:http://pay.weixin.qq.com/wiki/doc/api/index.html

已经和上面写的不一样了。


2015/11/30提示:

这里:https://pay.weixin.qq.com/wiki/doc/api/index.html

的四种支付方式,我们分两类:

1、刷卡支付,公众号支付和扫码支付

这类需要使用公众平台的公众号;

2、APP支付

这类需要用开放平台的账号的。


这里有点混的地方是,上面两类公众平台和开放平台的支付申请下来之后会有各自对应的商户平台账号。

统一下单接口里面的appid和mch_id用各自平台账号的就行!签名用的key需要在各个平台对应的商户平台后台去设置后再使用。


 评论
暂无评论