101 lines
4.3 KiB
PHP
101 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Pay;
|
|
|
|
use App\Models\Pay\Bill;
|
|
use App\Models\Pay\RefundBill;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yansongda\LaravelPay\Facades\Pay;
|
|
|
|
class NotifyController
|
|
{
|
|
/**
|
|
* 微信异步通知入口
|
|
*
|
|
* @param $config_name
|
|
* @return \Psr\Http\Message\ResponseInterface|void
|
|
* @throws \Throwable
|
|
* @throws \Yansongda\Pay\Exception\ContainerException
|
|
* @throws \Yansongda\Pay\Exception\InvalidParamsException
|
|
*/
|
|
public function payWechat($config_name)
|
|
{
|
|
$result = Pay::wechat()->callback(null, ['_config' => $config_name]);
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
switch ($result['event_type']) {
|
|
case 'TRANSACTION.SUCCESS': // 支付成功
|
|
$payData = $result['resource']['ciphertext'];
|
|
if ($payData['trade_state'] == 'SUCCESS') {
|
|
$data = [
|
|
'mchid' => $payData['mchid'],
|
|
'appid' => $payData['appid'],
|
|
'pay_no' => $payData['out_trade_no'],
|
|
'pay_service_no' => $payData['transaction_id'],
|
|
'pay_amount' => $payData['amount']['total'] / 100,
|
|
'pay_at' => Carbon::parseFromLocale($payData['success_time'])->toDateTimeString(),
|
|
];
|
|
$handleResult = Bill::handleNotify($data, Bill::PAY_WAY_WECHAT);
|
|
if ($handleResult === true) {
|
|
DB::commit();
|
|
return Pay::wechat()->success();
|
|
} else {
|
|
log_i('支付成功处理失败-' . $handleResult . "\n" . var_export($result, true), 'pay-handle-error', 'wechat');
|
|
}
|
|
} else {
|
|
log_i('支付通知失败' . "\n" . var_export($result, true), 'pay-notify-error', 'wechat');
|
|
}
|
|
break;
|
|
|
|
case 'REFUND.SUCCESS': // 退款成功
|
|
$payData = $result['resource']['ciphertext'];
|
|
if ($payData['refund_status'] == 'SUCCESS') {
|
|
$data = [
|
|
'refund_no' => $payData['out_refund_no'],
|
|
'refund_service_no' => $payData['refund_id'],
|
|
'pay_no' => $payData['out_trade_no'],
|
|
'pay_service_no' => $payData['transaction_id'],
|
|
'refund_amount' => $payData['amount']['refund'] / 100,
|
|
'refund_at' => Carbon::parseFromLocale($payData['success_time'])->toDateTimeString(),
|
|
];
|
|
$handleResult = RefundBill::handleNotify($data);
|
|
if ($handleResult === true) {
|
|
DB::commit();
|
|
return Pay::wechat()->success();
|
|
} else {
|
|
log_i('退款成功处理失败-' . $handleResult . "\n" . var_export($result, true), 'refund-handle-error', 'wechat');
|
|
}
|
|
} else {
|
|
log_i('退款通知失败' . "\n" . var_export($result, true), 'refund-notify-error', 'wechat');
|
|
}
|
|
break;
|
|
|
|
default: // 其他情况
|
|
log_i('其他通知失败' . "\n" . var_export($result, true), 'other-notify-error', 'wechat');
|
|
break;
|
|
}
|
|
} catch (Exception $exception) {
|
|
log_i('通知处理失败-程序出错' . $exception->getMessage() . "\n" . $exception->getTraceAsString() . "\n" . var_export($result, true), 'all-handle-exception', 'wechat');
|
|
}
|
|
|
|
DB::rollBack();
|
|
return new Response(
|
|
500,
|
|
['Content-Type' => 'application/json'],
|
|
json_encode(['code' => 'FAIL', 'message' => '失败']),
|
|
);
|
|
}
|
|
|
|
public function messageWechatMini($config_name)
|
|
{
|
|
$server = app('easywechat.official_account.' . $config_name)->getServer();
|
|
$message = $server->getDecryptedMessage();
|
|
log_s($message);
|
|
return $server->serve();
|
|
}
|
|
}
|