base.php
public $app;
public $payapp;
public function initialize()
{
//小程序配置
$config=array(
'app_id' => Config('app.WeiXinAPPID'),
'secret' => Config('app.WeiXinAPPSECRET'),
// 下面为可选项
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
);
$this->app= Factory::miniProgram($config);
//验证tocken
//$this->CheckAppTocken();
$payconfig = [
// 必要配置
'app_id' =>Config('app.WeiXinAPPID'),
'mch_id' => Config('app.WeiXinMCHID'),
'key' =>Config('app.WeiXinKEY'), // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path' => Config('app.Domain').'/extend/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
'key_path' => Config('app.Domain').'/extend/cert/apiclient_key.pem', // XXX: 绝对路径!!!!
'notify_url' =>'', // 你也可以在下单时单独设置来想覆盖它
];
$this->payapp = Factory::payment($payconfig);
}
调微信支付参数:
//easywechat调起微信支付
$result = $this->payapp->order->unify([
'body' => '在线支付',
'out_trade_no' =>$order_frontnumber ,
'total_fee' =>(float)($ordermoney*100),
'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
'notify_url' => Config('app.Domain').'/api/pay/wxsuccess1.html', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $openid,
]);
$jssdk = $this->payapp->jssdk;
$config = $jssdk->bridgeConfig($result['prepay_id'],false); // 返回数组
$back['payparam']=$config;
$back['totalorderid']=$totalorderid;
微信支付回调
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Config;
use EasyWeChat\Factory;
use EasyWeChat\Payment\Kernel\BaseClient;
class Pay extends BaseController
{
public $payapp='';
public function initialize()
{
$payconfig = [
// 必要配置
'app_id' =>Config('app.WeiXinAPPID'),
'mch_id' => Config('app.WeiXinMCHID'),
'key' =>Config('app.WeiXinKEY'), // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path' => Config('app.Domain').'/extend/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
'key_path' => Config('app.Domain').'/extend/cert/apiclient_key.pem', // XXX: 绝对路径!!!!
'notify_url' =>'', // 你也可以在下单时单独设置来想覆盖它
];
$this->payapp = Factory::payment($payconfig);
}
public function wxsuccess1() {
BaseClient::setDefaultOptions([
'curl' => [
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
],
'verify'=>false //不开启CURLOPT_SSL_VERIFYPEER, 这里后来线上ssl报错加的,原因忘了
]);
$response = $this->payapp->handlePaidNotify(function ($message, $fail) {
file_put_contents('./wx100.txt',var_export($message,true));
$orderCode = $message['out_trade_no'];
$orderinfo=DB::name('orderfront')->where(['order_frontnumber'=>$orderCode])->find();
// echo '<pre>';
// print_r($orderinfo);
//如果订单不存在 或者 订单已经支付过了告诉微信,我已经处理完了,订单没找到,别再通知我了
if(!$orderinfo ||$orderinfo['order_frontpaystate']==1){ return true; }
DB::startTrans();
if ($message['return_code'] === 'SUCCESS' && $message['result_code']=== 'SUCCESS') {
PayNotify1($message);
DB::commit();
return true;
} else {
DB::rollBack();
return true;
}
});
return $response;
}
common.php
function PayNotify1($back)
{
//file_put_contents('./200.txt',var_export($back,true));
$where = array();
$where['order_frontnumber'] = $back['out_trade_no'];
$info = DB::name('orderfront')->where($where)->find();
$total_fee=round($back['total_fee']/100,2);
$time=time();
if(empty($info['order_frontnumber'])){ return false;}
DB::name('orderfront')->where(['order_frontnumber'=>$info['order_frontnumber']])->update(['order_frontpaystate'=>1,'order_frontpaytime'=>$time]);
//更新订单状态--已预约
DB::name('order')->where(['order_totalordernumber'=>$info['order_totalordernumber']])->update(['order_state'=>1]);
}