* 此示例为 微信支付(H5 invoke 方式) * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 */ 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);
// 获取会话信息 function getAuthorize() { if (isAuthorize()) { return unserialize($_SESSION[TOKEN]); } } // 请除登录状态 function clearAuthorize() { unset($_SESSION[TOKEN]); } /** * END */ $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());
// 或自行跳转 // $url = $o->getAuthorizeURL($callbackUrl, 'snsapi_userinfo', 'state'); // header("Location: {$url}"); /** * 第二步:通过 code 换取 AccessToken */ // $token = $o->getAccessToken($_GET['code']); /** * 第三步:通过 openid 和 AccessToken 获取用户信息(scope 为 snsapi_userinfo 时有用) */ // $userinfo = $o->getUser($token->openid, $token->access_token); // echo '<pre>'; // var_dump($token, $userinfo); // echo '</pre>'; /** * 完整流程 */ $o = new OAuth(APPID, APPSECRET); if (!isset($_GET['code'])) { $o->authorize($callbackUrl, 'snsapi_userinfo'); } else { try { $token = $o->getAccessToken($_GET['code']); $userinfo = $o->getUser($token->openid, $token->access_token); } catch (OAuthException $e) { exit($e->getMessage()); } echo '<pre>'; var_dump($token, $userinfo); echo '</pre>'; }
require './config.php'; session_start(); use Thenbsp\Wechat\OAuth; use Thenbsp\Wechat\Jssdk; use Thenbsp\Wechat\Wechat; 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('Your callback url'); } else { $token = $o->getAccessToken($_GET['code']); $_SESSION['openid'] = $token->openid; } } /** * 第 1 步:获取 JSSDK 配置 */ $o = new Jssdk(new Wechat(APPID, APPSECRET)); $jssdkConfig = $o->getConfig(array('chooseWXPay'), true); /** * 第 1 步:定义商户 * 设置商户证书路径请使用 setClientCert/setClientKey
<?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);