You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Customer;
use App\Events\RechargeSucceed;
use App\Http\Controllers\Controller;
use App\Models\Orders;
use App\Models\Recharge;
use Illuminate\Support\Facades\Log;
class PayCallbackController extends Controller
{
public function index()
{
//处理回调数据
$xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
Log::info($xml);
$array = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$pay = (new Recharge())->with("order")->where("serial", $array["out_trade_no"])->first();
if (!$pay) exit();
if ($pay->paid_at) exit();
Log::info($pay);
//开始处理业务
$notify = new \NotifyPub($pay->order->project_id);
//存储微信的回调
$notify->saveData($xml);
//验证签名,并回应微信。
//对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
//微信会通过一定的策略如30分钟共8次定期重新发起通知
//尽可能提高通知的成功率,但微信不保证通知最终能成功。
if ($notify->checkSign() == FALSE) {
$notify->setReturnParameter("return_code", "FAIL");//返回状态码
$notify->setReturnParameter("return_msg", "签名失败");//返回信息
} else {
$notify->setReturnParameter("return_code", "SUCCESS");//设置返回码
}
$returnXml = $notify->returnXml();
echo $returnXml;
if ($notify->checkSign() == TRUE) {
if ($notify->data["return_code"] == "FAIL") {
exit();
}
$transaction_id = $notify->data["transaction_id"];
$update = [
"paid_at" => date("Y-m-d H:i:s"),
"payment_serial" => $transaction_id
];
$result = $pay->update($update);
//充值成功后处理
if ($result) {
event(new RechargeSucceed($pay));
}
}
}
}