|
|
|
|
|
<?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";
|
|
|
|
|
|
|
|
|
|
|
|
function checkParameters()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!$this->parameters["out_trade_no"]) {
|
|
|
|
|
|
throw new Exception("缺少必填参数out_trade_no!" . "<br>");
|
|
|
|
|
|
} elseif (!$this->parameters["body"]) {
|
|
|
|
|
|
throw new Exception("缺少必填参数body!" . "<br>");
|
|
|
|
|
|
} elseif (!$this->parameters["total_fee"]) {
|
|
|
|
|
|
throw new Exception("缺少必填参数total_fee!" . "<br>");
|
|
|
|
|
|
} elseif (!$this->parameters["auth_code"]) {
|
|
|
|
|
|
throw new Exception("缺少必填参数auth_code!" . "<br>");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* 扫码支付,并且确认结果,接口比较慢
|
|
|
|
|
|
* @param $recharge
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function pay(Recharge $recharge)
|
|
|
|
|
|
{
|
|
|
|
|
|
//提交被扫支付
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
|
|
|
|
$this->checkParameters();
|
|
|
|
|
|
$xml = $this->createXml();
|
|
|
|
|
|
$result = $this->postXmlCurl($xml, $this->pay_url);
|
|
|
|
|
|
$result = $this->xmlToArray($result);
|
|
|
|
|
|
|
|
|
|
|
|
//如果返回成功
|
|
|
|
|
|
if (!array_key_exists("return_code", $result) || !array_key_exists("result_code", $result)) {
|
|
|
|
|
|
throw new Exception("接口调用失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dd($result);
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|