黑帽联盟

 找回密码
 会员注册
查看: 1417|回复: 0
打印 上一主题 下一主题

[php] 用php实现微信摇一摇周边红包

[复制链接]

852

主题

38

听众

3180

积分

管理员

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    奋斗
    昨天 13:06
  • 签到天数: 1541 天

    [LV.Master]伴坛终老

    要实现摇一摇红包功能,在网上搜了好久,都没有找到源码,没办法,只有自己写了,下面分享给大家供参考

    微信官方说明如下

    摇一摇红包说明
    功能说明
    摇一摇周边红包接口是为线下商户提供的发红包功能。用户可以在商家门店等线下场所通过摇一摇周边领取商家发放的红包,在线上转发分享无效。
    开发者可通过接口开发摇一摇红包功能,特点包括:
    1.可选择使用模板加载页或自定义Html5页面调起微信原生红包页面(详见创建红包活动中use_template字段,1为使用模板,2为使用自定义Html5页面)
    2.原生红包页面拆红包,无需通过公众号消息下发
    3.提供关注公众号能力,用户可自行选择是否关注(裂变红包分享时无效)
    4.完成页面可配置跳转链接,可跳转商户的其他自定义Html5页面
    5.同一个用户在单个红包活动中只能领取1次红包
    用户侧交互流程

    https://zb.weixin.qq.com ,进入开发者支持,申请开通摇一摇红包组件接口;
    2. 红包预下单:调用微信支付的api进行红包预下单,告知需要发放的红包金额,人数,生成红包ticket;
    3. 创建活动并录入红包信息:调用摇周边平台的api录入创建红包活动并录入信息,传入预下单时生成的红包ticket;
    4. 调用jsapi抽红包:在摇出的页面中通过调用jsapi抽红包,抽中红包的用户可以拆红包;
    5. 调用以上接口时,红包提供商户和红包发放商户公众号要求一致。

    说明:

      红包提供商户:红包预下单接口传入的参数wxappid所代表的商户
      红包发放商户:调用红包接口创建红包活动、录入红包信息、发放红包的商户公众号所以步骤应该是 ① 创建红包活动 ② 预下单 ③ 录入红包找出来了之前整理的类 在写一下1.创建活动

    接口说明
    创建红包活动,设置红包活动有效期,红包活动开关等基本信息,返回活动id
    接口调用说明

    服务器端调用

    http请求方式: POST
    URL: https://api.weixin.qq.com/shakea ... 1&logo_url=LOGO_URL
    请求参数说明

    请求示例

    Content-Type: application/json Post Body:
    {                              
    "title": "title",              
    "desc": "desc",               
    "onoff": 1,                 
    "begin_time": 1428854400,              
    "expire_time": 1428940800,              
    "sponsor_appid": "wxxxxxxxxxxxxxx",
    "total": 10,
    "jump_url": JUMP_URL,   
    "key": "keyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"              
    }
    返回数据说明

    示例

    1. {   
    2. "errcode":0,   
    3. "errmsg":"",   
    4. "lottery_id":"xxxxxxllllll",
    5. "page_id":1,
    6. }
    7. /**
    8. * 摇一摇红包 创建活动
    9. * @author jiosen
    10. */
    11. class addlotteryinfo_pub extends Wxpay_client_pub
    12. {
    13.   var $code;//code码,用以获取openid
    14.   var $openid;//用户的openid
    15.   function __construct($access_token,$logo)
    16.   {
    17.     //设置接口链接
    18.     $this->url = "https://api.weixin.qq.com/shakearound/lottery/addlotteryinfo?access_token=".$access_token."&use_template=1&logo_url=".$logo;
    19.     //设置curl超时时间
    20.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    21.   }
    22.   /**
    23.    * 生成接口参数 json
    24.    */
    25.   function createJson()
    26.   {
    27.     try
    28.     {
    29.       //检测必填参数
    30.       if($this->parameters["title"] == null)
    31.       {
    32.         throw new SDKRuntimeException("缺少抽奖活动名称title!"."
    33. ");
    34.       }elseif ($this->parameters["desc"] == null ) {
    35.         throw new SDKRuntimeException("缺少抽奖活动描述desc!"."
    36. ");
    37.       }elseif ($this->parameters["begin_time"] == null) {
    38.         throw new SDKRuntimeException("缺少活动开始时间 begin_time!"."
    39. ");
    40.       }elseif ($this->parameters["expire_time"] == null) {
    41.         throw new SDKRuntimeException("缺少活动结束时间 expire_time!"."
    42. ");
    43.       }elseif ($this->parameters["total"] == null) {
    44.         throw new SDKRuntimeException("缺少红包总数total!"."
    45. ");
    46.       }elseif ($this->parameters["jump_url"] == null) {
    47.         throw new SDKRuntimeException("缺少红包关注跳转连接jump_url!"."
    48. ");
    49.       }elseif ($this->parameters["key"] == null) {
    50.         throw new SDKRuntimeException("缺少红包key!"."
    51. ");
    52.       }
    53.       $this->parameters["title"] = urlencode($this->parameters["title"]);
    54.       $this->parameters["desc"] = urlencode($this->parameters["desc"]);
    55.       $this->parameters["onoff"] = '1';//开启活动
    56.       $this->parameters["sponsor_appid"] = WxPayConf_pub::APPID;//公众账号ID
    57.       //var_dump($this->parameters);
    58.       //echo json_encode($this->parameters);
    59.       return json_encode($this->parameters);
    60.     }catch (SDKRuntimeException $e)
    61.     {
    62.       die($e->errorMessage());
    63.     }
    64.   }
    65.   function hbpreorder()
    66.   {
    67.     $data = $this->createJson();
    68.     $result = $this->curl_post($this->url,urldecode($data));
    69.     $result = json_decode($result);
    70.     return $result;
    71.   }
    72.   function curl_post($url,$data)
    73.   {
    74.     $curl = curl_init($url);
    75.     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    76.     curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    77.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    78.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    79.     curl_setopt($curl, CURLOPT_POST, 1);//发送一个常规的Post请求
    80.     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//Post提交的数据包
    81.     $rv = curl_exec($curl);//输出内容
    82.     curl_close($curl);
    83.     return $rv;
    84.   }
    85.   /**
    86.    * 作用:生成可以获得code的url
    87.    */
    88.   function createOauthUrlForCode($redirectUrl)
    89.   {
    90.     $urlObj["appid"] = WxPayConf_pub::APPID;
    91.     $urlObj["redirect_uri"] = "$redirectUrl";
    92.     $urlObj["response_type"] = "code";
    93.     $urlObj["scope"] = "snsapi_base";
    94.     $urlObj["state"] = "STATE"."#wechat_redirect";
    95.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    96.     return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    97.   }
    98.   /**
    99.    * 作用:生成可以获得openid的url
    100.    */
    101.   function createOauthUrlForOpenid()
    102.   {
    103.     $urlObj["appid"] = WxPayConf_pub::APPID;
    104.     $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    105.     $urlObj["code"] = $this->code;
    106.     $urlObj["grant_type"] = "authorization_code";
    107.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    108.     return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    109.   }
    110.   /**
    111.    * 作用:通过curl向微信提交code,以获取openid
    112.    */
    113.   function getOpenid()
    114.   {
    115.     $url = $this->createOauthUrlForOpenid();
    116.     //初始化curl
    117.     $ch = curl_init();
    118.     //设置超时
    119.     curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    120.     curl_setopt($ch, CURLOPT_URL, $url);
    121.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    122.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    123.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    124.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    125.     //运行curl,结果以jason形式返回
    126.     $res = curl_exec($ch);
    127.     curl_close($ch);
    128.     $data = json_decode($res,true);
    129.     $this->openid = $data['openid'];
    130.     return $this->openid;
    131.   }
    132.   /**
    133.    * 作用:设置code
    134.    */
    135.   function setCode($code_)
    136.   {
    137.     $this->code = $code_;
    138.   }
    139. }
    复制代码
    要注意提交的数据是json 不是xml
    前端页面随便做一下

    php 代码
    1. $title = $_POST['title'];
    2. $file = $_FILES['img'];
    3. $tools = new Tools(); //这是一个文件上传类 随意选择一样你喜欢的上传方式
    4. $logo_url = $tools->_upload_award("poll_img", $file, time());
    5. $description = $_POST['description'];
    6. $total = $_POST['total'];
    7. $jump_url = $_POST['jump_url'];
    8. $token = getAccessToken();  //这里是我封装的一个获取 token的 方法 做了时间限制 防止超出调用次数
    9.    $Redpack = new addlotteryinfo_pub($token,SITE_URL.$logo_url);
    10.    $time = time();
    11.    $end = time()+60*24*60*60;//两个月 这里的开始和结束时间我固定了
    12.   $key = $Redpack->createNoncestr(); //key
    13.  $Redpack->setParameter('title', $title);
    14. //活动标题
    15. $Redpack->setParameter('desc', $description);
    16. //活动描述
    17. $Redpack->setParameter('begin_time', $time);
    18. //开始时间
    19. $Redpack->setParameter('expire_time', $end);
    20. //结束时间
    21. $Redpack->setParameter('total', $total);
    22. //红包总数
    23. $Redpack->setParameter('jump_url', $jump_url);
    24. //key
    25. $Redpack->setParameter('key', $key);
    26. $result = $Redpack->hbpreorder();
    27. $result = (array)$result;
    28. if($result['errcode']==0){
    29.    $lottery_id = $result['lottery_id'];
    30.   $page_id = $result['page_id'];
    31.   //这里记得存一下数据库;           
    32. }else{
    33.   //echo '创建活动失败:'.$result['errmsg'];
    34.   //这里是错误提示
    35. }   
    复制代码
    2.预下单

    接口说明
    设置单个红包的金额,类型等,生成红包信息。预下单完成后,需要在72小时内调用jsapi完成抽红包的操作。(红包过期失效后,资金会退回到商户财付通帐号。)
    接口调用说明

    服务器端调用

    http请求方式: POST

    https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder

    POST数据格式:XML

    需要商户证书
    请求参数说明

    请求示例

    <xml>   
    <sign><![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]></sign>   
    <mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>   
    <mch_id><![CDATA[10000097]]></mch_id>   
    <wxappid><![CDATA[wxcbda96de0b165486]]></wxappid>   
    <send_name><![CDATA[send_name]]></send_name>   
    <hb_type><![CDATA[NORMAL]]></hb_type>   
    <auth_mchid><![CDATA[10000098]]></auth_mchid>   
    <auth_appid><![CDATA[wx7777777]]></auth_appid>   
    <total_amount><![CDATA[200]]></total_amount>   
    <amt_type><![CDATA[ALL_RAND]]></amt_type>   
    <total_num><![CDATA[3]]></total_num>   
    <wishing><![CDATA[恭喜发财 ]]></wishing>   
    <act_name><![CDATA[ 新年红包 ]]></act_name>   
    <remark><![CDATA[新年红包 ]]></remark>   
    <risk_cntl><![CDATA[NORMAL]]></risk_cntl>   
    <nonce_str><![CDATA[50780e0cca98c8c8e814883e5caa672e]]></nonce_str>
    </xml>
    返回数据说明
    以下字段在return_code 和result_code都为SUCCESS的时候有返回

    成功示例

    <xml>
    <return_code><![CDATA[SUCCESS]]></return_code>
    <return_msg><![CDATA[发放成功.]]></return_msg>
    <result_code><![CDATA[SUCCESS]]></result_code>
    <err_code><![CDATA[0]]></err_code>
    <err_code_des><![CDATA[发放成功.]]></err_code_des>
    <mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>
    <mch_id>10010404</mch_id>
    <wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>
    <sp_ticket><![CDATA[0cca98c8c8e814883]]></sp_ticket>
    <total_amount>3</total_amount>
    <detail_id><![CDATA[001001040420141117000004888]]></detail_id>
    <send_time><![CDATA[20150101080000]]></send_time>
    </xml>

    失败示例
    1. <xml>   
    2. <return_code><![CDATA[FAIL]]></return_code>
    3. <return_msg><![CDATA[系统繁忙,请稍后再试.]]></return_msg>
    4. <result_code><![CDATA[FAIL]]></result_code>
    5. <err_code><![CDATA[268458547]]></err_code>
    6. <err_code_des><![CDATA[系统繁忙,请稍后再试.]]></err_code_des>
    7. <mch_billno><![CDATA[0010010404201411170000046542]]></mch_billno>     
    8. <mch_id>10010404</mch_id>
    9. <wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>  
    10. <total_amount>3</total_amount>
    11. </xml>
    12. /**
    13. * 摇一摇红包预下单
    14. * @author jiosen
    15. */
    16. class Yhb_pub extends Wxpay_client_pub
    17. {
    18.   var $code;//code码,用以获取openid
    19.   var $openid;//用户的openid
    20.   function __construct()
    21.   {
    22.     //设置接口链接
    23.     $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder";
    24.     //设置curl超时时间
    25.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    26.   }
    27.   /**
    28.    * 生成接口参数xml
    29.    */
    30.   function createXml()
    31.   {
    32.     try
    33.     {
    34.       //检测必填参数
    35.       if($this->parameters["mch_billno"] == null)
    36.       {
    37.         throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."
    38. ");
    39.       }elseif ($this->parameters["send_name"] == null ) {
    40.         throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."
    41. ");
    42.       }elseif ($this->parameters["total_amount"] == null) {
    43.         throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."
    44. ");
    45.       }elseif ($this->parameters["total_num"] == null) {
    46.         throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."
    47. ");
    48.       }elseif ($this->parameters["wishing"] == null) {
    49.         throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."
    50. ");
    51.       }elseif ($this->parameters["act_name"] == null) {
    52.         throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."
    53. ");
    54.       }elseif ($this->parameters["remark"] == null) {
    55.         throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."
    56. ");
    57.       }
    58.       $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
    59.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    60.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    61.       //$this->parameters["re_openid"] = $this->openid;//用户openid
    62.       $this->parameters["hb_type"] = 'NORMAL';//红包类型 NORMAL-普通红包;GROUP-裂变红包(可分享红包给好友,无关注公众号能力)。
    63.       $this->parameters["auth_mchid"] = '1000052601';//摇周边商户号
    64.       $this->parameters["auth_appid"] = 'wxbf42bd79c4391863';//摇周边 appid
    65.       $this->parameters["risk_cntl"] = 'NORMAL';//风控设置
    66.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    67.       return $this->arrayToXml($this->parameters);
    68.     }catch (SDKRuntimeException $e)
    69.     {
    70.       die($e->errorMessage());
    71.     }
    72.   }
    73.   function hbpreorder()
    74.   {
    75.     $this->postXmlSSL();
    76.     $this->result = $this->xmlToArray($this->response);
    77.     return $this->result;
    78.   }
    79.   /**
    80.    * 作用:生成可以获得code的url
    81.    */
    82.   function createOauthUrlForCode($redirectUrl)
    83.   {
    84.     $urlObj["appid"] = WxPayConf_pub::APPID;
    85.     $urlObj["redirect_uri"] = "$redirectUrl";
    86.     $urlObj["response_type"] = "code";
    87.     $urlObj["scope"] = "snsapi_base";
    88.     $urlObj["state"] = "STATE"."#wechat_redirect";
    89.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    90.     return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    91.   }
    92.   /**
    93.    * 作用:生成可以获得openid的url
    94.    */
    95.   function createOauthUrlForOpenid()
    96.   {
    97.     $urlObj["appid"] = WxPayConf_pub::APPID;
    98.     $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    99.     $urlObj["code"] = $this->code;
    100.     $urlObj["grant_type"] = "authorization_code";
    101.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    102.     return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    103.   }
    104.   /**
    105.    * 作用:通过curl向微信提交code,以获取openid
    106.    */
    107.   function getOpenid()
    108.   {
    109.     $url = $this->createOauthUrlForOpenid();
    110.     //初始化curl
    111.     $ch = curl_init();
    112.     //设置超时
    113.     curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    114.     curl_setopt($ch, CURLOPT_URL, $url);
    115.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    116.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    117.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    118.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    119.     //运行curl,结果以jason形式返回
    120.     $res = curl_exec($ch);
    121.     curl_close($ch);
    122.     //取出openid
    123.     $data = json_decode($res,true);
    124.     $this->openid = $data['openid'];
    125.     return $this->openid;
    126.   }
    127.   /**
    128.    * 作用:设置code
    129.    */
    130.   function setCode($code_)
    131.   {
    132.     $this->code = $code_;
    133.   }
    134. }
    复制代码
    这里需要注意的是  auth_mchid 和 auth_appid 要填摇周边平台给出的appid 和商户号
    调用 (这里不贴前端页面了)
    $Redpack = new \Yhb_pub();
    $Redpack->setParameter('mch_billno', WxPayConf_pub::MCHID.date('YmdHis').rand(1000, 9999));
    //商户名称
    $Redpack->setParameter('send_name', "商户名称");
    //付款金额
    $Redpack->setParameter('total_amount', 100); //单位分
    //红包发放总人数
    $Redpack->setParameter('amt_type', "ALL_RAND");
    $Redpack->setParameter('total_num', 1);
    //红包祝福语
    $Redpack->setParameter('wishing', "摇一摇送红包");
    //活动名称
    $Redpack->setParameter('act_name', "摇一摇送红包");
    //备注
    $Redpack->setParameter('remark', "摇一摇送红包 备注");
    $result = $Redpack->hbpreorder();
    if($result[''])
    3.录入红包

    接口说明
    在调用"创建红包活动"接口之后,调用此接口录入红包信息。注意,此接口每次调用,都会向某个活动新增一批红包信息,如果红包数少于100 个,请通过一次调用添加所有红包信息。如果红包数大于100,可以多次调用接口添加。请注意确保多次录入的红包ticket总的数目不大于创建该红包活动 时设置的total值。
    接口调用说明

    服务器端调用
    http请求方式: POST
    URL:https://api.weixin.qq.com/shakea ... s_token=ACCESSTOKEN
    请求参数说明


    POST BODY:JSON格式的结构体

    请求示例

    Content-Type: application/json Post Body:
    {   
    "lottery_id": "xxxxxxllllll",   
    "mchid": "10000098",   
    "sponsor_appid": "wx8888888888888888",  
    "prize_info_list": [     
       {      
      "ticket": "v1|ZiPs2l0hpMBp3uwGI1rwp45vOdz/V/zQ/00jP9MeWT+e47/q1FJjwCIP34frSjzOxAEzJ7k2CtAg1pmcShvkChBWqbThxPm6MBuzceoHtj79iHuHaEn0WAO+j4sXnXnbGswFOlDYWg1ngvrRYnCY3g=="
       },
       {
      "ticket": "v1|fOhNUTap1oepSm5ap0hx1gmATM\/QX\/xn3sZWL7K+5Z10sbV5\/mZ4SwxwxbK2SPV32eLRvjd4ww1G3H5a+ypqRrySi+4oo97y63KoEQbRCPjbkyQBY8AYVyvD40V2b9slTQCm2igGY98mPe+VxZiayQ=="
       }
      ]
    }


    返回数据说明

    示例
    1. {   
    2. "errcode":0,   
    3. "errmsg":"",   
    4. "repeat_ticket_list":[     
    5.    {      
    6. "ticket": "v1|ZiPs2l0hpMBp3uwGI1rwp45vOdz/V/zQ/00jP9MeWT+e47/q1FJjwCIP34frSjzOxAEzJ7k2CtAg1pmcShvkChBWqbThxPm6MBuzceoHtj79iHuHaEn0WAO+j4sXnXnbGswFOlDYWg1ngvrRYnCY3g=="            
    7.    },
    8.    {
    9. "ticket":"v1|ZiPs2l0zzXCsdfwe45dxCdHiukOdz/V/zQ/89xcnC5XnT+e47/q1FJjwCO4frSjzOxAEzJ7k2CtAg1pmcShvkChBWzc45dDGC32Dcxx4DGxczjDCGsdjowe9iHuaEn0WAO+GswFOlDYWg1ngvrRYnCY3g=="     }   
    10.    }
    11. ],
    12. "success_num":100
    13. }

    14. /**
    15. * 摇一摇红包 录入红包
    16. * @author jiosen
    17. */
    18. class lottery_pub extends Wxpay_client_pub
    19. {
    20.   var $code;//code码,用以获取openid
    21.   var $openid;//用户的openid
    22.   function __construct($access_token)
    23.   {
    24.     //设置接口链接
    25.     $this->url = "https://api.weixin.qq.com/shakearound/lottery/setprizebucket?access_token=".$access_token;
    26.     //设置curl超时时间
    27.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    28.   }
    29.   /**
    30.    * 生成接口参数 json
    31.    */
    32.   function createJson()
    33.   {
    34.     try
    35.     {
    36.       //检测必填参数
    37.       if($this->parameters["lottery_id"] == null)
    38.       {
    39.         throw new SDKRuntimeException("缺少抽奖活动id lottery_id !"."
    40. ");
    41.       }else if(empty($this->parameters["prize_info_list"])){
    42.         throw new SDKRuntimeException("缺少抽奖活动红包 prize_info_list !"."
    43. ");
    44.       }
    45.       $this->parameters["mchid"] = WxPayConf_pub::MCHID;//授权商户号
    46.       $this->parameters["sponsor_appid"] = WxPayConf_pub::APPID;//授权上号appid
    47.       return json_encode($this->parameters);
    48.       //echo json_encode($this->parameters);die;
    49.     }catch (SDKRuntimeException $e)
    50.     {
    51.       die($e->errorMessage());
    52.     }
    53.   }
    54.   function setJsonArray($parameter, $parameterValue){
    55.     $this->parameters[$this->trimString($parameter)] = $parameterValue;
    56.   }
    57.   function hbpreorder()
    58.   {
    59.     $data = $this->createJson();
    60.     $result = $this->curl_post($this->url,$data);
    61.     $result = json_decode($result);
    62.     return $result;
    63.   }
    64.   function curl_post($url,$data)
    65.   {
    66.     $curl = curl_init($url);
    67.     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    68.     curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    69.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    70.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    71.     curl_setopt($curl, CURLOPT_POST, 1);//发送一个常规的Post请求
    72.     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//Post提交的数据包
    73.     $rv = curl_exec($curl);//输出内容
    74.     curl_close($curl);
    75.     return $rv;
    76.   }
    77.   /**
    78.    * 作用:生成可以获得code的url
    79.    */
    80.   function createOauthUrlForCode($redirectUrl)
    81.   {
    82.     $urlObj["appid"] = WxPayConf_pub::APPID;
    83.     $urlObj["redirect_uri"] = "$redirectUrl";
    84.     $urlObj["response_type"] = "code";
    85.     $urlObj["scope"] = "snsapi_base";
    86.     $urlObj["state"] = "STATE"."#wechat_redirect";
    87.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    88.     return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    89.   }
    90.   /**
    91.    * 作用:生成可以获得openid的url
    92.    */
    93.   function createOauthUrlForOpenid()
    94.   {
    95.     $urlObj["appid"] = WxPayConf_pub::APPID;
    96.     $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    97.     $urlObj["code"] = $this->code;
    98.     $urlObj["grant_type"] = "authorization_code";
    99.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    100.     return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    101.   }
    102.   /**
    103.    * 作用:通过curl向微信提交code,以获取openid
    104.    */
    105.   function getOpenid()
    106.   {
    107.     $url = $this->createOauthUrlForOpenid();
    108.     //初始化curl
    109.     $ch = curl_init();
    110.     //设置超时
    111.     curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    112.     curl_setopt($ch, CURLOPT_URL, $url);
    113.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    114.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    115.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    116.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    117.     //运行curl,结果以jason形式返回
    118.     $res = curl_exec($ch);
    119.     curl_close($ch);
    120.     //取出openid
    121.     $data = json_decode($res,true);
    122.     $this->openid = $data['openid'];
    123.     return $this->openid;
    124.   }
    125.   /**
    126.    * 作用:设置code
    127.    */
    128.   function setCode($code_)
    129.   {
    130.     $this->code = $code_;
    131.   }
    132. }
    133. 调用


    134.     $token = getAccessToken();
    135.     $Redpack = new \lottery_pub($token);
    136.     $lottery_id = ''; //这里读取数据库取出创建活动时返回的 lottery_id
    137. $Redpack->setParameter('lottery_id', $lottery_id);
    138.   //活动id
    139.   $prize_info_list =array(array('ticket'=>'这里取出预下单返回的sp_ticket'));
    140.   $Redpack->setJsonArray('prize_info_list', $prize_info_list);
    141.   //提交
    142.   $Redpack->hbpreorder();
    143. 抢红包页面  php

    144. function getshakeinfo($access_token,$ticket){
    145.     $getshakeinfourl='https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token='.$access_token;
    146.      $jo=0;
    147.      if($access_token){
    148.       $data=array('ticket' =>$ticket);
    149.       $rd=$this->curl_post($getshakeinfourl,json_encode($data));
    150.       $jo=json_decode($rd);
    151.      }else{
    152.       echo 'access_token null';
    153.      }
    154.      return $jo;
    155.   }
    156.     $ticket=$_GET['ticket'];//获叏设备信息,包括 U UID 、 major 、 minor ,以及距离、 openID 等信息
    157.     $token = getAccessToken();
    158.     $shake=getshakeinfo($token,$ticket);
    159.     $openid=$shake->data->openid;
    160.     $jsapi = new Common_util_pub();
    161.     $noncestr = $jsapi->createNoncestr();
    162.     $parameters = array(
    163.         'lottery_id' =>'创建活动时候返回的活动ID',
    164.         'noncestr'=>$noncestr,
    165.         'openid'=>$openid,
    166.       );
    167.     $signStr = $jsapi->formatBizQueryParaMap($parameters,false);
    168.     $key = '创建活动时候的key';
    169.     $signStr=$signStr."&key=".$key;
    170.     $sign = strtoupper(md5($signStr));
    171. 上一步返回的参数填在抢红包html页面

    172. <script type="text/javascript" src="http://zb.weixin.qq.com/app/shakehb/BeaconShakehbJsBridge.js">
    173. </script>
    174. <script type="text/javascript">
    175.   BeaconShakehbJsBridge.ready(function(){
    176.     //alert();
    177.     BeaconShakehbJsBridge.invoke('jumpHongbao',{lottery_id:"{$lottery_id}",noncestr:"{$noncestr}",openid:"{$openid}",sign:"{$sign}"});
    178.     });
    179. </script>
    复制代码
    红包绑定用户事件通知     
    接口说明
    用户进入红包页面时,后台会将一个红包ticket和用户openid绑定,微信会把这个事件推送到开发者填写的URL(登录公众平台进入开发者中心设置)。推送内容包含用户openid,红包活动id,红包ticket、金额以及红包绑定时间。
    注:红包绑定用户不等同于用户领取红包。用户进入红包页面后,有可能不拆红包,但该红包ticket已被绑定,不能再被其他用户绑定,过期后会退回商户财付通账户。
    推送XML数据包示例

    <xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>1442824314</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[ShakearoundLotteryBind]]></Event>
    <LotteryId><![CDATA[lotteryid]]></LotteryId>
    <Ticket><![CDATA[ticket]]></Ticket>
    <Money>88</Money>
    <BindTime>1442824313</BindTime>
    </xml>
    添加事件处理即可
    1. /**
    2.    * 事件处理
    3.    * @param unknown $object
    4.    * @return string
    5.    */
    6.   public function handleEvent($object) {
    7.     // Event是事件类型(subscribe,LOCATION)
    8.     $oneEvent = $object->Event;
    9.     // EventKey是菜单事件的key值
    10.     $key = $object->EventKey;
    11.     // 关注事件
    12.     if ($oneEvent == "subscribe" || $oneEvent == "SCAN") {
    13.       if(!empty($object->Ticket)) {
    14.         //扫码事件
    15.         ....
    16.       } else {
    17.         //关注事件
    18.         ....
    19.       }
    20.     }else if($oneEvent=="ShakearoundLotteryBind"){
    21.       //添加到数据库
    22.     }else if.......其他的事件......
    23.   }
    24. 完毕了.时间比较匆忙 也没时间做优化 大神经过顺便指导12  我好搓的英文基础
    25. 下面贴上完整WxPayPubHelper 集成了所有支付类 配置可用
    26. <?php
    27. /**
    28. * 微信支付帮助库
    29. * ====================================================
    30. * 接口分三种类型:
    31. * 【请求型接口】--Wxpay_client_
    32. *   统一支付接口类--UnifiedOrder
    33. *   订单查询接口--OrderQuery
    34. *   退款申请接口--Refund
    35. *   退款查询接口--RefundQuery
    36. *   对账单接口--DownloadBill
    37. *   短链接转换接口--ShortUrl
    38. * 【响应型接口】--Wxpay_server_
    39. *   通用通知接口--Notify
    40. *   Native支付——请求商家获取商品信息接口--NativeCall
    41. * 【其他】
    42. *   静态链接二维码--NativeLink
    43. *   JSAPI支付--JsApi
    44. * =====================================================
    45. * 【CommonUtil】常用工具:
    46. *   trimString(),设置参数时需要用到的字符处理函数
    47. *   createNoncestr(),产生随机字符串,不长于32位
    48. *   formatBizQueryParaMap(),格式化参数,签名过程需要用到
    49. *   getSign(),生成签名
    50. *   arrayToXml(),array转xml
    51. *   xmlToArray(),xml转 array
    52. *   postXmlCurl(),以post方式提交xml到对应的接口url
    53. *   postXmlSSLCurl(),使用证书,以post方式提交xml到对应的接口url
    54. */
    55.   include_once("SDKRuntimeException.php");
    56.   include_once("WxPay.pub.config.php");
    57. /**
    58. * 所有接口的基类
    59. */
    60. class Common_util_pub
    61. {
    62.   function __construct() {
    63.   }
    64.   function trimString($value)
    65.   {
    66.     $ret = null;
    67.     if (null != $value)
    68.     {
    69.       $ret = $value;
    70.       if (strlen($ret) == 0)
    71.       {
    72.         $ret = null;
    73.       }
    74.     }
    75.     return $ret;
    76.   }
    77.   /**
    78.    * 作用:产生随机字符串,不长于32位
    79.    */
    80.   public function createNoncestr( $length = 32 )
    81.   {
    82.     $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    83.     $str ="";
    84.     for ( $i = 0; $i < $length; $i++ ) {
    85.       $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
    86.     }
    87.     return $str;
    88.   }
    89.   /**
    90.    * 作用:格式化参数,签名过程需要使用
    91.    */
    92.   function formatBizQueryParaMap($paraMap, $urlencode)
    93.   {
    94.     $buff = "";
    95.     ksort($paraMap);
    96.     foreach ($paraMap as $k => $v)
    97.     {
    98.       if($urlencode)
    99.       {
    100.         $v = urlencode($v);
    101.       }
    102.       //$buff .= strtolower($k) . "=" . $v . "&";
    103.       $buff .= $k . "=" . $v . "&";
    104.     }
    105.     $reqPar;
    106.     if (strlen($buff) > 0)
    107.     {
    108.       $reqPar = substr($buff, 0, strlen($buff)-1);
    109.     }
    110.     return $reqPar;
    111.   }
    112.   /**
    113.    * 作用:生成签名
    114.    */
    115.   public function getSign($Obj)
    116.   {
    117.     foreach ($Obj as $k => $v)
    118.     {
    119.       $Parameters[$k] = $v;
    120.     }
    121.     //签名步骤一:按字典序排序参数
    122.     ksort($Parameters);
    123.     $String = $this->formatBizQueryParaMap($Parameters, false);
    124.     //echo '【string1】'.$String.'</br>';
    125.     //签名步骤二:在string后加入KEY
    126.     $String = $String."&key=".WxPayConf_pub::KEY;
    127.     //echo "【string2】".$String."</br>";
    128.     //签名步骤三:MD5加密
    129.     $String = md5($String);
    130.     //echo "【string3】 ".$String."</br>";
    131.     //签名步骤四:所有字符转为大写
    132.     $result_ = strtoupper($String);
    133.     //echo "【result】 ".$result_."</br>";
    134.     return $result_;
    135.   }
    136.   /**
    137.    * 作用:array转xml
    138.    */
    139.   function arrayToXml($arr)
    140.   {
    141.     $xml = "<xml>";
    142.     foreach ($arr as $key=>$val)
    143.     {
    144.        if (is_numeric($val))
    145.        {
    146.         $xml.="<".$key.">".$val."</".$key.">";
    147.        }
    148.        else
    149.         $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
    150.     }
    151.     $xml.="</xml>";
    152.     return $xml;
    153.   }
    154.   /**
    155.    * 作用:将xml转为array
    156.    */
    157.   public function xmlToArray($xml)
    158.   {   
    159.     //将XML转为array   
    160.     $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);   
    161.     return $array_data;
    162.   }
    163.   /**
    164.    * 作用:以post方式提交xml到对应的接口url
    165.    */
    166.   public function postXmlCurl($xml,$url,$second=30)
    167.   {   
    168.     //初始化curl   
    169.     $ch = curl_init();
    170.     //设置超时
    171.     curl_setopt($ch, CURLOP_TIMEOUT, $second);
    172.     //这里设置代理,如果有的话
    173.     //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
    174.     //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
    175.     curl_setopt($ch,CURLOPT_URL, $url);
    176.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    177.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    178.     //设置header
    179.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    180.     //要求结果为字符串且输出到屏幕上
    181.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    182.     //post提交方式
    183.     curl_setopt($ch, CURLOPT_POST, TRUE);
    184.     curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    185.     //运行curl
    186.     $data = curl_exec($ch);
    187.     curl_close($ch);
    188.     //返回结果
    189.     if($data)
    190.     {
    191.       curl_close($ch);
    192.       return $data;
    193.     }
    194.     else
    195.     {
    196.       $error = curl_errno($ch);
    197.       echo "curl出错,错误码:$error"."
    198. ";
    199.       echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
    200.       curl_close($ch);
    201.       return false;
    202.     }
    203.   }
    204.   /**
    205.    * 作用:使用证书,以post方式提交xml到对应的接口url
    206.    */
    207.   function postXmlSSLCurl($xml,$url,$second=30)
    208.   {
    209.     $ch = curl_init();
    210.     //超时时间
    211.     curl_setopt($ch,CURLOPT_TIMEOUT,$second);
    212.     //这里设置代理,如果有的话
    213.     //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
    214.     //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
    215.     curl_setopt($ch,CURLOPT_URL, $url);
    216.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    217.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    218.     //设置header
    219.     curl_setopt($ch,CURLOPT_HEADER,FALSE);
    220.     //要求结果为字符串且输出到屏幕上
    221.     curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    222.     //设置证书
    223.     //使用证书:cert 与 key 分别属于两个.pem文件
    224.     //默认格式为PEM,可以注释
    225. //   curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
    226. //   curl_setopt($ch,CURLOPT_SSLCERT,WxPayConf_pub::SSLCERT_PATH );
    227. //   //默认格式为PEM,可以注释
    228. //   curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
    229. //   curl_setopt($ch,CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH);
    230.     curl_setopt($ch, CURLOPT_SSLCERT,WxPayConf_pub::SSLCERT_PATH);
    231.     curl_setopt($ch, CURLOPT_SSLKEY,WxPayConf_pub::SSLKEY_PATH);
    232.     curl_setopt($ch, CURLOPT_CAINFO, WxPayConf_pub::SSLCA_PATH); // CA根证书(用来验证的网站证书是否是CA颁布)
    233.     //post提交方式
    234.     curl_setopt($ch,CURLOPT_POST, true);
    235.     curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
    236.     $data = curl_exec($ch);
    237.     //返回结果
    238.     if($data){
    239.       curl_close($ch);
    240.       return $data;
    241.     }
    242.     else {
    243.       $error = curl_errno($ch);
    244.       echo "curl出错,错误码:$error"."
    245. ";
    246.       echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
    247.       curl_close($ch);
    248.       return false;
    249.     }
    250.   }
    251.   /**
    252.    * 作用:打印数组
    253.    */
    254.   function printErr($wording='',$err='')
    255.   {
    256.     print_r('<pre>');
    257.     echo $wording."</br>";
    258.     var_dump($err);
    259.     print_r('</pre>');
    260.   }
    261. }
    262. /**
    263. * 请求型接口的基类
    264. */
    265. class Wxpay_client_pub extends Common_util_pub
    266. {
    267.   var $parameters;//请求参数,类型为关联数组
    268.   public $response;//微信返回的响应
    269.   public $result;//返回参数,类型为关联数组
    270.   var $url;//接口链接
    271.   var $curl_timeout;//curl超时时间
    272.   /**
    273.    * 作用:设置请求参数
    274.    */
    275.   function setParameter($parameter, $parameterValue)
    276.   {
    277.     $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
    278.   }
    279.   /**
    280.    * 作用:设置标配的请求参数,生成签名,生成接口参数xml
    281.    */
    282.   function createXml()
    283.   {
    284.     $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    285.     $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    286.     $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    287.     $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    288.     return $this->arrayToXml($this->parameters);
    289.   }
    290.   /**
    291.    * 作用:post请求xml
    292.    */
    293.   function postXml()
    294.   {
    295.     $xml = $this->createXml();
    296.     $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout);
    297.     return $this->response;
    298.   }
    299.   /**
    300.    * 作用:使用证书post请求xml
    301.    */
    302.   function postXmlSSL()
    303.   {  
    304.     $xml = $this->createXml();
    305.     $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout);
    306.     return $this->response;
    307.   }
    308.   /**
    309.    * 作用:获取结果,默认不使用证书
    310.    */
    311.   function getResult()
    312.   {   
    313.     $this->postXml();
    314.     $this->result = $this->xmlToArray($this->response);
    315.     return $this->result;
    316.   }
    317. }
    318. /**
    319. * 统一支付接口类
    320. */
    321. class UnifiedOrder_pub extends Wxpay_client_pub
    322. {  
    323.   function __construct()
    324.   {
    325.     //设置接口链接
    326.     $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    327.     //设置curl超时时间
    328.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    329.   }
    330.   /**
    331.    * 生成接口参数xml
    332.    */
    333.   function createXml()
    334.   {
    335.     try
    336.     {
    337.       //检测必填参数
    338.       if($this->parameters["out_trade_no"] == null)
    339.       {
    340.         throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no!"."
    341. ");
    342.       }elseif($this->parameters["body"] == null){
    343.         throw new SDKRuntimeException("缺少统一支付接口必填参数body!"."
    344. ");
    345.       }elseif ($this->parameters["total_fee"] == null ) {
    346.         throw new SDKRuntimeException("缺少统一支付接口必填参数total_fee!"."
    347. ");
    348.       }elseif ($this->parameters["notify_url"] == null) {
    349.         throw new SDKRuntimeException("缺少统一支付接口必填参数notify_url!"."
    350. ");
    351.       }elseif ($this->parameters["trade_type"] == null) {
    352.         throw new SDKRuntimeException("缺少统一支付接口必填参数trade_type!"."
    353. ");
    354.       }elseif ($this->parameters["trade_type"] == "JSAPI" &&
    355.         $this->parameters["openid"] == NULL){
    356.         throw new SDKRuntimeException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"."
    357. ");
    358.       }
    359.       $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    360.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    361.       $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip   
    362.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    363.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    364.       return $this->arrayToXml($this->parameters);
    365.     }catch (SDKRuntimeException $e)
    366.     {
    367.       die($e->errorMessage());
    368.     }
    369.   }
    370.   /**
    371.    * 获取prepay_id
    372.    */
    373.   function getPrepayId()
    374.   {
    375.     $this->postXml();
    376.     $this->result = $this->xmlToArray($this->response);
    377.     $prepay_id = $this->result["prepay_id"];
    378.     return $prepay_id;
    379.   }
    380. }
    381. /**
    382. * 订单查询接口
    383. */
    384. class OrderQuery_pub extends Wxpay_client_pub
    385. {
    386.   function __construct()
    387.   {
    388.     //设置接口链接
    389.     $this->url = "https://api.mch.weixin.qq.com/pay/orderquery";
    390.     //设置curl超时时间
    391.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;   
    392.   }
    393.   /**
    394.    * 生成接口参数xml
    395.    */
    396.   function createXml()
    397.   {
    398.     try
    399.     {
    400.       //检测必填参数
    401.       if($this->parameters["out_trade_no"] == null &&
    402.         $this->parameters["transaction_id"] == null)
    403.       {
    404.         throw new SDKRuntimeException("订单查询接口中,out_trade_no、transaction_id至少填一个!"."
    405. ");
    406.       }
    407.       $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    408.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    409.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    410.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    411.       return $this->arrayToXml($this->parameters);
    412.     }catch (SDKRuntimeException $e)
    413.     {
    414.       die($e->errorMessage());
    415.     }
    416.   }
    417. }
    418. /**
    419. * 退款申请接口
    420. */
    421. class Refund_pub extends Wxpay_client_pub
    422. {
    423.   function __construct() {
    424.     //设置接口链接
    425.     $this->url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
    426.     //设置curl超时时间
    427.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;   
    428.   }
    429.   /**
    430.    * 生成接口参数xml
    431.    */
    432.   function createXml()
    433.   {
    434.     try
    435.     {
    436.       //检测必填参数
    437.       if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) {
    438.         throw new SDKRuntimeException("退款申请接口中,out_trade_no、transaction_id至少填一个!"."
    439. ");
    440.       }elseif($this->parameters["out_refund_no"] == null){
    441.         throw new SDKRuntimeException("退款申请接口中,缺少必填参数out_refund_no!"."
    442. ");
    443.       }elseif($this->parameters["total_fee"] == null){
    444.         throw new SDKRuntimeException("退款申请接口中,缺少必填参数total_fee!"."
    445. ");
    446.       }elseif($this->parameters["refund_fee"] == null){
    447.         throw new SDKRuntimeException("退款申请接口中,缺少必填参数refund_fee!"."
    448. ");
    449.       }elseif($this->parameters["op_user_id"] == null){
    450.         throw new SDKRuntimeException("退款申请接口中,缺少必填参数op_user_id!"."
    451. ");
    452.       }
    453.       $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    454.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    455.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    456.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    457.       return $this->arrayToXml($this->parameters);
    458.     }catch (SDKRuntimeException $e)
    459.     {
    460.       die($e->errorMessage());
    461.     }
    462.   }
    463.   /**
    464.    * 作用:获取结果,使用证书通信
    465.    */
    466.   function getResult()
    467.   {   
    468.     $this->postXmlSSL();
    469.     $this->result = $this->xmlToArray($this->response);
    470.     return $this->result;
    471.   }
    472. }
    473. /**
    474. * 退款查询接口
    475. */
    476. class RefundQuery_pub extends Wxpay_client_pub
    477. {
    478.   function __construct() {
    479.     //设置接口链接
    480.     $this->url = "https://api.mch.weixin.qq.com/pay/refundquery";
    481.     //设置curl超时时间
    482.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;   
    483.   }
    484.   /**
    485.    * 生成接口参数xml
    486.    */
    487.   function createXml()
    488.   {   
    489.     try
    490.     {
    491.       if($this->parameters["out_refund_no"] == null &&
    492.         $this->parameters["out_trade_no"] == null &&
    493.         $this->parameters["transaction_id"] == null &&
    494.         $this->parameters["refund_id "] == null)
    495.       {
    496.         throw new SDKRuntimeException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"."
    497. ");
    498.       }
    499.       $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    500.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    501.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    502.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    503.       return $this->arrayToXml($this->parameters);
    504.     }catch (SDKRuntimeException $e)
    505.     {
    506.       die($e->errorMessage());
    507.     }
    508.   }
    509.   /**
    510.    * 作用:获取结果,使用证书通信
    511.    */
    512.   function getResult()
    513.   {   
    514.     $this->postXmlSSL();
    515.     $this->result = $this->xmlToArray($this->response);
    516.     return $this->result;
    517.   }
    518. }
    519. /**
    520. * 对账单接口
    521. */
    522. class DownloadBill_pub extends Wxpay_client_pub
    523. {
    524.   function __construct()
    525.   {
    526.     //设置接口链接
    527.     $this->url = "https://api.mch.weixin.qq.com/pay/downloadbill";
    528.     //设置curl超时时间
    529.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;   
    530.   }
    531.   /**
    532.    * 生成接口参数xml
    533.    */
    534.   function createXml()
    535.   {   
    536.     try
    537.     {
    538.       if($this->parameters["bill_date"] == null )
    539.       {
    540.         throw new SDKRuntimeException("对账单接口中,缺少必填参数bill_date!"."
    541. ");
    542.       }
    543.       $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    544.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    545.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    546.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    547.       return $this->arrayToXml($this->parameters);
    548.     }catch (SDKRuntimeException $e)
    549.     {
    550.       die($e->errorMessage());
    551.     }
    552.   }
    553.   /**
    554.    * 作用:获取结果,默认不使用证书
    555.    */
    556.   function getResult()
    557.   {   
    558.     $this->postXml();
    559.     $this->result = $this->xmlToArray($this->result_xml);
    560.     return $this->result;
    561.   }
    562. }
    563. /**
    564. * 短链接转换接口
    565. */
    566. class ShortUrl_pub extends Wxpay_client_pub
    567. {
    568.   function __construct()
    569.   {
    570.     //设置接口链接
    571.     $this->url = "https://api.mch.weixin.qq.com/tools/shorturl";
    572.     //设置curl超时时间
    573.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;   
    574.   }
    575.   /**
    576.    * 生成接口参数xml
    577.    */
    578.   function createXml()
    579.   {   
    580.     try
    581.     {
    582.       if($this->parameters["long_url"] == null )
    583.       {
    584.         throw new SDKRuntimeException("短链接转换接口中,缺少必填参数long_url!"."
    585. ");
    586.       }
    587.       $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    588.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    589.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    590.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    591.       return $this->arrayToXml($this->parameters);
    592.     }catch (SDKRuntimeException $e)
    593.     {
    594.       die($e->errorMessage());
    595.     }
    596.   }
    597.   /**
    598.    * 获取prepay_id
    599.    */
    600.   function getShortUrl()
    601.   {
    602.     $this->postXml();
    603.     $prepay_id = $this->result["short_url"];
    604.     return $prepay_id;
    605.   }
    606. }
    607. /**
    608. * 响应型接口基类
    609. */
    610. class Wxpay_server_pub extends Common_util_pub
    611. {
    612.   public $data;//接收到的数据,类型为关联数组
    613.   var $returnParameters;//返回参数,类型为关联数组
    614.   /**
    615.    * 将微信的请求xml转换成关联数组,以方便数据处理
    616.    */
    617.   function saveData($xml)
    618.   {
    619.     $this->data = $this->xmlToArray($xml);
    620.   }
    621.   function checkSign()
    622.   {
    623.     $tmpData = $this->data;
    624.     unset($tmpData['sign']);
    625.     $sign = $this->getSign($tmpData);//本地签名
    626.     if ($this->data['sign'] == $sign) {
    627.       return TRUE;
    628.     }
    629.     return FALSE;
    630.   }
    631.   /**
    632.    * 获取微信的请求数据
    633.    */
    634.   function getData()
    635.   {   
    636.     return $this->data;
    637.   }
    638.   /**
    639.    * 设置返回微信的xml数据
    640.    */
    641.   function setReturnParameter($parameter, $parameterValue)
    642.   {
    643.     $this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
    644.   }
    645.   /**
    646.    * 生成接口参数xml
    647.    */
    648.   function createXml()
    649.   {
    650.     return $this->arrayToXml($this->returnParameters);
    651.   }
    652.   /**
    653.    * 将xml数据返回微信
    654.    */
    655.   function returnXml()
    656.   {
    657.     $returnXml = $this->createXml();
    658.     return $returnXml;
    659.   }
    660. }
    661. /**
    662. * 通用通知接口
    663. */
    664. class Notify_pub extends Wxpay_server_pub
    665. {
    666. }
    667. /**
    668. * 请求商家获取商品信息接口
    669. */
    670. class NativeCall_pub extends Wxpay_server_pub
    671. {
    672.   /**
    673.    * 生成接口参数xml
    674.    */
    675.   function createXml()
    676.   {
    677.     if($this->returnParameters["return_code"] == "SUCCESS"){
    678.       $this->returnParameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    679.       $this->returnParameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    680.       $this->returnParameters["nonce_str"] = $this->createNoncestr();//随机字符串
    681.       $this->returnParameters["sign"] = $this->getSign($this->returnParameters);//签名
    682.     }
    683.     return $this->arrayToXml($this->returnParameters);
    684.   }
    685.   /**
    686.    * 获取product_id
    687.    */
    688.   function getProductId()
    689.   {
    690.     $product_id = $this->data["product_id"];
    691.     return $product_id;
    692.   }
    693. }
    694. /**
    695. * 静态链接二维码
    696. */
    697. class NativeLink_pub extends Common_util_pub
    698. {
    699.   var $parameters;//静态链接参数
    700.   var $url;//静态链接
    701.   function __construct()
    702.   {
    703.   }
    704.   /**
    705.    * 设置参数
    706.    */
    707.   function setParameter($parameter, $parameterValue)
    708.   {
    709.     $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
    710.   }
    711.   /**
    712.    * 生成Native支付链接二维码
    713.    */
    714.   function createLink()
    715.   {
    716.     try
    717.     {   
    718.       if($this->parameters["product_id"] == null)
    719.       {
    720.         throw new SDKRuntimeException("缺少Native支付二维码链接必填参数product_id!"."
    721. ");
    722.       }      
    723.       $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
    724.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    725.       $time_stamp = time();
    726.       $this->parameters["time_stamp"] = "$time_stamp";//时间戳
    727.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    728.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名     
    729.       $bizString = $this->formatBizQueryParaMap($this->parameters, false);
    730.       $this->url = "weixin://wxpay/bizpayurl?".$bizString;
    731.     }catch (SDKRuntimeException $e)
    732.     {
    733.       die($e->errorMessage());
    734.     }
    735.   }
    736.   /**
    737.    * 返回链接
    738.    */
    739.   function getUrl()
    740.   {   
    741.     $this->createLink();
    742.     return $this->url;
    743.   }
    744. }
    745. /**
    746. * JSAPI支付——H5网页端调起支付接口
    747. */
    748. class JsApi_pub extends Common_util_pub
    749. {
    750.   var $code;//code码,用以获取openid
    751.   var $openid;//用户的openid
    752.   var $parameters;//jsapi参数,格式为json
    753.   var $prepay_id;//使用统一支付接口得到的预支付id
    754.   var $curl_timeout;//curl超时时间
    755.   function __construct()
    756.   {
    757.     //设置curl超时时间
    758.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    759.   }
    760.   /**
    761.    * 作用:生成可以获得code的url
    762.    */
    763.   function createOauthUrlForCode($redirectUrl)
    764.   {
    765.     $urlObj["appid"] = WxPayConf_pub::APPID;
    766.     $urlObj["redirect_uri"] = "$redirectUrl";
    767.     $urlObj["response_type"] = "code";
    768.     $urlObj["scope"] = "snsapi_base";
    769.     $urlObj["state"] = "STATE"."#wechat_redirect";
    770.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    771.     return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    772.   }
    773.   /**
    774.    * 作用:生成可以获得openid的url
    775.    */
    776.   function createOauthUrlForOpenid()
    777.   {
    778.     $urlObj["appid"] = WxPayConf_pub::APPID;
    779.     $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    780.     $urlObj["code"] = $this->code;
    781.     $urlObj["grant_type"] = "authorization_code";
    782.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    783.     return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    784.   }
    785.   /**
    786.    * 作用:通过curl向微信提交code,以获取openid
    787.    */
    788.   function getOpenid()
    789.   {
    790.     $url = $this->createOauthUrlForOpenid();
    791.     //初始化curl
    792.     $ch = curl_init();
    793.     //设置超时
    794.     curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    795.     curl_setopt($ch, CURLOPT_URL, $url);
    796.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    797.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    798.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    799.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    800.     //运行curl,结果以jason形式返回
    801.     $res = curl_exec($ch);
    802.     curl_close($ch);
    803.     //取出openid
    804.     $data = json_decode($res,true);
    805.     $this->openid = $data['openid'];
    806.     return $this->openid;
    807.   }
    808.   /**
    809.    * 作用:设置prepay_id
    810.    */
    811.   function setPrepayId($prepayId)
    812.   {
    813.     $this->prepay_id = $prepayId;
    814.   }
    815.   /**
    816.    * 作用:设置code
    817.    */
    818.   function setCode($code_)
    819.   {
    820.     $this->code = $code_;
    821.   }
    822.   /**
    823.    * 作用:设置jsapi的参数
    824.    */
    825.   public function getParameters()
    826.   {
    827.     $jsApiObj["appId"] = WxPayConf_pub::APPID;
    828.     $timeStamp = time();
    829.     $jsApiObj["timeStamp"] = "$timeStamp";
    830.     $jsApiObj["nonceStr"] = $this->createNoncestr();
    831.     $jsApiObj["package"] = "prepay_id=$this->prepay_id";
    832.     $jsApiObj["signType"] = "MD5";
    833.     $jsApiObj["paySign"] = $this->getSign($jsApiObj);
    834.     $this->parameters = json_encode($jsApiObj);
    835.     return $this->parameters;
    836.   }
    837. }
    838. /**
    839. * 现金红包接口
    840. * @author gaoyl101
    841. */
    842. class Redpack_pub extends Wxpay_client_pub
    843. {
    844.   var $code;//code码,用以获取openid
    845.   var $openid;//用户的openid
    846.   function __construct()
    847.   {
    848.     //设置接口链接
    849.     $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
    850.     //设置curl超时时间
    851.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    852.   }
    853.   /**
    854.    * 生成接口参数xml
    855.    */
    856.   function createXml()
    857.   {
    858.     try
    859.     {
    860.       //检测必填参数
    861.       if($this->parameters["mch_billno"] == null)
    862.       {
    863.         throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."
    864. ");
    865.       }elseif($this->parameters["nick_name"] == null){
    866.         throw new SDKRuntimeException("缺少发红包接口必填参数nick_name!"."
    867. ");
    868.       }elseif ($this->parameters["send_name"] == null ) {
    869.         throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."
    870. ");
    871.       }elseif ($this->parameters["total_amount"] == null) {
    872.         throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."
    873. ");
    874.       }elseif($this->parameters["min_value"] == null){
    875.         throw new SDKRuntimeException("缺少发红包接口必填参数min_value!"."
    876. ");
    877.       }elseif ($this->parameters["max_value"] == null ) {
    878.         throw new SDKRuntimeException("缺少发红包接口必填参数max_value!"."
    879. ");
    880.       }elseif ($this->parameters["total_num"] == null) {
    881.         throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."
    882. ");
    883.       }elseif ($this->parameters["wishing"] == null) {
    884.         throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."
    885. ");
    886.       }elseif ($this->parameters["act_name"] == null) {
    887.         throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."
    888. ");
    889.       }elseif ($this->parameters["remark"] == null) {
    890.         throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."
    891. ");
    892.       }
    893.       $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
    894.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    895.       $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
    896.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    897.       $this->parameters["re_openid"] = $this->parameters["re_openid"];
    898.       //$this->openid;//用户openid
    899.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    900.       return $this->arrayToXml($this->parameters);
    901.     }catch (SDKRuntimeException $e)
    902.     {
    903.       die($e->errorMessage());
    904.     }
    905.   }
    906.   function sendRedpack()
    907.   {
    908.     $this->postXmlSSL();
    909.     $this->result = $this->xmlToArray($this->response);
    910.     return $this->result;
    911.   }
    912.   /**
    913.    * 作用:生成可以获得code的url
    914.    */
    915.   function createOauthUrlForCode($redirectUrl)
    916.   {
    917.     $urlObj["appid"] = WxPayConf_pub::APPID;
    918.     $urlObj["redirect_uri"] = "$redirectUrl";
    919.     $urlObj["response_type"] = "code";
    920.     $urlObj["scope"] = "snsapi_base";
    921.     $urlObj["state"] = "STATE"."#wechat_redirect";
    922.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    923.     return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    924.   }
    925.   /**
    926.    * 作用:生成可以获得openid的url
    927.    */
    928.   function createOauthUrlForOpenid()
    929.   {
    930.     $urlObj["appid"] = WxPayConf_pub::APPID;
    931.     $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    932.     $urlObj["code"] = $this->code;
    933.     $urlObj["grant_type"] = "authorization_code";
    934.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    935.     return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    936.   }
    937.   /**
    938.    * 作用:通过curl向微信提交code,以获取openid
    939.    */
    940.   function getOpenid()
    941.   {
    942.     $url = $this->createOauthUrlForOpenid();
    943.     //初始化curl
    944.     $ch = curl_init();
    945.     //设置超时
    946.     curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    947.     curl_setopt($ch, CURLOPT_URL, $url);
    948.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    949.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    950.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    951.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    952.     //运行curl,结果以jason形式返回
    953.     $res = curl_exec($ch);
    954.     curl_close($ch);
    955.     //取出openid
    956.     $data = json_decode($res,true);
    957.     $this->openid = $data['openid'];
    958.     return $this->openid;
    959.   }
    960.   /**
    961.    * 作用:设置code
    962.    */
    963.   function setCode($code_)
    964.   {
    965.     $this->code = $code_;
    966.   }
    967. }
    968. /**
    969. * 红包支付接口
    970. * @author gaoyl101
    971. */
    972. class Groupredpack_pub extends Wxpay_client_pub
    973. {
    974.   var $code;//code码,用以获取openid
    975.   var $openid;//用户的openid
    976.   function __construct()
    977.   {
    978.     //设置接口链接
    979.     $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";
    980.     //设置curl超时时间
    981.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    982.   }
    983.   /**
    984.    * 生成接口参数xml
    985.    */
    986.   function createXml()
    987.   {
    988.     try
    989.     {
    990.       //检测必填参数
    991.       if($this->parameters["mch_billno"] == null)
    992.       {
    993.         throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."
    994. ");
    995.       }elseif ($this->parameters["send_name"] == null ) {
    996.         throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."
    997. ");
    998.       }elseif ($this->parameters["total_amount"] == null) {
    999.         throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."
    1000. ");
    1001.       }elseif ($this->parameters["total_num"] == null) {
    1002.         throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."
    1003. ");
    1004.       }elseif ($this->parameters["amt_type"] == null) {
    1005.         throw new SDKRuntimeException("缺少发红包接口必填参数amt_type!"."
    1006. ");
    1007.       }elseif ($this->parameters["wishing"] == null) {
    1008.         throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."
    1009. ");
    1010.       }elseif ($this->parameters["act_name"] == null) {
    1011.         throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."
    1012. ");
    1013.       }elseif ($this->parameters["remark"] == null) {
    1014.         throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."
    1015. ");
    1016.       }
    1017.       $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
    1018.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    1019.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    1020.       $this->parameters["re_openid"] = $this->openid;//用户openid
    1021.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    1022.       return $this->arrayToXml($this->parameters);
    1023.     }catch (SDKRuntimeException $e)
    1024.     {
    1025.       die($e->errorMessage());
    1026.     }
    1027.   }
    1028.   function sendRedpack()
    1029.   {
    1030.     $this->postXmlSSL();
    1031.     $this->result = $this->xmlToArray($this->response);
    1032.     return $this->result;
    1033.   }
    1034.   /**
    1035.    * 作用:生成可以获得code的url
    1036.    */
    1037.   function createOauthUrlForCode($redirectUrl)
    1038.   {
    1039.     $urlObj["appid"] = WxPayConf_pub::APPID;
    1040.     $urlObj["redirect_uri"] = "$redirectUrl";
    1041.     $urlObj["response_type"] = "code";
    1042.     $urlObj["scope"] = "snsapi_base";
    1043.     $urlObj["state"] = "STATE"."#wechat_redirect";
    1044.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    1045.     return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    1046.   }
    1047.   /**
    1048.    * 作用:生成可以获得openid的url
    1049.    */
    1050.   function createOauthUrlForOpenid()
    1051.   {
    1052.     $urlObj["appid"] = WxPayConf_pub::APPID;
    1053.     $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    1054.     $urlObj["code"] = $this->code;
    1055.     $urlObj["grant_type"] = "authorization_code";
    1056.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    1057.     return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    1058.   }
    1059.   /**
    1060.    * 作用:通过curl向微信提交code,以获取openid
    1061.    */
    1062.   function getOpenid()
    1063.   {
    1064.     $url = $this->createOauthUrlForOpenid();
    1065.     //初始化curl
    1066.     $ch = curl_init();
    1067.     //设置超时
    1068.     curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    1069.     curl_setopt($ch, CURLOPT_URL, $url);
    1070.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    1071.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    1072.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    1073.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    1074.     //运行curl,结果以jason形式返回
    1075.     $res = curl_exec($ch);
    1076.     curl_close($ch);
    1077.     //取出openid
    1078.     $data = json_decode($res,true);
    1079.     $this->openid = $data['openid'];
    1080.     return $this->openid;
    1081.   }
    1082.   /**
    1083.    * 作用:设置code
    1084.    */
    1085.   function setCode($code_)
    1086.   {
    1087.     $this->code = $code_;
    1088.   }
    1089. }
    1090. /**
    1091. * 摇一摇红包预下单
    1092. * @author jiosen
    1093. */
    1094. class Yhb_pub extends Wxpay_client_pub
    1095. {
    1096.   var $code;//code码,用以获取openid
    1097.   var $openid;//用户的openid
    1098.   function __construct()
    1099.   {
    1100.     //设置接口链接
    1101.     $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder";
    1102.     //设置curl超时时间
    1103.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    1104.   }
    1105.   /**
    1106.    * 生成接口参数xml
    1107.    */
    1108.   function createXml()
    1109.   {
    1110.     try
    1111.     {
    1112.       //检测必填参数
    1113.       if($this->parameters["mch_billno"] == null)
    1114.       {
    1115.         throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."
    1116. ");
    1117.       }elseif ($this->parameters["send_name"] == null ) {
    1118.         throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."
    1119. ");
    1120.       }elseif ($this->parameters["total_amount"] == null) {
    1121.         throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."
    1122. ");
    1123.       }elseif ($this->parameters["total_num"] == null) {
    1124.         throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."
    1125. ");
    1126.       }elseif ($this->parameters["wishing"] == null) {
    1127.         throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."
    1128. ");
    1129.       }elseif ($this->parameters["act_name"] == null) {
    1130.         throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."
    1131. ");
    1132.       }elseif ($this->parameters["remark"] == null) {
    1133.         throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."
    1134. ");
    1135.       }
    1136.       $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
    1137.       $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
    1138.       $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
    1139.       //$this->parameters["re_openid"] = $this->openid;//用户openid
    1140.       $this->parameters["hb_type"] = 'NORMAL';//红包类型 NORMAL-普通红包;GROUP-裂变红包(可分享红包给好友,无关注公众号能力)。
    1141.       $this->parameters["auth_mchid"] = '1000052601';//摇周边商户号
    1142.       $this->parameters["auth_appid"] = 'wxbf42bd79c4391863';//摇周边 appid
    1143.       $this->parameters["risk_cntl"] = 'NORMAL';//风控设置
    1144.       $this->parameters["sign"] = $this->getSign($this->parameters);//签名
    1145.       //echo json_encode($this->parameters);die;
    1146.       return $this->arrayToXml($this->parameters);
    1147.       //echo $this->parameters["auth_appid"].'--'.$this->parameters["auth_mchid"];die;
    1148.     }catch (SDKRuntimeException $e)
    1149.     {
    1150.       die($e->errorMessage());
    1151.     }
    1152.   }
    1153.   function hbpreorder()
    1154.   {
    1155.     $this->postXmlSSL();
    1156.     $this->result = $this->xmlToArray($this->response);
    1157.     return $this->result;
    1158.   }
    1159.   /**
    1160.    * 作用:生成可以获得code的url
    1161.    */
    1162.   function createOauthUrlForCode($redirectUrl)
    1163.   {
    1164.     $urlObj["appid"] = WxPayConf_pub::APPID;
    1165.     $urlObj["redirect_uri"] = "$redirectUrl";
    1166.     $urlObj["response_type"] = "code";
    1167.     $urlObj["scope"] = "snsapi_base";
    1168.     $urlObj["state"] = "STATE"."#wechat_redirect";
    1169.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    1170.     return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
    1171.   }
    1172.   /**
    1173.    * 作用:生成可以获得openid的url
    1174.    */
    1175.   function createOauthUrlForOpenid()
    1176.   {
    1177.     $urlObj["appid"] = WxPayConf_pub::APPID;
    1178.     $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    1179.     $urlObj["code"] = $this->code;
    1180.     $urlObj["grant_type"] = "authorization_code";
    1181.     $bizString = $this->formatBizQueryParaMap($urlObj, false);
    1182.     return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
    1183.   }
    1184.   /**
    1185.    * 作用:通过curl向微信提交code,以获取openid
    1186.    */
    1187.   function getOpenid()
    1188.   {
    1189.     $url = $this->createOauthUrlForOpenid();
    1190.     //初始化curl
    1191.     $ch = curl_init();
    1192.     //设置超时
    1193.     curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    1194.     curl_setopt($ch, CURLOPT_URL, $url);
    1195.     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    1196.     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    1197.     curl_setopt($ch, CURLOPT_HEADER, FALSE);
    1198.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    1199.     //运行curl,结果以jason形式返回
    1200.     $res = curl_exec($ch);
    1201.     curl_close($ch);
    1202.     //取出openid
    1203.     $data = json_decode($res,true);
    1204.     $this->openid = $data['openid'];
    1205.     return $this->openid;
    1206.   }
    1207.   /**
    1208.    * 作用:设置code
    1209.    */
    1210.   function setCode($code_)
    1211.   {
    1212.     $this->code = $code_;
    1213.   }
    1214. }
    1215. /**
    1216. * 摇一摇红包 创建活动
    1217. * @author jiosen
    1218. */
    1219. class addlotteryinfo_pub extends Wxpay_client_pub
    1220. {
    1221.   var $code;//code码,用以获取openid
    1222.   var $openid;//用户的openid
    1223.   function __construct($access_token,$logo)
    1224.   {
    1225.     //设置接口链接
    1226.     $this->url = "https://api.weixin.qq.com/shakearound/lottery/addlotteryinfo?access_token=".$access_token."&use_template=1&logo_url=".$logo;
    1227.     //设置curl超时时间
    1228.     $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    1229.   }
    1230.   /**
    1231.    * 生成接口参数 json
    1232.    */
    1233.   function createJson()
    1234.   {
    1235.     try
    1236.     {
    1237.       //检测必填参数
    1238.       if($this->parameters["title"] == null)
    1239.       {
    1240.         throw new SDKRuntimeException
    1241.      }
    1242. }
    复制代码
    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    勿忘初心,方得始终!
    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部