安装扩展
composer require naixiaoxin/think-wechat
配置
修改配置文件 修改项目根目录下config/wechat.php中对应的参数
'payment' => [
'default' => [
'sandbox' => env('WECHAT_PAYMENT_SANDBOX', false),
'app_id' => env('WECHAT_PAYMENT_APP_ID', ''),
'mch_id' => env('WECHAT_PAYMENT_MCH_ID', 'your-mch-id'),
'key' => env('WECHAT_PAYMENT_KEY', 'key-for-signature'),
'cert_path' => env('WECHAT_PAYMENT_CERT_PATH', 'path/to/cert/apiclient_cert.pem'), // XXX: 绝对路径!!!!
'key_path' => env('WECHAT_PAYMENT_KEY_PATH', 'path/to/cert/apiclient_key.pem'), // XXX: 绝对路径!!!!
'notify_url' => env('WECHAT_PAYMENT_NOTIFY_URL', ''), // 默认支付结果通知地址
],
// ...
],
调起支付
没错,什么 H5 支付,公众号支付,扫码支付,全部都是用这个接口下单。其他的参数不用填,如果是JSAPI支付需要填写openid。
namespace app\user\controller;
use think\Controller;
use think\Request;
use Naixiaoxin\ThinkWechat\Facade;
class Deposit extends Controller
{
public function save()
{
$result = $payment->order->unify([
'body' => '充值',
'out_trade_no' => '1', //商户自己的订单id
'total_fee' => 1, //支付金额,单位为分
'trade_type' => 'NATIVE', //支付方式
]);
dump($result);
}
支付回调
在你的回调文件下
namespace app\user\controller;
use think\Controller;
use think\Request;
use Naixiaoxin\ThinkWechat\Facade;
class WxNotify extends Controller
{
public function save()
{
$payment = Facade::payment();
$respons = $payment->handlePaidNotify(function ($notify,$successful){
if($successful){
//这里是支付成功后的业务操作,$notify是微信带过来的数据
return true;
}else{
return false;
}
});
return $respons;
}
参考
微信支付统一下单api文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
EasyWeChat教程文档:https://www.easywechat.com/docs/4.1/payment/order