예제 #1
0
파일: Cache.php 프로젝트: pfinal/wechat
 public static function init()
 {
     if (empty(self::$cache)) {
         $config = Kernel::getConfig('cache', array('class' => 'PFinal\\Cache\\FileCache', 'keyPrefix' => 'pfinal.wechat'));
         $class = $config['class'];
         unset($config['class']);
         self::$cache = new $class($config);
     }
 }
예제 #2
0
파일: Session.php 프로젝트: pfinal/wechat
 protected static function init()
 {
     if (static::$session === null) {
         $config = Kernel::getConfig('session', array('class' => 'PFinal\\Session\\NativeSession', 'keyPrefix' => 'pfinal.wechat'));
         $class = $config['class'];
         unset($config['class']);
         static::$session = new $class($config);
     }
 }
예제 #3
0
파일: Log.php 프로젝트: pfinal/wechat
 private static function init()
 {
     if (static::$logger === null) {
         $config = Kernel::getConfig('log', array('class' => 'PFinal\\Wechat\\Support\\Logger'));
         $class = $config['class'];
         unset($config['class']);
         static::$logger = new $class($config);
     }
 }
예제 #4
0
 /**
  * 给指定用户发红包
  * 请确保您的libcurl版本是否支持双向认证,版本高于7.20.1,通过 curl_version() 查看
  *
  * @param $openid
  * @param float $money 红包金额,单位元
  * @param string $actName
  * @param string $sendName
  * @param string $remark
  * @param string $wishing
  * @return array
  * @throws \Exception
  */
 public static function send($openid, $money, $actName = '抢红包活动', $sendName = '商家', $remark = '祝您生活愉快', $wishing = '恭喜发财')
 {
     $helper = Kernel::getRedpackHelper();
     $money = $money * 100;
     //红包金额,转为单位分
     $money = number_format($money, 0, '.', '');
     //去掉小数
     if ($money <= 0) {
         throw new \Exception('金额不能为0');
     }
     $commonUtil = new CommonUtil();
     $helper->setParameter("nonce_str", $commonUtil->create_noncestr());
     //随机字符串,长于 32 位
     $helper->setParameter("mch_billno", $helper->mchId . date('YmdHis') . rand(1000, 9999));
     //订单号
     $helper->setParameter("mch_id", $helper->mchId);
     //商户号
     $helper->setParameter("wxappid", $helper->appId);
     $helper->setParameter("send_name", $sendName);
     //商户名称  红包发送者名称
     $helper->setParameter("re_openid", $openid);
     //接受收红包的用户 用户在wxappid下的openid
     $helper->setParameter("total_amount", $money);
     //付款金额,单位分
     $helper->setParameter("total_num", 1);
     //红包发放总人数
     $helper->setParameter("wishing", $wishing);
     //红包祝福语
     $helper->setParameter("client_ip", '127.0.0.1');
     //调用接口的机器 Ip 地址
     $helper->setParameter("act_name", $actName);
     //活动名称
     $helper->setParameter("remark", $remark);
     //备注信息
     $postXml = $helper->create_hongbao_xml();
     $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
     $responseXml = $helper->curl_post_ssl($url, $postXml);
     if ($responseXml === false) {
         throw new \Exception("curl失败");
     }
     //<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[发放成功]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <mch_billno><![CDATA[1284519901201606210833284760]]></mch_billno> <mch_id>1284519901</mch_id> <wxappid><![CDATA[wxf26a72acf056aba7]]></wxappid> <re_openid><![CDATA[o0N6btzkUNg84Ys9OmUXQexCMz5s]]></re_openid> <total_amount>101</total_amount> <send_listid><![CDATA[0010819155201606210907873331]]></send_listid> <send_time><![CDATA[20160621143330]]></send_time> </xml>
     Log::debug($responseXml);
     /*
     https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5
     错误码
     
     NO_AUTH	发放失败,此请求可能存在风险,已被微信拦截	用户账号异常,被拦截	请提醒用户检查自身帐号是否异常。使用常用的活跃的微信号可避免这种情况。
     SENDNUM_LIMIT	该用户今日领取红包个数超过限制	该用户今日领取红包个数超过你在微信支付商户平台配置的上限	如有需要、请在微信支付商户平台【api安全】中重新配置 【每日同一用户领取本商户红包不允许超过的个数】。
     ILLEGAL_APPID	非法appid,请确认是否为公众号的appid,不能为APP的appid	错误传入了app的appid	接口传入的所有appid应该为公众号的appid(在mp.weixin.qq.com申请的),不能为APP的appid(在open.weixin.qq.com申请的)。
     MONEY_LIMIT	红包金额发放限制	发送红包金额不再限制范围内	每个红包金额必须大于1元,小于200元(可联系微信支付wxhongbao@tencent.com申请调高额度)
     SEND_FAILED	红包发放失败,请更换单号再重试	该红包已经发放失败	如果需要重新发放,请更换单号再发放
     FATAL_ERROR	openid和原始单参数不一致	更换了openid,但商户单号未更新	请商户检查代码实现逻辑
     金额和原始单参数不一致	更换了金额,但商户单号未更新	请商户检查代码实现逻辑	请检查金额、商户订单号是否正确
     CA_ERROR	CA证书出错,请登录微信支付商户平台下载证书	请求携带的证书出错	到商户平台下载证书,请求带上证书后重试
     SIGN_ERROR	签名错误	1、没有使用商户平台设置的商户API密钥进行加密(有可能之前设置过密钥,后来被修改了,没有使用新的密钥进行加密)。
     2、加密前没有按照文档进行参数排序(可参考文档)
     3、把值为空的参数也进行了签名。可到(http://mch.weixin.qq.com/wiki/tools/signverify/ )验证。
     4、如果以上3步都没有问题,把请求串中(post的数据)里面中文都去掉,换成英文,试下,看看是否是编码问题。(post的数据要求是utf8)	1. 到商户平台重新设置新的密钥后重试
     2. 检查请求参数把空格去掉重试
     3. 中文不需要进行encode,使用CDATA
     4. 按文档要求生成签名后再重试
     在线签名验证工具:http://mch.weixin.qq.com/wiki/tools/signverify/
     SYSTEMERROR	请求已受理,请稍后使用原单号查询发放结果	系统无返回明确发放结果	使用原单号调用接口,查询发放结果,如果使用新单号调用接口,视为新发放请求
     XML_ERROR	输入xml参数格式错误	请求的xml格式错误,或者post的数据为空	检查请求串,确认无误后重试
     FREQ_LIMIT	超过频率限制,请稍后再试	受频率限制	请对请求做频率控制(可联系微信支付wxhongbao@tencent.com申请调高)
     NOTENOUGH	帐号余额不足,请到商户平台充值后再重试	账户余额不足	充值后重试
     OPENID_ERROR	openid和appid不匹配	openid和appid不匹配	发红包的openid必须是本appid下的openid
     PARAM_ERROR	act_name字段必填,并且少于32个字符	请求的act_name字段填写错误	填写正确的act_name后重试
     发放金额、最小金额、最大金额必须相等	请求的金额相关字段填写错误	按文档要求填写正确的金额后重试
     红包金额参数错误	红包金额过大	修改金额重试
     appid字段必填,最长为32个字符	请求的appid字段填写错误	填写正确的appid后重试
     订单号字段必填,最长为28个字符	请求的mch_billno字段填写错误	填写正确的billno后重试
     client_ip必须是合法的IP字符串	请求的client_ip填写不正确	填写正确的IP后重试
     输入的商户号有误	请求的mchid字段非法(或者没填)	填写对应的商户号再重试
     找不到对应的商户号	请求的mchid字段填写错误	填写正确的mchid字段后重试
     nick_name字段必填,并且少于16字符	请求的nick_name字段错误	按文档填写正确的nick_name后重试
     nonce_str字段必填,并且少于32字符	请求的nonce_str字段填写不正确	按文档要求填写正确的nonce_str值后重试
     re_openid字段为必填并且少于32个字符	请求的re_openid字段非法	填写对re_openid后重试
     remark字段为必填,并且少于256字符	请求的remark字段填写错误	填写正确的remark后重试
     send_name字段为必填并且少于32字符	请求的send_name字段填写不正确	按文档填写正确的send_name字段后重试
     total_num必须为1	total_num字段值不为1	修改total_num值为1后重试
     wishing字段为必填,并且少于128个字符	缺少wishing字段	填写wishing字段再重试
     商户号和wxappid不匹配	商户号和wxappid不匹配	请修改Mchid或wxappid参数
     */
     $responseObj = @simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
     if ($responseObj === false) {
         throw new \Exception("解析xml失败");
     }
     if ("{$responseObj->result_code}" === "SUCCESS") {
         return (array) $responseObj;
     }
     throw new \Exception($responseObj->err_code . ' ' . $responseObj->return_msg);
 }
예제 #5
0
 protected static function getApi()
 {
     return Kernel::getApi();
 }
예제 #6
0
파일: server.php 프로젝트: pfinal/wechat
    $event->stopPropagation();
}, 1);
*/
Kernel::register(Receive::TYPE_EVENT_SUBSCRIBE, function (WechatEvent $event) {
    $event->setResponse('你关注或是不关注,我都在这里,不悲不喜~~');
    $event->stopPropagation();
});
Kernel::register(Receive::TYPE_EVENT_UNSUBSCRIBE, function (WechatEvent $event) {
    $openid = $event->getMessage()->FromUserName;
    Log::debug($openid . '取消了关注');
});
Kernel::register(Receive::TYPE_SHORT_VIDEO, function (WechatEvent $event) {
    $event->setResponse('收到小视频');
    $event->stopPropagation();
});
Kernel::register(Receive::TYPE_VIDEO, function (WechatEvent $event) {
    $event->setResponse('收到视频');
    $event->stopPropagation();
});
Kernel::register(Receive::TYPE_EVENT_CLICK, function (WechatEvent $event) {
    $event->setResponse('点菜单' . $event->getMessage()->EventKey);
    $event->stopPropagation();
});
Kernel::register(Receive::TYPE_EVENT_SCAN, function (WechatEvent $event) {
    Log::debug('扫码', (array) $event->getMessage());
    $event->setResponse('扫码');
    $event->stopPropagation();
});
//处理微信服务器的请求
$response = Kernel::handle();
echo $response;
예제 #7
0
파일: wxpay.php 프로젝트: pfinal/wechat
<?php

require __DIR__ . '/../vendor/autoload.php';
$config = array('appId' => 'wx00e5904efes9', 'appSecret' => '419ffc73eb1fga846c6b04b', 'token' => '54b780cbe9047', 'encodingAesKey' => 'cz7NR8g4vk5yMydpG3g8amyFGbjavCuR33', 'mchId' => '1220633201', 'apiSecret' => 'b4944959c6eaed319a86e3a10d');
// 初始化SDK
\PFinal\Wechat\Kernel::init($config);
// 获取OpenID
$openid = \PFinal\Wechat\Service\OAuthService::getOpenid();
//$openid='oU3OCt5O46PumN7IE87WcoYZY9r0';
// JS签名
$signPackage = \PFinal\Wechat\Service\JsService::getSignPackage();
// 支付成功通知url
/* 收到支付成功通知时,请使用下面的方法,验证订单号和支付金额
    $info = \PFinal\Wechat\Service\PayService::notify();
    $info数组,内容如下
    [
         mch_id          //微信支付分配的商户号
         appid           //微信分配的公众账号ID
         openid          //用户在商户appid下的唯一标识
         transaction_id  //微信支付订单号
         out_trade_no    //商户订单号
         total_fee       //订单总金额单位默认为分,已转为元
         is_subscribe    //用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效
         attach          //商家数据包,原样返回
         time_end        //支付完成时间
    ]
*/
$notifyUrl = 'http://xxx.com/index.php/wxpay/notify';
// 订单信息
$order = array('totalFee' => 0.01, 'tradeNo' => uniqid(), 'name' => '测试订单');
// 业务签名
예제 #8
0
 public static function getConfig()
 {
     return array('mch_id' => Kernel::getConfig('mchId'), 'appid' => Kernel::getConfig('appId'), 'key' => Kernel::getConfig('apiSecret'));
 }
예제 #9
0
파일: mp.php 프로젝트: pfinal/wechat
<?php

require __DIR__ . '/../vendor/autoload.php';
use PFinal\Wechat\Kernel;
//请复制 config-local.example 为 config-local.php
$config = (require __DIR__ . '/config-local.php');
Kernel::init($config);
if (file_exists(__DIR__ . '/test-data.php')) {
    require __DIR__ . '/test-data.php';
    // 一些测试用的数据
}
$api = Kernel::getApi();
if (isset($_GET['test'])) {
    //var_dump(\PFinal\Wechat\Service\MessageService::send($openid2, new \PFinal\Wechat\Message\News($news)));
    var_dump(\PFinal\Wechat\Service\MaterialService::uploadFileTemporary('./test.jpg', 'image'));
    //var_dump(\PFinal\Wechat\Service\MaterialService::uploadFile('./test.jpg','image'));
    //var_dump(\PFinal\Wechat\Service\MaterialService::uploadFile('./mz.png','image'));
    //array(2) { ["media_id"]=> string(43) "ZbXWSFYu_gmXjP9EW5ydPCSCPlhJC99YeqUtL0rRYXQ" ["url"]=> string(133) "https://mmbiz.qlogo.cn/mmbiz/D7sHwECXBUtWxg2eVOmIsqWOERic2dfBWpjONuWl2prbAI07ZRiasOw9Dt3ibILvs9uIJib7kIiaVbowRWrpnJH1Zew/0?wx_fmt=png" }
    //$arr = \PFinal\Wechat\Service\MaterialService::uploadNews('迈征', 'ZbXWSFYu_gmXjP9EW5ydPCSCPlhJC99YeqUtL0rRYXQ', 'ethan', '描述内容', 'https://mmbiz.qlogo.cn/mmbiz/D7sHwECXBUtWxg2eVOmIsqWOERic2dfBWpjONuWl2prbAI07ZRiasOw9Dt3ibILvs9uIJib7kIiaVbowRWrpnJH1Zew/0?wx_fmt=png', '内容内容');
    //$arr = \PFinal\Wechat\Service\MaterialService::uploadFile('./voice.mp3','voice');
    //$arr = \PFinal\Wechat\Service\MaterialService::uploadFile('./video.mp4', 'video', '测试', '描述');
    //var_dump($arr);
    //var_dump(\PFinal\Wechat\Service\MessageService::send($openid2, new \PFinal\Wechat\Message\Video($videoMediaId, $imageMediaId, 'aa', 'bb')));
    exit;
}
if (isset($_GET['token'])) {
    echo $api->getAccessToken(false);
    //不使用缓存,直接从服务器获取token
    exit;
}
if (isset($_GET['openid'])) {
예제 #10
0
파일: index.php 프로젝트: pfinal/wechat
<?php

require __DIR__ . '/../vendor/autoload.php';
use PFinal\Wechat\Kernel;
use PFinal\Wechat\Service\JsService;
//请复制 config-local.example 为 config-local.php
$config = (require __DIR__ . '/config-local.php');
Kernel::init($config);
$signPackage = JsService::getSignPackage();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <title>微信平台SDK</title>
</head>
<body>

<h1>微信平台SDK</h1>

<ul>
    <li><a href="mp.php?token">刷新accessToken</a></li>
    <li><a href="mp.php?openid">获取openid</a></li>
    <li><a href="mp.php?user">获取用户信息</a></li>
    <li><a href="mp.php?send">发客服消息</a></li>
    <li><a href="mp.php?preview">群发预览</a></li>
    <li><a href="mp.php?redpack">发红包</a></li>