TP5.x 微信SDK EasyWechat 安装

TP5.x 微信SDK EasyWechat 安装

EasyWeChat安装

composer require naixiaoxin/think-wechat

初探

微信公众号

use EasyWeChat\Factory;
//use think\facade\Env; //tp5.1

class Index{
    public function index(){
        $config = [
            'app_id' => 'wx3cf0f39249eb0xxx',
            'secret' => 'f1c242f4f28f735d4687abb469072xxx',
            'token' => 'TestToken',
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
                //'file' => Env::get('root_path').'/runtime/logs/wechat_payment.log', //tp5.1.x
                //'file' => ROOT_PATH.'/runtime/logs/wechat_payment.log', //tp5.0.x
            ],
            //...
        ];
        //$config = config('wechat.payment.default'); //或从配置文件中读取
        $app = Factory::officialAccount($config);
        $response = $app->server->serve();
        return $response->send();
        //exit;
    }
}

微信支付

5.0和5.1差别出请参照上面公众号例子

use EasyWeChat\Factory;

class Index{
    public function index(){
        $config = config('wechat.payment.default');
        $payment = Factory::payment($config);
        $result = $app->order->unify([
            'body' => '腾讯充值中心-QQ会员充值',
            'out_trade_no' => '20150806125346',
            'total_fee' => 88,
            'spbill_create_ip' => '123.12.12.123', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
            'notify_url' => 'https://pay.weixin.qq.com/wxpay/pay.action', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o',
        ]);
    }
}

以上抛砖引玉,具体参考官方文档

https://www.easywechat.com/docs/5.x/integration