ThinkPhp微信公众号支付

jianfly.com 2019-01-31 1646次浏览

注:关联网站版扫码微信支付

1.Vendor文件夹中放入如下文件,修改里面的Config参数

Weipay

2.php

<?php
namespace Home\Controller;
use Think\Controller;
//手机端的微信支付
class MweipayController extends PublicController {
    public function __construct() 
    {
        parent::__construct();
        vendor('Weipay.Api');
        vendor('Weipay.JsApiPay');
        vendor('Weipay.log');
        vendor('Weipay.Notify');
    }
    public function index() {
        $this->checkLogin();//检测登录
        $id = intval($_GET['id']);//订单id
        if($id > 0)
        {
            $orders = M('orders')->where(array('id'=>$id,'mid'=>$_SESSION['member']['id']))->find();
            if($orders)
            {
                $state = $orders['state'];
                if($state == 0) {
                    $orders["detail"] = M('ordergoods')->where(array('oid'=>$orders['id']))->select();
                    $this->assign("orders",$orders);
                     //开始微信支付
                    //①、获取用户openid
                    $tools = new \JsApiPay();
                    $openId = $tools->GetOpenid();
                    //②、统一下单
                    $input = new \WxPayUnifiedOrder();
                    $input->SetBody("****");
                    $input->SetAttach("在线微信支付");
                    $input->SetOut_trade_no($id);
                    $input->SetTotal_fee($orders["money"]*100);
                    $input->SetTime_start(date("YmdHis"));
                    $input->SetTime_expire(date("YmdHis", time() + 600));
                    $input->SetGoods_tag("订单id:".$orders['id']);
                    $input->SetNotify_url("http://wap.jianfly.com/Mweipay/notifyurl/");
                    $input->SetTrade_type("JSAPI");
                    $input->SetOpenid($openId);
                    $order = \WxPayApi::unifiedOrder($input);
                    $jsApiParameters = $tools->GetJsApiParameters($order);
                    $this->assign("jsApiParameters", $jsApiParameters);
                    $this->display();
                    exit;
                }else {
                    $this->error("该订单已支付");
                    exit;
                }
            }
        }
        $this->_404();
    }
    public function notifyurl(){
        $postArr =$GLOBALS['HTTP_RAW_POST_DATA'];
        $postObj = simplexml_load_string($postArr );
        $arr=json_decode(json_encode(simplexml_load_string($postArr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        $orderid=$postObj->out_trade_no;
        //$money=$arr['total_fee'];
        $order = M('orders')->where('id = '.$orderid)->find();//订单信息
        if($order['state'] == 0)
        {
            $data = array();
            $data['state'] = 1;
            M('orders')->where('id = '.$orderid)->save($data);
            //详情产品
            $order_det = M('ordergoods')->where(array("oid"=>$orderid))->select();
            //销量+1
            foreach ( $order_det as $key=>$val ) {
                M('goods')->where(array("id"=>$val['gid']))->setInc('click');
            }
            //发送短信
            $mid = $order['mid'];//用户id
            $member = M('member')->where(array("id"=>$mid))->find();
            $mobile = $member['mobile'];//手机号
            $time = date("Y-m-d H:i:s", time());//付款时间
            
            $tmpl = "";
            foreach ( $order_det as $key=>$val ) {
                $tmpl .= ( "-".$val['title']."(".$val['num']."个)" );
            }
            $content = "【****】您已成功购买".$tmpl.",购买时间".$time.",请及时选择就近门店取货!";
            $result = $this->sendMsg( $mobile, $content );
        }
        $notify = new \WxPayNotify();
        $notify->Handle(false);
    }
}