Skip to content

Commit

Permalink
add payment/address
Browse files Browse the repository at this point in the history
  • Loading branch information
thenbsp committed Feb 20, 2016
1 parent 27ed778 commit d606b6a
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
96 changes: 96 additions & 0 deletions example/payment-address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

require './example.php';

use Thenbsp\Wechat\Bridge\Util;
use Thenbsp\Wechat\OAuth\Client;
use Thenbsp\Wechat\Payment\Address\ConfigGenerator;

/**
* 只能在微信中打开
*/
if ( Util::isWechat() ) {
exit('请在微信中打开');
}

/**
* 获取用户 AccessToken
*/
if( !isset($_SESSION['token']) ) {

$client = new Client(APPID, APPSECRET);

if( !isset($_GET['code']) ) {
header('Location: '.$client->getAuthorizeUrl());
}

try {
$token = $client->getAccessToken($_GET['code']);
} catch (\Exception $e) {
exit($e->getMessage());
}

$_SESSION['token'] = $token;
}

/**
* 生成收货地址共享组件配置
*/
$config = new ConfigGenerator($_SESSION['token']);

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Wechat SDK</title>
</head>
<body ontouchstart="">

<h1>共享收货地址&nbsp;&nbsp;<a href="javascript:;" onclick="window.location.reload()">刷新</a></h1>

<button type="button" onclick="getAddress()" style="font-size:16px;height:38px;">选择收货地址</button>

<ul>
<li><strong>姓名:</strong><span id="name"></span></li>
<li><strong>手机:</strong><span id="mobile"></span></li>
<li><strong>省份:</strong><span id="provice"></span></li>
<li><strong>城市:</strong><span id="city"></span></li>
<li><strong>详细地址:</strong><span id="address"></span></li>
<li><strong>邮政编码:</strong><span id="postalCode"></span></li>
<li><strong>收货地址国家码:</strong><span id="nationalCode"></span></li>
</ul>

<script>
var getAddress = function() {
if( typeof WeixinJSBridge === 'undefined' ) {
alert('请在微信在打开页面!');
return false;
}
WeixinJSBridge.invoke('editAddress', <?php echo $config; ?>, function(res) {
switch(res.err_msg) {
case 'edit_address:ok':
document.getElementById('name').innerHTML = res.userName;
document.getElementById('mobile').innerHTML = res.telNumber;
document.getElementById('provice').innerHTML = res.proviceFirstStageName;
document.getElementById('city').innerHTML = res.addressCitySecondStageName;
document.getElementById('address').innerHTML = res.addressDetailInfo;
document.getElementById('postalCode').innerHTML = res.addressPostalCode;
document.getElementById('nationalCode').innerHTML = res.nationalCode;
break;
case 'edit_address:fail':
alert('获取编辑收货地址失败!');
break;
case 'edit_address:cancel':
alert('您已取消获取地址!');
break;
default:
alert(JSON.stringify(res));
break;
}
});
}
</script>
</body>
</html>
8 changes: 8 additions & 0 deletions src/OAuth/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function __construct($appid, ArrayCollection $options)
parent::__construct($options->toArray());
}

/**
* 公众号 appid
*/
public function getAppid()
{
return $this->appid;
}

/**
* 刷新用户 access_token
*/
Expand Down
75 changes: 75 additions & 0 deletions src/Payment/Address/ConfigGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Thenbsp\Wechat\Payment\Address;

use Thenbsp\Wechat\Bridge\Util;
use Thenbsp\Wechat\Bridge\Serializer;
use Thenbsp\Wechat\OAuth\AccessToken;

class ConfigGenerator
{
/**
* Thenbsp\Wechat\OAuth\AccessToken
*/
protected $accessToken;

/**
* 构造方法
*/
public function __construct(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
}

/**
* 设置用户 AccessToken
*/
public function setAccessToken(AccessToken $accessToken)
{
if( !$accessToken->isValid() ) {
$accessToken->refresh();
}

$this->accessToken = $accessToken;
}

/**
* 获取配置
*/
public function getConfig($asArray = false)
{
$options = array(
'appid' => $this->accessToken->getAppid(),
'url' => Util::getCurrentUrl(),
'timestamp' => Util::getTimestamp(),
'noncestr' => Util::getRandomString(),
'accesstoken' => $this->accessToken['access_token']
);

// 按 ASCII 码排序
ksort($options);

$signature = http_build_query($options);
$signature = urldecode($signature);
$signature = sha1($signature);

$config = array(
'appId' => $options['appid'],
'scope' => 'jsapi_address',
'signType' => 'sha1',
'addrSign' => $signature,
'timeStamp' => $options['timestamp'],
'nonceStr' => $options['noncestr'],
);

return $asArray ? $config : Serializer::jsonEncode($config);
}

/**
* 输出对象
*/
public function __toString()
{
return $this->getConfig();
}
}

0 comments on commit d606b6a

Please sign in to comment.