/** * 获取配置文件(json) */ public function getConfig(array $api, $debug = false) { $this->config['jsApiList'] = $api; if ($debug) { $this->config['debug'] = true; } return Json::encode($this->config); }
/** * 构造方法 */ function __construct($accessToken, $openid) { $params = array('access_token' => $accessToken, 'openid' => $openid); $curl = new Curl(); $response = $curl->get(static::USERINFO_URL, $params); if (empty($response)) { throw new OAuthException('Get Userinfo Failure: ' . $curl->error()); } $object = Json::decode($response); if (!(isset($object->openid) || isset($object->nickname) || isset($object->headimgurl))) { throw new OAuthException($object->errcode . ': ' . $object->errmsg); } $this->params = $object; }
/** * 授权回调(通过 Code 换取 AccessToken) * */ public function getAccessToken($code) { $params = array('appid' => $this->appid, 'secret' => $this->appsecret, 'code' => $code, 'grant_type' => 'authorization_code'); $curl = new Curl(); $response = $curl->get(static::ACCESS_TOKEN_URL, $params); if (empty($response)) { throw new OAuthException('Get AccessToken Failure: ' . $curl->error()); } $object = Json::decode($response); if (isset($object->access_token) && isset($object->expires_in) && isset($object->openid)) { return $object; } throw new OAuthException($object->errcode . ': ' . $object->errmsg); }
<?php require './config.php'; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\Util\Json; use Thenbsp\Wechat\Util\Cache; try { $cache = new Cache('../Storage'); } catch (\Exception $e) { exit($e->getMessage()); } if ($request = @file_get_contents('php://input')) { $content = Util::XML2Array($request); $cache->set('payment_notify', Json::encode($content)); } // 微信服务器会在支付成功后该求该 URL,以 XML 的方式提交此次支付信息,具体信息类似: // { // "appid":"wx345f3830c28971f4", // "bank_type":"CFT", // "cash_fee":"1", // "fee_type":"CNY", // "is_subscribe":"Y", // "mch_id":"1241642202", // "nonce_str":"fTbkMTXgMmnvTaO1", // "openid":"oWY-5jjLjo7pYUK86JPpwvcnF2Js", // "out_trade_no":"124164220220150803161536", // "result_code":"SUCCESS", // "return_code":"SUCCESS", // "sign":"4B8948BD3E831394C956B5DFA8AAEB1E", // "time_end":"20150803161547", // "total_fee":"1",
/** * 获取用户信息 */ public function getUser($openid, $accessToken) { $params = array('access_token' => $accessToken, 'openid' => $openid); $response = $this->curl->get(static::USERINFO, $params); if (empty($response)) { throw new OAuthException('Get Userinfo Failure: ' . $this->curl->error()); } $object = Json::decode($response); if (isset($object->openid) && isset($object->nickname) && isset($object->headimgurl)) { return $object; } throw new OAuthException("{$object->errcode}: {$object->errmsg}"); }
/** * 获取配置文件(用于 Jssdk chooseWXPay 方式) */ public function getConfigJssdk($asJson = true) { $config = $this->_generateConfig(); $params = array('timestamp' => $config['timeStamp'], 'nonceStr' => $config['nonceStr'], 'package' => $config['package'], 'signType' => $config['signType'], 'paySign' => $config['paySign']); return $asJson ? Json::encode($params) : $params; }
/** * 获取 Ticket(内部方法) */ private function _getTicketResponse($type) { if (null === $this->accessToken) { $accessToken = $this->getAccessToken(); $this->setAccessToken($accessToken); } $params = array('access_token' => $this->accessToken, 'type' => $type); $response = $this->curl->get(static::TICKET_URL, $params); if (empty($response)) { throw new TicketException(sprintf('Get Ticket Failure: %s', $this->curl->error())); } $object = Json::decode($response); if (isset($object->ticket) && isset($object->expires_in)) { return $object; } throw new TicketException("{$object->errcode}: {$object->errmsg}"); }