//安装
composer require “overtrue/laravel-wechat:~4.0”
在 config/app.php 注册 ServiceProvider 和 Facade (Laravel 5.5 无需手动注册)
‘providers’ => [
// …
Overtrue\LaravelWeChat\ServiceProvider::class,
],
‘aliases’ => [
// …
‘EasyWeChat’ => Overtrue\LaravelWeChat\Facade::class,
],
创建配置文件:
php artisan vendor:publish --provider=“Overtrue\LaravelWeChat\ServiceProvider”
修改应用根目录下的 config/wechat.php 中对应的参数即可。
参数在微信公众平台配置和查看
同时设置号 域名 白名单 等
demo
/*
* 公众号
/
。。。。
‘official_account’ => [
‘default’ => [
‘app_id’ => env(‘WECHAT_OFFICIAL_ACCOUNT_APPID’, ‘wxe4f459877777’), // AppID
‘secret’ => env(‘WECHAT_OFFICIAL_ACCOUNT_SECRET’, ‘2323233uewuwuewueyue’), //
‘token’ => env(‘WECHAT_OFFICIAL_ACCOUNT_TOKEN’, ‘weee’), // Token
‘aes_key’ => env(‘WECHAT_OFFICIAL_ACCOUNT_AES_KEY’, ‘23333333’),
/
* OAuth 配置
*
* scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
* callback:OAuth授权完成后的回调页地址(如果使用中间件,则随便填写。。。)
*/
// ],
],
],
。。。。。
//支付所需参数 微信商家平台里获取对应参数
‘pay_config’ => [
// 必要配置
‘app_id’ => ‘同上’,
‘mch_id’ => ‘368779###’,
‘secret’ => ‘3b1e348d68610d07wqwqwqw3ee’,
‘key’ => ‘ecipa2018wxhuwqwqwqw0’, // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
‘cert_path’ => ‘…/cert/apiclient_cert.pem’, // XXX: 绝对路径!!!!
‘key_path’ => ‘…/cert/apiclient_key.pem’, // XXX: 绝对路径!!!!
//‘notify_url’ => ‘http://’.$_SERVER[‘HTTP_HOST’].’/api/WeChatNotify’, // 默认的订单回调地址,你也可以在下单时单独设置来想覆盖它
//其他参数
],
证书 微信公众平台有下载地址
···
配置完成后
开始使用
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Payment\Kernel\BaseClient;
use EasyWeChat\Factory;
…
//用户点击支付 生成预支付订单
public function PrepayOrder(Request
r
e
q
u
e
s
t
,
request,
request,order_sn) {
…
$config = config(‘wechat.pay_config’);
a
p
p
=
F
a
c
t
o
r
y
:
:
p
a
y
m
e
n
t
(
app = Factory::payment(
app=Factory::payment(config);
$jssdk = $app->jssdk;
//微信以分为单位 注意订单价格
$actualPrice = $order[‘actual_price’]*100;
//生成预付单
$host = $_SERVER[‘HTTP_HOST’];
try {
$result = $app->order->unify([
‘body’ => $order[‘name’],
‘out_trade_no’ => $order[‘order_sn’],
‘total_fee’ => $actualPrice,//todo
‘attach’ => $order[‘id’],
‘spbill_create_ip’ => ‘’, // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
//‘notify_url’ =>
c
o
n
f
i
g
[
′
n
o
t
i
f
y
u
r
l
′
]
,
/
/
支
付
结
果
通
知
网
址
,
如
果
不
设
置
则
会
使
用
配
置
里
的
默
认
地
址
′
n
o
t
i
f
y
u
r
l
′
=
>
′
h
t
t
p
:
/
/
′
.
config['notify_url'], // 支付结果通知网址,如果不设置则会使用配置里的默认地址 'notify_url' => 'http://'.
config[′notifyurl′],//支付结果通知网址,如果不设置则会使用配置里的默认地址′notifyurl′=>′http://′._SERVER[‘HTTP_HOST’].’/api/WeChatNotify’,
‘trade_type’ => ‘JSAPI’,//支付方式
‘openid’ => $openid,
]);
} catch (InvalidConfigException $e) {
}
//预支付订单号prepayId, 生成支付 JS 配置
$prepayId = $result[‘prepay_id’];
$jsApiParameters =
j
s
s
d
k
−
>
b
r
i
d
g
e
C
o
n
f
i
g
(
jssdk->bridgeConfig(
jssdk−>bridgeConfig(prepayId);
a
r
r
=
[
′
e
r
r
o
r
′
=
>
0
,
′
j
s
A
p
i
P
a
r
a
m
e
t
e
r
s
′
=
>
arr = [ 'error'=>0, 'jsApiParameters'=>
arr=[′error′=>0,′jsApiParameters′=>jsApiParameters,
];
return $arr;
//页面调起微信支付js
#支付后的回调-》 预支付notify_url
use EasyWeChat\Kernel\Exceptions\Exception;
use EasyWeChat\Factory;
use EasyWeChat\Payment\Kernel\BaseClient;
…
//支付结果
public function notify()
{
// 判断是否支付成功
$config = config(‘wechat.pay_config’);
a
p
p
=
F
a
c
t
o
r
y
:
:
p
a
y
m
e
n
t
(
app = Factory::payment(
app=Factory::payment(config);
try {
BaseClient::setDefaultOptions([
‘curl’ => [
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
],
‘verify’=>false //不开启CURLOPT_SSL_VERIFYPEER, 这里后来线上ssl报错加的,原因忘了
]);
$response =
a
p
p
−
>
h
a
n
d
l
e
P
a
i
d
N
o
t
i
f
y
(
f
u
n
c
t
i
o
n
(
app->handlePaidNotify(function (
app−>handlePaidNotify(function(message, $fail) {
$orderSn =
m
e
s
s
a
g
e
[
′
o
u
t
t
r
a
d
e
n
o
′
]
;
/
/
开
启
事
务
并
上
锁
D
B
:
:
b
e
g
i
n
T
r
a
n
s
a
c
t
i
o
n
(
)
;
检
查
价
格
/
/
订
单
不
是
待
付
款
或
者
金
额
不
对
直
接
结
束
/
/
t
o
d
o
价
格
∗
100
D
B
:
:
r
o
l
l
B
a
c
k
(
)
;
r
e
t
u
r
n
t
r
u
e
;
/
/
用
户
是
否
支
付
成
功
i
f
(
message['out_trade_no']; //开启事务并上锁 DB::beginTransaction(); 检查价格 { //订单不是待付款 或者金额不对直接结束//todo价格*100 DB::rollBack(); return true; } // 用户是否支付成功 if (
message[′outtradeno′];//开启事务并上锁DB::beginTransaction();检查价格//订单不是待付款或者金额不对直接结束//todo价格∗100DB::rollBack();returntrue;//用户是否支付成功if(message[‘return_code’] === ‘SUCCESS’ && array_get(KaTeX parse error: Expected '}', got 'EOF' at end of input: … data_set(orderInfo,‘status’,1); //支付成功
data_set(
o
r
d
e
r
I
n
f
o
,
′
o
u
t
o
r
d
e
r
s
n
′
,
a
r
r
a
y
g
e
t
(
orderInfo,'out_order_sn',array_get(
orderInfo,′outordersn′,arrayget(message, ‘transaction_id’)); //支付单号
data_set(
o
r
d
e
r
I
n
f
o
,
′
p
a
y
s
t
a
t
u
s
′
,
1
)
;
/
/
支
付
成
功
d
a
t
a
s
e
t
(
orderInfo,'pay_status',1); //支付成功 data_set(
orderInfo,′paystatus′,1);//支付成功dataset(orderInfo,‘pay_time’,date(“Y-m-d H:i:s”)); //支付时间
data_set(
o
r
d
e
r
I
n
f
o
,
′
a
t
t
r
i
b
u
t
e
s
′
,
j
s
o
n
e
n
c
o
d
e
(
[
]
)
)
;
/
/
清
空
预
支
付
订
单
i
d
i
f
(
orderInfo,'attributes',json_encode([]));//清空预支付订单id if(
orderInfo,′attributes′,jsonencode([]));//清空预支付订单idif(orderInfo->save()){
DB::commit();
}else{
//纪录错误日志。。
$error = new ErrorLogModel($data);
$error->save();
DB::rollBack();
}
}else{
//支付失败直接结束
DB::rollBack();
return true;
}
}
// 添加账单 --无账单处理
//微信和短信通知支付成功
$goods = new GoodsModel;
//获取商品详情
$goodsDetails = $goods->getGoodsById($message['attach']);
//微信通知
ExamNotify::notifyWechat($goodsDetails,$orderInfo->user_id, $message['openid']);
// 短信通知to或邮件通知
$phone = UserModel::query()->where('id', $orderInfo->user_id)->value('phone');
if ($phone) {
ExamNotify::notifySms($goodsDetails,$orderInfo->user_id);
}else{
ExamNotify::notifyEmail($goodsDetails,$orderInfo->user_id);
}
});
return $response;
} catch (Exception $e) {
// 纪录日志
$data = [
'note' => '微信通知异常',
'params' => json_encode($e),
'user_id' => 0,
'code' => 'wechat_notify',
'ip' => ip2long(request()->ip()),
];
$error = new ErrorLogModel($data);
$error->save();
}
作者:胡知鱼
链接:https://www.jianshu.com/p/8dd09d446f20
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。