码迷,mamicode.com
首页 > 微信 > 详细

php微信公众号支付接口开发demo

时间:2017-10-30 14:38:29      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:路径   head   yii   $_server   length   protect   encode   component   www.   

 本支付接口使用Yii2框架,所以控制器的格式都是该框架的,不过放到其他框架都差不多,根据对应的规则修改一下控制器的方法名字就行了,亲测有效,比较简单,没有封装,想了解微信支付实现流程的可以看看。

<?php
namespace frontend\controllers;

use Yii;
use frontend\components\Controller;

class PayController extends Controller
{
    private $app_id = ‘xxxxxxxxx‘;//appid和app_secret在开通微信支付后接收到的邮件里面可看到

    private $mch_id = ‘123456790‘;//商户号

    private $makesign = ‘sahdasfjasfdasdfasdfasdfasfdafd‘;//支付的签名,32位签名,微信商户后台设置

    private $app_secret = ‘hsisdidfdkkdkd‘;

    private $parameters;

    private $notify_url=‘http://www.mingtern.com‘;// 本控制器下面的 notifyurl  方法的URL路径 记得格式 是 http://......    【这是回调】

    public $error = 0;

    public $orderid =  null;
    
    #选择支付方式#
    #记得填写授权目录
    public function actionWxhandle()
    {    
        $open_id = ‘hsdudf67dshd12‘;//这里是用户的open_id
        
        #支付前的数据配置
        $reannumb = $this->randomkeys(4).time().Yii::$app->user->id.$this->randomkeys(4);
        
       //这里写插入语句
        $money = $params[‘money‘];
        $conf = $this->payconfig(‘mdxz‘.$reannumb, $money*100, ‘订单支付‘, $open_id);

        if (!$conf || $conf[‘return_code‘] == ‘FAIL‘) exit("<script>alert(‘对不起,微信支付接口调用错误!" . $conf[‘return_msg‘] . "‘);history.go(-1);</script>");

        $this->orderid = $conf[‘prepay_id‘];

        //生成页面调用参数

        $jsApiObj["appId"] = $conf[‘appid‘];
        $timeStamp = time();
        $jsApiObj["timeStamp"] = "$timeStamp";
        $jsApiObj["nonceStr"] = $this->createNoncestr();
        $jsApiObj["package"] = "prepay_id=" . $conf[‘prepay_id‘];
        $jsApiObj["signType"] = "MD5";
        $jsApiObj["paySign"] = $this->MakeSign($jsApiObj);        
        return json_encode($jsApiObj);
    }

   //回调
    public function actionNotifyurl()
    {
        $xml = file_get_contents("php://input");
        $log = json_decode(json_encode(simplexml_load_string($xml, ‘SimpleXMLElement‘, LIBXML_NOCDATA)), true);
        $id = $log[‘out_trade_no‘];  //获取单号

         //这里修改状态
         exit(‘SUCCESS‘);  //打死不能去掉
    }

    #微信JS支付参数获取#
    protected function payconfig($no, $fee, $body, $open_id)
    {
        $openid = $open_id;
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $data[‘appid‘] = $this->app_id;
        $data[‘mch_id‘] = $this->mch_id; //商户号
        $data[‘device_info‘] = ‘WEB‘;
        $data[‘body‘] = $body;
        $data[‘out_trade_no‘] = $no; //订单号
        $data[‘total_fee‘] = $fee; //金额
        $data[‘spbill_create_ip‘] = $_SERVER["REMOTE_ADDR"];
        $data[‘notify_url‘] = $this->notify_url;           //通知url
        $data[‘trade_type‘] = ‘JSAPI‘;
        $data[‘openid‘] = $openid;   //获取openid
        $data[‘nonce_str‘] = $this->createNoncestr();
        $data[‘sign‘] = $this->MakeSign($data);
        
        $xml = $this->ToXml($data);

        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        
        //设置header
        curl_setopt($curl, CURLOPT_HEADER, FALSE);

        //要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_POST, TRUE); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        $tmpInfo = curl_exec($curl); // 执行操作
        curl_close($curl); // 关闭CURL会话
        $arr = $this->FromXml($tmpInfo);
        
        return $arr;
    }

    /**
     *  作用:产生随机字符串,不长于32位
     */

    public function createNoncestr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++)
        {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }

    /**
     *  作用:产生随机字符串,不长于32位
     */
    public function randomkeys($length)
    {
        $pattern = ‘1234567890123456789012345678905678901234‘;
        $key = null;

        for ($i = 0; $i < $length; $i++) 
        {
            $key .= $pattern{mt_rand(0, 30)};    //生成php随机数
        }

        return $key;
    }

    /**
     * 将xml转为array
     * @param string $xml
     * @throws WxPayException
     */
    public function FromXml($xml)
    {
        //将XML转为array
        return json_decode(json_encode(simplexml_load_string($xml, ‘SimpleXMLElement‘, LIBXML_NOCDATA)), true);
    }

    /**
     * 输出xml字符
     * @throws WxPayException
     **/
    public function ToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key => $val) 
        {
            if (is_numeric($val)) 
            {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } 
            else 
            {
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }

    /**
     * 生成签名
     * @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
     */
    protected function MakeSign($arr)
    {
        //签名步骤一:按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);

        //签名步骤二:在string后加入KEY
        $string = $string . "&key=" . $this->makesign;

        //签名步骤三:MD5加密
        $string = md5($string);

        //签名步骤四:所有字符转为大写
        $result = strtoupper($string);

        return $result;
    }

    /**
     * 格式化参数格式化成url参数
     */
    protected function ToUrlParams($arr)
    {
        $buff = "";
        
        foreach ($arr as $k => $v) 
        {
            if ($k != "sign" && $v != "" && !is_array($v))
            {
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
}

有问题的可以访问原帖地址:https://www.mingtern.com/subject/5134777/ 进行交流。

php微信公众号支付接口开发demo

标签:路径   head   yii   $_server   length   protect   encode   component   www.   

原文地址:http://www.cnblogs.com/sunsets/p/7753770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!