小程序支付及退款

  if (empty(cookie("minOpenIds"))) {


              $jsApi = new \JsApi_pub(C("WX_APPID"), C("WX_MCHID"), C("WX_PARTENT"), C("WX_SECRET"));
              $unifiedOrder = new \UnifiedOrder_pub(C('WX_APPID'), C("WX_MCHID"), C("WX_PARTENT"), C('WX_SECRET'));
              $unifiedOrder->setParameter("openid", $openid);//商品描述
              $unifiedOrder->setParameter("body", $info_order["order_def_goods_name"] . ($info_order["order_def_total_num"] > 1 ? "等多件商品" : ""));//商品描述

              $timeStamp = time();
              $out_trade_no = time();
              $unifiedOrder->setParameter("out_trade_no", "$order_no");//商户订单号


          }else{
              /*小程序支付*/
              $body = $info_order["order_def_goods_name"] . ($info_order["order_def_total_num"] > 1 ? "等多件商品" : "");
              $total_fee = $info_order["order_due_pay"];
              $notify = "https://xxxxxx/index.php/CallBack/payNotify.html";
              $pay_param = $this->minPay($order_no,$body,$total_fee,$notify);
 file_put_contents('/hl/logmin.txt',print_r($pay_param,true),FILE_APPEND);
                $this->assign("payParam", $pay_param);
// echo $pay_param;exit;
  $this->assign("jsApiParameters", $pay_param);
   }
   function minPay($order_no,$body,$total_fee,$notify){
            import("Vendor.Wxpay.lib.WxPay#Api", "", ".php");
            $input = new \WxPayUnifiedOrder();
            $input->SetBody($body);
            $input->SetOut_trade_no($order_no);
            $input->SetTotal_fee($total_fee);
            $input->SetNotify_url($notify);
            $input->SetTrade_type("JSAPI");
           $input->SetOpenid(cookie("minOpenIds"));
      //$input->SetOpenid("o42kY4-ybHVNB0hVbSgKijouklWw");
            $config = new \WxPayConfig();
            //  var_dump($config);
            //    echo $config->GetAppId();
            $unifiedOrder = \WxPayApi::unifiedOrder($config,$input);
//    var_dump($unifiedOrder);
      file_put_contents('/hl/logmin.txt',print_r($unifiedOrder,true),FILE_APPEND);
            if ($unifiedOrder['result_code'] == 'SUCCESS' && $unifiedOrder['return_code'] == 'SUCCESS') {
                $time = time();
                $data['timeStamp'] = "$time";              //时间戳
                $data['nonceStr'] = $unifiedOrder['nonce_str'];     //随机字符串
                $data['signType'] = 'MD5';                //签名算法,暂支持 MD5
                $data['package'] = 'prepay_id=' . $unifiedOrder['prepay_id'];  //统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
                $data['paySign'] = $this->genPaySign($unifiedOrder, $time,$config);

                $data['out_trade_no'] = $order_no;
                return json_encode($data);

            }

        }
    //使用退款接口
   if(empty($info_order["openid"])){
//echo 33333;exit;

                       $refund = new \Refund_pub(C("WX_APPID"),C("WX_MCHID"),C("WX_PARTENT"),C("WX_SECRET"));
                       //设置必填参数
                       //appid已填,商户无需重复填写
                       //mch_id已填,商户无需重复填写
                       //noncestr已填,商户无需重复填写
                       //sign已填,商户无需重复填写
                       $refund->setParameter("out_trade_no","".$info_order["order_no"]);//商户订单号
                       $refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
                       $refund->setParameter("total_fee","".$info_order["order_due_pay"]);//总金额
                       $refund->setParameter("refund_fee","".$info_order["order_due_pay"]);//退款金额
                       $refund->setParameter("op_user_id",C("WX_MCHID"));//操作员
                       //非必填参数,商户可根据实际情况选填
                       //$refund->setParameter("sub_mch_id","XXXX");//子商户号
                       //$refund->setParameter("device_info","XXXX");//设备号
                       //$refund->setParameter("transaction_id","XXXX");//微信订单号

                       $refundResult = $refund ->getResult();



   }else{//echo 2222222;exit;
                       import("Vendor.Wxpay.lib.WxPay#Api", "", ".php");
                       $config = new \WxPayConfig();
                       $wxRefundData = new \WxPayRefund();
                       $wxRefundData->SetOut_trade_no($info_order["order_no"]);
                       $wxRefundData->SetOut_refund_no($out_refund_no);
                       $wxRefundData->SetTotal_fee($info_order["order_due_pay"]);
                       $wxRefundData->SetRefund_fee($info_order["order_due_pay"]);
                       $wxRefundData->SetOp_user_id($config->GetMerchantId());
                       //echo  $config->GetMerchantId();
                       //var_dump($config);exit;
                       $refundResult = \WxPayApi::refund($config,$wxRefundData);
   }

猜你喜欢

转载自blog.csdn.net/Lucky____Boy/article/details/86688475