分类
后端

微信公众号,微信小程序插件

官方文档
https://www.easywechat.com/docs/4.1/installation

#扫码支付
use EasyWeChat\Factory;
public function get_code_url()
{
        $config = [
            // 必要配置
            'app_id' => '', //开发者ID(AppID)
            'mch_id' => '',  //商户ID
            'key' => '',   // API 密钥
            'notify_url' => "",     //必须带上域名  如https://www.baidu.com/
        ];
        $app = Factory::payment($config);
        $result = $app->order->unify([
            'trade_type' => 'NATIVE',//扫码支付类型
            'product_id' => "",//商品ID
            'body' => '',//付款备注
            'out_trade_no' => "",//订单号
            'total_fee' => "",//支付金额
        ]);
        if (!isset($result['code_url'])) { //及是二维码连接 需要手动生成二维码
            $this->error('操作失败');
        }
        $this->assign('data', $order);
        $this->assign('code_url', $result['code_url']);
        return view(); 
}
#生成二维码 composer require endroid/qr-code
use Endroid\QrCode\QrCode;
public function get_qr_code()
{
        $code_url = $this->request->param("code_url", '', 'trim');
        if (!$code_url) {
            $this->error('操作失败');
        }
        $qrCode = new QrCode($code_url);
        return $qrCode->writeString();
}
#微信公众号支付
#1. 授权 获取openid
    private function config_user()
    {
        $config = [
            // 必要配置
            'app_id' => '',
            'mch_id' => '',
            'key' => '',   // API 密钥
            'notify_url' => "",     // 你也可以在下单时单独设置
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'callback' => 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"],
            ],
            'secret' => '',
        ];
        $apps = Factory::OfficialAccount($config);
        return $apps;
    }
    
private function get_openid()
{
        $code = $this->request->param("code", '', 'trim');
        $apps = $this->config_user();
        if (!$code) {
            $oauth = $apps->oauth;
            //记录当前URL
            $oauth->redirect()->send();
        } else {
            $user = $apps->oauth->user();
            // 获取 openid
            $OPENID = $user->getId();
        }
        return $OPENID;
}


#2. 生成jssdk支付
    private function config_pay()
    {
        $config = [
            // 必要配置
            'app_id' => 'wxb4769a105b20a410',
            'mch_id' => '1589736411',
            'key' => '3e612f64850d07c247dcb038d121d86c',   // API 密钥
            'notify_url' => "http://" . $_SERVER['SERVER_NAME'] . url('index/pay/notify_url'),     // 你也可以在下单时单独设置
        ];
        $app = Factory::payment($config);
        return $app;
    }

public function pay()
{
        $app = $this->config_pay();
        $payment = $app->order->unify([
            'body' => '购买手机号',
            'out_trade_no' => $order['order_sn'],
            'total_fee' => intval($order['price'] * 100),
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $this->get_openid(), //上面获取的$OPENID  如果用户$OPENID存在于数据库中可直接获取
        ]);
        //生成JSSDK
        $jssdk = $app->jssdk;
        $config = $jssdk->bridgeConfig($payment['prepay_id']);
        $this->assign('config',$config);
        return view();
}

3.js 发起 jssdk
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信支付</title>
</head>
<body>
</body>
<script type="text/javascript" src="/assets/js/jquery.js"></script>//必须要 jquery链接
<script>
    $(document).ready(function(){
        callpay();
    });
    function jsApiCall()
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
             {$config},
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok"){
                alert('支付成功',{icon: 1});
            }else{
                //支付调试方法
                // alert(res.err_code+res.err_desc+res.err_msg);
                // return false;
                alert('支付失败');
            }
        }
    );
    }
    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>
</html>