ufutx.love.util/app/Http/Controllers/WechatPayController.php

66 lines
2.1 KiB
PHP
Raw Normal View History

2026-04-23 15:32:04 +08:00
<?php
namespace App\Http\Controllers;
use App\Facades\WechatPayService;
use App\Http\Response\ResponseJson;
2026-04-23 15:40:58 +08:00
use Exception;
2026-04-23 15:32:04 +08:00
use Illuminate\Http\Request;
2026-04-23 16:01:25 +08:00
use Illuminate\Support\Facades\Validator;
2026-04-24 11:04:56 +08:00
use Log;
2026-04-23 16:07:06 +08:00
use TypeError;
2026-04-23 15:32:04 +08:00
class WechatPayController extends Controller
{
use ResponseJson;
function mchTransfer(Request $request)
{
2026-04-23 16:01:25 +08:00
$rules = [
2026-04-23 16:14:16 +08:00
"trade_no" => "required|string",
2026-04-23 16:04:09 +08:00
"scene_id" => "string",
2026-04-23 16:14:16 +08:00
"openid" => "required|string",
"amount" => "required|integer",
2026-04-24 11:04:56 +08:00
"remark" => "required|string",
"notify_url" => "required|url"
2026-04-23 16:01:25 +08:00
];
$input = $request->all();
$validator = Validator::make($input, $rules, $messages = [
'required' => 'The :attribute field is required.',
]);
foreach ($validator->errors()->all() as $message) {
return $this->failure($message);
}
2026-04-23 15:32:04 +08:00
$trade_no = $request->trade_no;
$scene_id = $request->scene_id;
if (empty($scene_id)) {
$scene_id = config("wechatpay.screen.commission");
}
$openid = $request->openid;
$amount = $request->amount;
$remark = $request->remark;
2026-04-24 11:04:56 +08:00
$notify_url = $request->notify_url;
$res = WechatPayService::mchTransfer($trade_no, $scene_id, $openid, $amount, $remark, $notify_url, []);
2026-04-23 15:32:04 +08:00
return $this->success("ok", $res);
}
2026-04-24 11:04:56 +08:00
function mchTransferCallback(Request $request)
{
$headers = [
'wechatpay-timestamp' => $_SERVER['HTTP_WECHATPAY_TIMESTAMP'] ?? '',
'wechatpay-nonce' => $_SERVER['HTTP_WECHATPAY_NONCE'] ?? '',
'wechatpay-signature' => $_SERVER['HTTP_WECHATPAY_SIGNATURE'] ?? '',
'wechatpay-serial' => $_SERVER['HTTP_WECHATPAY_SERIAL'] ?? '',
];
$body = file_get_contents('php://input');
2026-04-24 11:20:43 +08:00
Log::info("请求头", ["headers" => $headers]);
Log::info("请求体", ["body" => $body]);
2026-04-24 11:04:56 +08:00
$res = WechatPayService::mchTransferCallback($headers, $body);
Log::info("解密数据", ["data" => $res]);
return $this->success("ok", $res);
}
2026-04-23 15:32:04 +08:00
}