GetOut_trade_no(); //②、接口调用成功,明确返回调用失败 if($result["return_code"] == "SUCCESS" && $result["result_code"] == "FAIL" && $result["err_code"] != "USERPAYING" && $result["err_code"] != "SYSTEMERROR") { return false; } //③、确认支付是否成功 $queryTimes = 60; $i = 0; while($queryTimes > 0) { $succResult = 0; $queryResult = $this->query($out_trade_no, $succResult); //如果需要等待1s后继续 if($succResult == 2){ $i++; if($i>60){ return 0; }else{ continue; } } else if($succResult == 1){//查询成功 return $queryResult; } else {//订单交易失败 return 0; return false; } } //④、次确认失败,则撤销订单 if(!$this->cancel($out_trade_no)) { throw new WxpayException("撤销单失败!"); } return false; } /** * * 查询订单情况 * @param string $out_trade_no 商户订单号 * @param int $succCode 查询订单结果 * @return 0 订单不成功,1表示订单成功,2表示继续等待 */ public function query($out_trade_no, &$succCode) { $queryOrderInput = new WxPayOrderQuery(); $queryOrderInput->SetOut_trade_no($out_trade_no); $result = WxPayApi::orderQuery($queryOrderInput); // var_dump($result);exit; if($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") { //支付成功 if($result["trade_state"] == "SUCCESS"){ //解锁 $key='apivcomcn20170421'; $ShopOrdID = $result["attach"]; $ShopUserID=urlencode("实体店-白马星汇良仓"); $method = "Order_AddressEdit"; $NewShopUserID = $ShopUserID; $arr = array("method"=>$method,"ShopOrdID"=>$ShopOrdID,"ShopUserID"=>$ShopUserID,"NewShopUserID"=>$NewShopUserID); ksort($arr); $i=0; $stringA=''; foreach($arr as $k=>$v){ $stringA=$stringA.$k.$v; } $stringSignTemp=$stringA.$key; $stringSignTemp=str_replace(" ", "%20", $stringSignTemp); $sign=MD5($stringSignTemp); $content = ""; $content = 'method='.$method.'&ShopOrdID='.$ShopOrdID.'&ShopUserID='.$ShopUserID."&NewShopUserID=".$NewShopUserID; $content .= '&Sign='.$sign; $order_url='http://api.v.com.cn/order/?'.$content; $order_url=str_replace(" ", "%20", $order_url); // var_dump($url);exit; $orderinfo_change=file_get_contents($order_url); $orderinfo_change=json_decode($orderinfo_change,true); //刷新支付信息 $method = "Order_PayNotify"; $transaction_id = $result["transaction_id"]; $mch_id = $result["mch_id"]; $openid = $result["openid"]; $out_trade_no = $result["out_trade_no"]; $total_fee = number_format($result["total_fee"]/100,2,'.',''); $PayMono = urlencode("微信支付"); $arr = array("method"=>$method,"transaction_id"=>$transaction_id,"mch_id"=>$mch_id,"out_trade_no"=>$out_trade_no,"total_fee"=>$total_fee,"ShopOrdID"=>$ShopOrdID,"PayMono"=>$PayMono,"openid"=>$openid); ksort($arr); $i=0; $stringA=''; foreach($arr as $k=>$v){ $stringA=$stringA.$k.$v; } $stringSignTemp=$stringA.$key; $stringSignTemp=str_replace(" ", "%20", $stringSignTemp); $sign=MD5($stringSignTemp); $content = 'method='.$method.'&transaction_id='.$transaction_id.'&mch_id='.$mch_id.'&out_trade_no='.$out_trade_no.'&total_fee='.$total_fee.'&ShopOrdID='.$ShopOrdID.'&PayMono='.$PayMono."&openid=".$openid; $content .= '&Sign='.$sign; $order_url='http://api.v.com.cn/order/?'.$content; $order_url=str_replace(" ", "%20", $order_url); $orderinfo=file_get_contents($order_url); $orderinfo=json_decode($orderinfo,true); if($orderinfo["result"] == "1"){ $succCode = 1; return $result; } } //用户支付中 else if($result["trade_state"] == "USERPAYING"){ $succCode = 2; return $succCode; } } //如果返回错误码为“此交易订单号不存在”则直接认定失败 if($result["err_code"] == "ORDERNOTEXIST") { $succCode = 0; // return $succCode; } else{ //如果是系统错误,则后续继续 $succCode = 2; return $succCode; } return false; } /** * * 撤销订单,如果失败会重复调用10次 * @param string $out_trade_no * @param 调用深度 $depth */ public function cancel($out_trade_no, $depth = 0) { if($depth > 10){ return false; } $clostOrder = new WxPayReverse(); $clostOrder->SetOut_trade_no($out_trade_no); $result = WxPayApi::reverse($clostOrder); //接口调用失败 if($result["return_code"] != "SUCCESS"){ return false; } //如果结果为success且不需要重新调用撤销,则表示撤销成功 if($result["result_code"] != "SUCCESS" && $result["recall"] == "N"){ return true; } else if($result["recall"] == "Y") { return $this->cancel($out_trade_no, ++$depth); } return false; } }