thinkphp微信支付功能

ThinkPHP是一个基于MVC模式开发的PHP框架,可以用于快速开发Web应用程序。微信支付是微信公众平台提供的一种在线支付服务,可以方便地集成到Web应用中。下面是一个ThinkPHP结合微信支付的示例:

  1. 配置微信支付参数

在ThinkPHP项目中的config目录下,新建一个wechat.php文件,添加以下内容:

<?php
return [
    'app_id' => '微信公众号的app_id',
    'mch_id' => '商户号',
    'key' => '商户支付密钥',
    'notify_url' => '支付结果回调地址',
];

  1. 创建微信支付控制器

在ThinkPHP项目中的application目录下,新建一个controller目录,在controller目录下新建一个WechatPayController.php文件,添加以下内容:

<?php
namespace app\controller;

use think\Controller;
use app\library\WechatPay;

class WechatPayController extends Controller
{
    public function pay()
    {
        $openid = session('openid');
        $total_fee = 1; //支付金额
        $order_sn = time(); //订单号,可以自定义
        $notify_url = config('wechat.notify_url');

        $wechatPay = new WechatPay();
        $result = $wechatPay->unifiedOrder($openid, $total_fee, $order_sn, $notify_url);
        if ($result['return_code'] == 'SUCCESS') {
            $prepay_id = $result['prepay_id'];
            $jsApiParam = $wechatPay->getJsApiParameters($prepay_id);
            $this->assign('jsApiParam', $jsApiParam);
            return $this->fetch();
        } else {
            echo $result['return_msg'];
        }
    }

    public function notify()
    {
        $wechatPay = new WechatPay();
        $result = $wechatPay->notify();
        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            //处理订单逻辑
            echo 'success';
        } else {
            echo 'fail';
        }
    }
}

在上面的代码中,pay()方法用于生成预支付订单并获取支付参数,notify()方法用于接收支付结果回调通知并处理订单逻辑。

  1. 创建微信支付类

在ThinkPHP项目中的application目录下,新建一个library目录,在library目录下新建一个WechatPay.php文件,添加以下内容:

<?php
namespace app\library;

use think\facade\Config;
use EasyWeChat\Factory;

class WechatPay
{
    private $app;
    private $config;

    public function __construct()
    {
        $this->config = [
            'app_id' => config('wechat.app_id'),
            'mch_id' => config('wechat.mch_id'),
            'key' => config('wechat.key'),
            'notify_url' => config('wechat.notify_url'),
        ];

        $this->app = Factory::payment($this->config);
    }

    public function unifiedOrder($openid, $totalFee, $orderSn, $notifyUrl)
    {
        $attributes = [
            'openid' => $openid,
            'trade_type' => 'JSAPI',
            'body' => '测试支付', //商品描述
            'out_trade_no' => $orderSn,
            'total_fee' => $totalFee * 100,
            'notify_url' => $notifyUrl,
        ];

        return $this->app->order->unify($attributes);
    }

    public function getJsApiParameters($prepayId)
    {
        return $this->app->jssdk->bridgeConfig($prepayId);
    }

    public function notify()
    {
        $response = $this->app->handlePaidNotify(function ($message, $fail) {
            //处理支付结果
            return true;
        });

        return $response;
    }
}

在上面的代码中,WechatPay类用于统一下单、获取支付参数以及处理支付结果回调通知。

  1. 创建支付页面

在ThinkPHP项目中的application目录下,新建一个view目录,在view目录下新建一个wechat_pay目录,在wechat_pay目录下新建一个pay.html文件,添加以下内容:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>微信支付</title>
</head>
<body>
    <button id="payBtn">微信支付</button>

    <script src="//res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
    <script>
        wx.config({{ jsApiParam }});
        wx.ready(function() {
            document.querySelector('#payBtn').onclick = function() {
                wx.chooseWXPay({
                    timestamp: '', // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。
                    nonceStr: '', // 支付签名随机串,不长于 32 位
                    package: '', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
                    signType: '', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
                    paySign: '', // 支付签名
                    success: function(res) {
                        // 支付成功后的回调函数
                    },
                    fail: function(res) {
                        // 支付失败后的回调函数
                    }
                });
            }
        });
    </script>
</body>
</html>

在上面的代码中,pay.html用于展示支付页面,并使用微信JS-SDK接口调用支付。

注意:在使用微信支付时,需要先获取用户的openid,并将openid保存在session中。可以在微信网页授权中获取用户的openid。具体实现方式可以参考微信公众平台文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值