|
|
<?php
|
|
|
|
|
|
namespace App\Libs;
|
|
|
|
|
|
use App\Events\RechargeSucceed;
|
|
|
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}元" . ($recharge->project ? "-" . $recharge->project->name : ""));
|
|
|
$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(json_encode($result, JSON_UNESCAPED_UNICODE));
|
|
|
}
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
//查询支付结果
|
|
|
try {
|
|
|
$out_trade_no = $this->parameters["out_trade_no"];
|
|
|
$query_times = 15;
|
|
|
while ($query_times > 0) {
|
|
|
$query_result = $this->orderQuery($out_trade_no);
|
|
|
if ($query_result["return_code"] != "SUCCESS" && $result["result_code"] != "SUCCESS") {
|
|
|
sleep(2);
|
|
|
$query_times--;
|
|
|
continue;
|
|
|
} elseif (!isset($query_result["trade_state"])) {
|
|
|
sleep(2);
|
|
|
$query_times--;
|
|
|
continue;
|
|
|
} elseif ($query_result["trade_state"] == "USERPAYING") {
|
|
|
sleep(2);
|
|
|
$query_times--;
|
|
|
continue;
|
|
|
} elseif ($query_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);
|
|
|
//充值成功后处理
|
|
|
event(new RechargeSucceed($recharge));
|
|
|
return true;
|
|
|
}
|
|
|
$query_times--;
|
|
|
}
|
|
|
} catch (Exception $exception) {
|
|
|
return $exception;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function manualQuery(Recharge $recharge)
|
|
|
{
|
|
|
$query_result = $this->orderQuery($recharge->serial);
|
|
|
if ($query_result["trade_state"] == "SUCCESS") {
|
|
|
if (!$recharge->paid_at) {
|
|
|
$transaction_id = $query_result["transaction_id"];
|
|
|
$datetime =
|
|
|
substr($query_result["time_end"], 0, 4) . "-"
|
|
|
. substr($query_result["time_end"], 4, 2) . "-"
|
|
|
. substr($query_result["time_end"], 6, 2) . " "
|
|
|
. substr($query_result["time_end"], 8, 2) . ":"
|
|
|
. substr($query_result["time_end"], 10, 2) . ":"
|
|
|
. substr($query_result["time_end"], 12, 2);
|
|
|
$update = [
|
|
|
"paid_at" => $datetime,
|
|
|
"payment_serial" => $transaction_id
|
|
|
];
|
|
|
$recharge->update($update);
|
|
|
//充值成功后处理
|
|
|
event(new RechargeSucceed($recharge));
|
|
|
}
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//todo: 取消支付单
|
|
|
}
|