# 实际运用
微信支付调起微信支付失败的测试
```
alert(res.err_code+res.err_desc+res.err_msg);
```
1. 发起获取code操作 **`$this->code();`**
2. 通过code获取用户openid **`$this->get_code();`**
3. 通过openid 获取 prepay_id `3.4步都在第四步`
4. 通过prepay_id 获取 JSSDK配置信息 **`$this->payment();`**
5. 通过JSSDK发起微信支付 ` 这个操作是前端操作,看最下面`
6. 支付成功后,微信返回异步通知 `微信操作`
7. 处理异步通知结果 **`$this->notify();`**
```
~~~
namespace app\api\controller;
use EasyWeChat\Factory;
/**
* //微信支付
* Class Pays
* @package app\api\controller
*/
class Pays
{
protected $wechat_config = [];
public function __construct()
{
$this->wechat_config = [
'app_id' => 'wx33bbed8b7c826d52',
'mch_id' => '1490502702',
'key' => '6c9e463accf01208d0948dfc4e2bb45b',
'secret' => '9c0229d48cf57fdcecb347ae92f310bd',
'token' => 'ivears_2012',
'notify_url' => 'http://' . $_SERVER['SERVER_NAME'] . url('home/pays/notify_url'),
'log' => [
'level' => 'debug',
'file' => '/tmp/easywechat.log',
],
'oauth' => [
'scopes' => ['snsapi_base'],
'callback' => 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"],
],
'aes_key' => 'iJakxyndRYJqKKSC7121F3gC3bN13Xv9ODkgKlZSIBZ',
];
}
/**
* 获取微信 JSSDK
* @param $data
* @return array|string
*/
public function payment($data)
{
$app = Factory::payment($this->wechat_config);
//获取微信 prepay_id
$payment = $app->order->unify([
'body' => $data['body'],
'out_trade_no' => $data['out_trade_no'],
'total_fee' => $data['total_fee'],
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => 'oQEMT1ZM340eG2O_xpBKGGKdG9sY',
]);
//生成JSSDK
$jssdk = $app->jssdk;
$config = $jssdk->bridgeConfig($payment['prepay_id']);
return $config;
}
/**
* 发起 获取微信CODE 操作
*/
public function code()
{
$app = Factory::OfficialAccount($this->wechat_config);
$oauth = $app->oauth;
//记录当前URL
$oauth->redirect()->send();
}
/**
* 微信授权登录回调地址, 获取code
* @return mixed
*/
public function get_code()
{
$app = Factory::OfficialAccount($this->wechat_config);
$user = $app->oauth->user();
// 获取 openid
$OPENID = $user->getId();
return $OPENID;
}
/**
* 微信异步回调方法
*/
public function notify()
{
$app = Factory::payment($this->wechat_config);
$response = $app->handlePaidNotify(function ($message, $fail) {
$Pays = new \app\home\controller\Pays();
$result = $Pays->result_reduction($message);
if ($result) {
return true;
}
$fail('Order not exists.');
});
$response->send();
}
}
~~~
```
# `发起微信支付前端页面`
```
<script>
$(document).ready(function(){
callpay();
});
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<?php echo $config; ?>,
function(res){
var pay_type = "{$pay_type}";
if(res.err_msg == "get_brand_wcpay_request:ok"){
layer.msg('支付成功',{icon: 1});
setTimeout(function(){
//返回支付成功页面
window.location = "{:url('members/report/index')}";
},1000)
}else{
//支付调试方法
// alert(res.err_code+res.err_desc+res.err_msg);
// return false;
layer.msg('充值失败',{icon: 2});
setTimeout(function(){
//返回支付失败页面
window.location = "{$site_url}"+"{:url('members/report/index')}";
},1000)
}
}
);
}
function callpay()
{
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}else{
jsApiCall();
}
}
</script>
```
- ThinkPHP
- MySql
- 事务
- 漏洞修复
- jQuery
- select搜索下拉框,多选,全选等
- 移动端富文本编辑器
- 复制内容
- ajax请求
- PHP
- 基本语法
- 关于屏蔽“未定义数组下标:0”错误的问题
- PHP文件操作
- 使用PhpOffice 在PHP7.3中导出数据报错问题
- curl
- PHP生成PPT
- Redis
- 如何在PHP中安装Redis扩展
- Redis队列,秒杀,抢购
- 压力测试(ab)
- apache ab压力测试
- 微信
- 微信支付
- 微信分享
- 授权登录
- 微信公众号服务器
- 自动回复
- 微信发起支付html页面
- composer
- 安装
- 二维码的生成
- Excel导入导出
- 数据采集
- 采集列表
- 采集内容详情
- 图片采集
- Linux
- 常用命令
- 解压压缩
- 服务器
- 服务器运行缓慢的原因
- IIS 隐藏index.php
- 端口检测
- Nginx
- 数据库
- Oracle数据库
- MySql
- 新增MySql
- Field字段
- Tp验证码加载不出来,解决办法
- API接口
- Google API
- 织梦CMS支付接口
- SMTP发送邮箱问题
- Could not connect to SMTP host
- 阿里云API接口
- 支付宝新版配置
- 腾讯API接口
- 安装方法
- 百度地图API
- 通过ip地址获取当前城市
- 常用笔记
- yarn