|
|
<?php
|
|
|
|
|
|
namespace App\Libs;
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
use App\Models\Recharge;
|
|
|
|
|
|
class WxMicroPay extends WxPayCommon
|
|
|
{
|
|
|
public $pay_url = "https://api.mch.weixin.qq.com/pay/micropay";
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* 扫码支付,并且确认结果,接口比较慢
|
|
|
* @param $recharge
|
|
|
*/
|
|
|
public function pay(Recharge $recharge)
|
|
|
{
|
|
|
//提交被扫支付
|
|
|
try {
|
|
|
$this->setParameter("out_trade_no", $recharge->serial);
|
|
|
$this->setParameter("body", "充值{$recharge->money}元");
|
|
|
$this->setParameter("total_fee", $recharge->money * 100);
|
|
|
$this->setParameter("auth_code", request()->auth_code);
|
|
|
|
|
|
if (!$this->parameters["out_trade_no"]) {
|
|
|
throw new Exception("缺少必填参数out_trade_no!");
|
|
|
} elseif (!$this->parameters["body"]) {
|
|
|
throw new Exception("缺少必填参数body!");
|
|
|
} elseif (!$this->parameters["total_fee"]) {
|
|
|
throw new Exception("缺少必填参数total_fee!");
|
|
|
} elseif (!$this->parameters["auth_code"]) {
|
|
|
throw new Exception("缺少必填参数auth_code!");
|
|
|
} elseif (strlen((string)$this->parameters["auth_code"]) != 18) {
|
|
|
throw new Exception("支付码不正确!");
|
|
|
}
|
|
|
|
|
|
$xml = $this->createXml();
|
|
|
$result = $this->postXmlCurl($xml, $this->pay_url);
|
|
|
\Log::info($result);
|
|
|
$result = $this->xmlToArray($result);
|
|
|
|
|
|
//判断接口是否返回成功
|
|
|
if (!array_key_exists("return_code", $result) || !array_key_exists("result_code", $result)) {
|
|
|
throw new Exception("接口调用失败!");
|
|
|
}
|
|
|
if ($result["return_code"] == "SUCCESS" && $result["result_code"] == "FAIL" && $result["err_code"] != "USERPAYING" && $result["err_code"] != "SYSTEMERROR") {
|
|
|
throw new Exception("支付失败!");
|
|
|
}
|
|
|
} catch (Exception $exception) {
|
|
|
return $exception;
|
|
|
}
|
|
|
|
|
|
//查询支付结果
|
|
|
$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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//④、次确认失败,则撤销订单
|
|
|
if (!$this->cancel($out_trade_no)) {
|
|
|
throw new WxpayException("撤销单失败!");
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|