php实现微信支付

官方文档

产品能力概览-文档中心-微信支付商户平台icon-default.png?t=M276https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml这里主要说一下小程序的支付,在官方文档中,有php的demo可以参考。

话不多说。直接贴代码

Controller:

    /**
     * 支付
     */
    public function doPay()
    {
        if (request()->isPost()) {
            $list = request()->post('list');

            $params = [
                'out_trade_no' => 'SDK-KH-' . orderNumber(6),             // 订单编号
                'description'  => $list['description'],                   // 商品描述
                'total'        => intval($list['total']),                 // 付款金额
                'trade_type'   => $list['trade_type'],                    // 交易类型
                'openid'       => $list['openid'],                        // openid
            ];

            if ($params['defray_type'] == 1) {
                // 微信支付
                $result = $this->wxPay->createOrder($params);
            } else {
                // 支付宝支付
                $result = $this->alipay->createOrder($params);
            }

            $this->_error($result['code'], $result['msg'], $result['data']);
        }
    }

    /**
     * 微信支付回调通知
     */
    public function wxPayNotity()
    {
        $headers = [
            'signature' => request()->header('Wechatpay-Signature'),
            'timestamp' => request()->header('Wechatpay-Timestamp'),
            'nonceStr' => request()->header('Wechatpay-Nonce'),
            'serial' => request()->header('Wechatpay-Serial'),
        ];

        $body = file_get_contents("php://input");

        if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
            log4j('当前PHP环境不支持SHA256withRSA');
        }

        // 签名验证
        $signRes = $this->wxPay->verifySign($headers, $body);
       
        // 订单操作
        $result = $this->wxPay->wxPayNotify($body);

        if ($result['code']) {
            log4j('微信支付通知回调 ', $result['msg']);
            exit(1);
        }

        return sprintf("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
    }

Service:

class WxPayService
{
    protected $client;
    protected $headers;

    protected $mchid;               // 商户号
    protected $appid;               // 小程序id
    protected $h5_appid;            // 公众号appid
    protected $h5_secret;           // 公众号密钥
    protected $app_secret;          // 小程序密钥
    protected $mobile_appid;        // 移动应用appid
    protected $serial_number;       // 商户API证书序列号

    protected $api_v3_key;          // V3秘钥

    protected $private_key;         // 商户私钥文件路径
    protected $public_key;          // 微信支付平台证书文件路径

    protected $wx_public_serial;    // 微信公钥证书序列号
    protected $wx_public_key;       // 微信公钥证书地址

    protected $notify_url;          // 支付回调通知地址

    protected $ordersModel;

    public function __construct(Payment $config, Orders $orders)
    {
        $this->headers = ['Accept' => 'application/json'];

        $this->appid = $config->appid;
        $this->h5_appid = $config->h5_appid;
        $this->app_secret = $config->app_secret;
        $this->mchid = $config->merchant_id;
        $this->serial_number = $config->serial_number;
        
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
实现微信支付,你需要先申请微信支付的开发者账号,并获取到相关的支付接口信息。 然后,你可以使用 PHP 语言编写代码来与微信支付接口进行交互,完成支付功能。 以下是一个 PHP 实现微信支付的示例代码: 1. 引入微信支付 SDK ```php require_once __DIR__ . '/WxPayAPI/lib/WxPay.Api.php'; require_once __DIR__ . '/WxPayAPI/example/WxPay.JsApiPay.php'; ``` 2. 初始化配置参数 ```php // 配置参数 $config = new WxPayConfig(); $config->SetAppId('你的APPID'); $config->SetMchId('你的商户ID'); $config->SetKey('你的商户支付密钥'); ``` 3. 创建订单 ```php // 创建订单 $input = new WxPayUnifiedOrder(); $input->SetBody('商品描述'); $input->SetAttach('附加信息'); $input->SetOut_trade_no('商户订单号'); $input->SetTotal_fee('订单总金额,单位为分'); $input->SetTime_start(date('YmdHis')); $input->SetTime_expire(date('YmdHis', time() + 600)); $input->SetNotify_url('支付回调地址'); $input->SetTrade_type('JSAPI'); $input->SetOpenid('用户的OPENID'); $order = WxPayApi::unifiedOrder($config, $input); ``` 4. 获取支付参数 ```php // 获取支付参数 $jsApiParameters = ''; if ($order['return_code'] == 'SUCCESS' && $order['result_code'] == 'SUCCESS') { $tools = new JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($order); } ``` 5. 调用支付 ```php // 调用支付 echo '<script type="text/javascript">'; echo 'function onBridgeReady(){WeixinJSBridge.invoke("getBrandWCPayRequest",'; echo $jsApiParameters; echo ',function(res){'; echo 'if(res.err_msg == "get_brand_wcpay_request:ok"){alert("支付成功!");}else{alert("支付失败!");}'; echo '});}'; echo 'if(typeof WeixinJSBridge == "undefined"){'; echo 'if(document.addEventListener){document.addEventListener("WeixinJSBridgeReady",onBridgeReady,false);}'; echo 'else if(document.attachEvent){document.attachEvent("WeixinJSBridgeReady",onBridgeReady);'; echo 'document.attachEvent("onWeixinJSBridgeReady",onBridgeReady);}}'; echo 'else{onBridgeReady();}'; echo '</script>'; ``` 这里的代码仅为示例,具体实现根据自己的需求进行调整。同时,为了确保支付安全,建议在服务器端进行支付操作,不要将支付密钥等重要信息暴露在客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泽修i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值