微信小程序:微信支付和退款

微信支付的前期准备:

  1. 小程序的appId和密钥(小程序配置界面)
  2. 商户号和api密钥(商家后台自己设置)

 整理支付的逻辑:

附:官方微信统一下单传送门API

 

  1. 在微信小程序端调用支付前,先组装支付的金额给后台发送请求,后台需要调用微信API统一下单
  2. 微信统一下单成功后,微信返回支付的5个参数
  3. 拿到5个参数,方可在小程序端调用wx.requestPayment(),在此微信弹出支付填写密码的界面
  4. 用户支付成功,微信发起支付成功通知,后台接受通知整理支付成功逻辑

 微信退款逻辑:

附:微信官方退款传送门API

  1. 用户主动发起退款,拿到订单信息,退款理由(可选)
  2. 调用退款接口
  3. 退款成功后,微信发起退款成功回调,整理退款后的逻辑

微信js代码:

 //支付
  goPay() {
    var that = this;
    var order_id = that.data.orderres.order_id;
    wx.login({
      success(res) {
        if (res.code) {
          wx.request({
            url: 'https://sslxxxxxxxxxxxxxxx/GoPay.html',
            data: {
              code: res.code,
              order_id: order_id,
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              //接受的5个参数,调用这个方法成功,微信就会弹出输入密码的界面
              wx.requestPayment({
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.package,
                signType: res.data.signType,
                paySign: res.data.paySign,
                success(res) {
                  //支付成功,可做一些逻辑判断
                  console.log("支付成功");
                 },
                fail(res) { 
                  //支付失败,可做一些逻辑判断
                  console.log("支付失败");
                }
              })
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          console.log('获取code失败' + res.errMsg)
        }
      }
    })
  },

php端代码(主要)

public function GoPay($code)
    {
        //获取openid
        $code = I("get.code");
        $infos = get_openid($code);
        $openid = $infos["openid"];
        $order_id = I("get.order_id");
        $factor['openid'] = $openid;
        $factor['order_id'] = $order_id;
        $orderRes = M("order")->where($factor)->find();
        if($orderRes){
            $title = '微信小程序支付';
            $total_price = $orderRes['total_price'];
            $num = 0;
            $sum = $total_price;
            $sum = 0.01;//$total_price ;//0.01;//举例支付0.01*数量
            $fee = $sum;
            $appid = 'DoveAppid';//appid
            $body = $title;
            $mch_id = 'DoveMch_id';//商户号
            $nonce_str = nonce_str();//随机字符串
            $notify_url = 'https://dove.io/Index/GoPayReturn'; //回调的url【自己填写】
            $out_trade_no = order_number($openid);//商户订单号
            $spbill_create_ip = '';//服务器的ip【自己填写】;
            $total_fee = $fee * 100;// 微信支付单位是分,所以这里需要*100
            $trade_type = 'JSAPI';//交易类型 默认
            //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
            $post['appid'] = $appid;
            $post['body'] = $body;
            $post['mch_id'] = $mch_id;
            $post['nonce_str'] = $nonce_str;//随机字符串
            $post['notify_url'] = $notify_url;
            $post['openid'] = $openid;
            $post['out_trade_no'] = $out_trade_no;
            $post['spbill_create_ip'] = $spbill_create_ip;//终端的ip
            $post['total_fee'] = $total_fee;//总金额
            $post['trade_type'] = $trade_type;
            $sign = sign($post);//签名
            $post_xml = '
             ' . $appid . '
             ' . $body . '
             ' . $mch_id . '
             ' . $nonce_str . '
             ' . $notify_url . '
             ' . $openid . '
             ' . $out_trade_no . '
             ' . $spbill_create_ip . '
             ' . $total_fee . '
             ' . $trade_type . '
             ' . $sign . '
             ';
            //统一接口prepay_id
            $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
            $xml = http_request($url, $post_xml);
            $array = xml($xml);
            if ($array['return_code'] == 'SUCCESS' && $array['result_code'] == 'SUCCESS') {
                $time = time();
                $tmp = array();//临时数组用于签名
                $tmp['appId'] = $appid;
                $tmp['nonceStr'] = $nonce_str;
                $tmp['package'] = 'prepay_id=' . $array['prepay_id'];
                $tmp['signType'] = 'MD5';
                $tmp['timeStamp'] = "$time";
                $data['state'] = 200;
                $data['timeStamp'] = "$time";//时间戳
                $data['nonceStr'] = $nonce_str;//随机字符串
                $data['signType'] = 'MD5';//签名算法,暂支持 MD5
                $data['package'] = 'prepay_id=' . $array['prepay_id'];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
                $data['paySign'] = sign($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
                $data['out_trade_no'] = $out_trade_no;

                $orderData['id'] = $orderRes['id'];
                $orderData['is_pay'] = '唤起了支付';
                $orderData['operate_time'] = date("Y/m/d H:i:s "); //订单操作时间
                $orderData["merchant_no"] = $out_trade_no; //商户号
                $orderSave = M("order")->save($orderData);
                if($orderSave !== false){
                    $data['mymsg'] = 1;
                }else{
                    $data['mymsg'] = 0;
                }
            } else {
                $data['state'] = 0;
                $data['text'] = "错误";
                $data['RETURN_CODE'] = $array['return_code'];
                $data['RETURN_MSG'] = $array['return_msg'];
            }

            $this->ajaxReturn($data, 'json');
        }else{
            echo json_encode(array('code' => 'F', 'msg'=>'订单不存在','factor' => $factor));
            die();
        }
    }

  回调方法 支付成功更改支付状态
    public function GoPayReturn(){
        $xml = file_get_contents("php://input");
        $jsonxml = json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA));
        //转成数组
        $result = json_decode($jsonxml, true);
        if ($result) {
            //如果成功返回了
            if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                $openid =$result["openid"];
                $merchant_no =$result["out_trade_no"];//商户订单号
                $price =$result["total_fee"]/100;//订单总金额单位为元
                $number =$result["transaction_id"];//微信支付订单号

                $datas["wx_order_id"] = $number;
                $datas["state"] = '待发货';
                $datas["is_pay"] = '已支付';
                $datas['operate_time'] = date("Y/m/d H:i:s "); //订单操作时间
                $datas["pay_time"] = date("Y/m/d H:i:s");//订单支付时间

                $factor['openid'] = $openid;
                $factor['merchant_no'] = $merchant_no;
                $factor['total_price'] = $price;
                $orderSave = M("order")->where($factor)->save($datas);
                if($orderSave){
                    exit('');
                }else{
                    echo json_encode(array('code'=>'F','res'=>'not found list'));die();
                }
            }
        }else{
            echo "error";
        }
    }

 

你可能感兴趣的:(微信小程序,php)