From 3114c3e5824533d98a33a1769453c9c8a92f3af7 Mon Sep 17 00:00:00 2001 From: weizong song Date: Mon, 5 Apr 2021 15:44:19 +0800 Subject: [PATCH] up --- app/Libs/WxMicroPay.class.php | 90 +++++++++++++--------------------- app/Libs/WxPayCommon.class.php | 47 ++++++++---------- 2 files changed, 53 insertions(+), 84 deletions(-) diff --git a/app/Libs/WxMicroPay.class.php b/app/Libs/WxMicroPay.class.php index e0a1bd1..e44614e 100644 --- a/app/Libs/WxMicroPay.class.php +++ b/app/Libs/WxMicroPay.class.php @@ -2,6 +2,7 @@ namespace App\Libs; +use App\Events\RechargeSucceed; use Exception; use App\Models\Recharge; @@ -53,64 +54,39 @@ class WxMicroPay extends WxPayCommon } //查询支付结果 - $out_trade_no = $this->parameters["out_trade_no"]; - $queryTimes = 10; - while ($queryTimes > 0) { - $succResult = 0; - $queryResult = $this->query($out_trade_no, $succResult); - //如果需要等待1s后继续 - if ($succResult == 2) { - sleep(2); - continue; - } else if ($succResult == 1) {//查询成功 - event(); - return $queryResult; - } else {//订单交易失败 - break; - } - } - - - //4、次确认失败,则撤销订单 - if (!$this->cancel($out_trade_no)) { - throw new WxpayException("撤销单失败!"); - } - - return true; - - - //取订单号 - $out_trade_no = $microPayInput->GetOut_trade_no(); - - //②、接口调用成功,明确返回调用失败 - if ($result["return_code"] == "SUCCESS" && - $result["result_code"] == "FAIL" && - $result["err_code"] != "USERPAYING" && - $result["err_code"] != "SYSTEMERROR") { - return false; - } - - //③、确认支付是否成功 - $queryTimes = 10; - while ($queryTimes > 0) { - $succResult = 0; - $queryResult = $this->query($out_trade_no, $succResult); - //如果需要等待1s后继续 - if ($succResult == 2) { - sleep(2); - continue; - } else if ($succResult == 1) {//查询成功 - return $queryResult; - } else {//订单交易失败 - break; + try { + $out_trade_no = $this->parameters["out_trade_no"]; + $query_times = 10; + while ($query_times > 0) { + $query_result = $this->query($out_trade_no); + if ($query_result["return_code"] == "SUCCESS" && $result["result_code"] == "FAIL" && $result["err_code"] == "SYSTEMERROR") { + \Log::info($query_result); + sleep(2); + $query_times--; + continue; + } + if ($query_result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS" && $result["trade_state"] == "USERPAYING") { + \Log::info($query_result); + sleep(2); + $query_times--; + continue; + } + if ($query_result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS" && $result["trade_state"] == "SUCCESS") { + $transaction_id = $query_result["transaction_id"]; + $update = [ + "paid_at" => date("Y-m-d H:i:s"), + "payment_serial" => $transaction_id + ]; + $recharge->update($update); + //充值成功后处理 + if ($result) { + event(new RechargeSucceed($recharge)); + } + return true; + } } + } catch (Exception $exception) { + return $exception; } - - //④、次确认失败,则撤销订单 - if (!$this->cancel($out_trade_no)) { - throw new WxpayException("撤销单失败!"); - } - - return false; } } diff --git a/app/Libs/WxPayCommon.class.php b/app/Libs/WxPayCommon.class.php index 3dcd862..1e16b2a 100644 --- a/app/Libs/WxPayCommon.class.php +++ b/app/Libs/WxPayCommon.class.php @@ -3,6 +3,7 @@ namespace App\Libs; use Exception; +use Illuminate\Support\Facades\Log; class WxPayCommon { @@ -12,6 +13,7 @@ class WxPayCommon public $ssl_cert_path; public $ssl_key_path; public $parameters = []; + public $query_url = "https://api.mch.weixin.qq.com/pay/orderquery"; public function __construct() { @@ -224,35 +226,26 @@ class WxPayCommon } } + /** - * - * 查询订单,WxPayOrderQuery中out_trade_no、transaction_id至少填一个 - * appid、mchid、spbill_create_ip、nonce_str不需要填入 - * @param WxPayConfigInterface $config 配置对象 - * @param WxPayOrderQuery $inputObj - * @param int $timeOut - * @return 成功时返回,其他抛异常 - * @throws WxPayException + * @param $out_trade_no + * @param int $time_out + * @return mixed */ - protected function orderQuery($inputObj, $timeOut = 6) + protected function orderQuery($out_trade_no, $time_out = 6) { - $url = "https://api.mch.weixin.qq.com/pay/orderquery"; - //检测必填参数 - if (!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) { - throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!"); - } - $inputObj->SetAppid($config->GetAppId());//公众账号ID - $inputObj->SetMch_id($config->GetMerchantId());//商户号 - $inputObj->SetNonce_str(self::getNonceStr());//随机字符串 - - $inputObj->SetSign($config);//签名 - $xml = $inputObj->ToXml(); - - $startTimeStamp = self::getMillisecond();//请求开始时间 - $response = self::postXmlCurl($config, $xml, $url, false, $timeOut); - $result = WxPayResults::Init($config, $response); - self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间 - - return $result; + $array = [ + "appid" => $this->app_id, + "mch_id" => $this->merchant_id, + "out_trade_no" => $out_trade_no, + "nonce_str" => $this->createNoncestr(), + ]; + $array["sign"] = $this->getSign($array); + $xml = $this->arrayToXml($array); + + $response = $this->postXmlCurl($xml, $this->query_url, $time_out); + $response = $this->xmlToArray($response); + + return $response; } }