/** * 构造方法 */ public function __construct(Wechat $wechat) { $config = array('appId' => $wechat->getAppid(), 'nonceStr' => Util::randomString(), 'timestamp' => time()); $params = array('jsapi_ticket' => $wechat->getTicket(), 'noncestr' => $config['nonceStr'], 'timestamp' => $config['timestamp'], 'url' => Util::currentUrl()); $query = urldecode(http_build_query($params)); $config['signature'] = sha1($query); $this->config = $config; }
/** * 构造方法 */ public function __construct(Wechat $wechat) { $config = array('appId' => $wechat->getAppid(), 'nonceStr' => Util::randomString(), 'timestamp' => time()); $signGenerator = new SignGenerator(array('jsapi_ticket' => $wechat->getTicket(), 'noncestr' => $config['nonceStr'], 'timestamp' => $config['timestamp'], 'url' => Util::currentUrl())); $signGenerator->setHashType('sha1'); $signGenerator->setUpper(false); $config['signature'] = $signGenerator->getResult(); $this->config = $config; }
public static function getJssdk(Wechat $wechat, $apis, $debug = false, $asArray = false) { try { $ticket = $wechat->getTicket('jsapi'); } catch (TicketException $e) { exit($e->getMessage()); } $bag = new Bag(); $bag->set('jsapi_ticket', $ticket); $bag->set('timestamp', time()); $bag->set('noncestr', Util::randomString()); $bag->set('url', Util::currentUrl()); $signGenerator = new SignGenerator($bag); $signGenerator->setUpper(false); $signGenerator->setHashType('sha1'); $config = array('appId' => $wechat->getAppid(), 'nonceStr' => $bag->get('noncestr'), 'timestamp' => $bag->get('timestamp'), 'signature' => $signGenerator->getResult(), 'jsApiList' => $apis); if ($debug) { $config['debug'] = true; } return $asArray ? $config : JSON::encode($config); }
*/ require './config.php'; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\OAuth; use Thenbsp\Wechat\Payment; use Thenbsp\Wechat\Payment\Order; use Thenbsp\Wechat\Payment\Business; use Thenbsp\Wechat\Payment\Unifiedorder; use Thenbsp\Wechat\Exception\PaymentException; /** * 以 JSAPI 方式的付款需要获取用户 Openid */ if (!isset($_SESSION['openid'])) { $o = new OAuth(APPID, APPSECRET); if (!isset($_GET['code'])) { $o->authorize(Util::currentUrl()); } else { $token = $o->getAccessToken($_GET['code']); $_SESSION['openid'] = $token->openid; } } /** * 第 1 步:定义商户 * 设置商户证书路径请使用 setClientCert/setClientKey * $business->setClientCert('/your/path/to/apiclient_cert.pem'); * $business->setClientKey('/your/path/to/apiclient_key.pem'); */ $business = new Business(); $business->appid(APPID); $business->appsecret(APPSECRET); $business->mch_id(MCHID);
$o = new OAuth(APPID, APPSECRET); /** * 授权流程 */ if (!isAuthorize()) { // 跳转至授权页 if (!isset($_GET['code'])) { $o->authorize(Util::currentUrl(), 'snsapi_userinfo'); } else { try { $token = $o->getToken($_GET['code']); } catch (OAuthException $e) { exit($e->getMessage()); } setAuthorize($token); header('Location: ' . Util::currentUrl()); } } /** * 根据 Token 来获取用户信息 */ $token = getAuthorize(); $user = $o->getUser($token); echo '<pre>'; var_dump($token); var_dump($user); echo '</pre>'; /** * 刷新 TOKEN */ if (!$o->accessTokenIsValid($token)) {
$business = new Business(); $business->appid(APPID); $business->appsecret(APPSECRET); $business->mch_id(MCHID); $business->mch_key(MCHKEY); // print_r($business->getParams()); /** * 第 2 步:定义订单 */ $order = new Order(); $order->body('iphone 6 plus'); $order->out_trade_no(date('Y-m-dHis') . mt_rand(10000, 99999)); $order->total_fee('1'); // $order->openid($_SESSION['openid']); $order->openid('oWY-5jjLjo7pYUK86JPpwvcnF2Js'); $order->notify_url(Util::currentUrl()); // print_r($order->getParams()); /** * 第 3 步:统一下单 */ $unifiedorder = new Unifiedorder(); $unifiedorder->setBusiness($business); $unifiedorder->setOrder($order); /** * 第 4 步:生成支付配置文件 */ $o = new Payment($unifiedorder); $configJSON = $o->getConfigJssdk(); ?> <!DOCTYPE html>
<?php require './config.php'; use Thenbsp\Wechat\User; use Thenbsp\Wechat\OAuth; use Thenbsp\Wechat\Util\Util; use Thenbsp\Wechat\Exception\OAuthException; // $openid = 'oWY-5jjLjo7pYUK86JPpwvcnF2Js'; /** * 网页授权获取用户信息 */ $o = new OAuth(APPID, APPSECRET); if (!isset($_GET['code'])) { $o->authorize(Util::currentUrl(), 'snsapi_userinfo'); } else { /** * 根据 code 换取 accessToken */ try { $token = $o->getAccessToken($_GET['code']); // echo '<pre>'; // var_dump($token); // echo '</pre>'; } catch (OAuthException $e) { exit($e->getMessage()); } /** * 根据 accessToken 获取用户信息 */ try { $user = new User($token->access_token, $token->openid);