|
|
<?php
namespace App\Http\Controllers\Api;use App\Models\Agent;use App\Models\AgentProduct;use App\Models\Order;use App\Models\Product;use App\Models\UserMoneyLog;use EasyWeChat\Factory;use EasyWeChat\Kernel\Exceptions\Exception;use EasyWeChat\Payment\Kernel\Exceptions\InvalidSignException;use Illuminate\Support\Facades\DB;use App\Common\OrderStatus;
class WxpayController{ //微信支付 支付结果通知网址
public function notify() { $agent_id = request()->route('agent_id'); $agent = Agent::find($agent_id);
$config = config('wechat.payment.default'); $config = array_merge($config, [ 'app_id' => $agent->appid, 'mch_id' => $agent->mchid, 'key' => $agent->mchkey, ]); $app = Factory::payment($config); try { $response = $app->handlePaidNotify(function ($message, $fail) { $this->log($message); // 请求成功
if ($message['return_code'] === 'SUCCESS') { //订单号带有status后缀,主要是为了区分定金支付和尾款支付,前24位才是真正的订单号
$order_no = substr($message['out_trade_no'], 0, 24); $order = Order::query() ->where(['order_no' => $order_no]) ->first();
//已经处理过的订单直接返回true
if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::SUCCESS])) { return true; }
//判断该微信支付订单号有没有处理过
$exist_log = UserMoneyLog::where([ 'user_id' => $order->user_id, 'order_id' => $order->id, 'type' => 1, 'transaction_id' => $message['transaction_id'], ])->first(); if ($exist_log) { return true; }
// 支付成功
if ($message['result_code'] === 'SUCCESS') { DB::beginTransaction(); try { $status = $order->status; $pay_type = $order->pay_type; $money = $message['total_fee'] / 100; //定金支付和首付款支付
if (in_array($pay_type, [1, 2])) { if ($status == OrderStatus::UNPAID) { $order->status = OrderStatus::PAY_EARNEST; } else if ($status == OrderStatus::PAY_EARNEST) { //只有支付的金额大于等于订单总价,才将订单状态修改为PAID_RETAINAGE
if ($order->paid_money + $money >= $order->price) { $order->status = OrderStatus::PAID_RETAINAGE; } } } else if ($pay_type == 0) { $order->status = OrderStatus::PAID; }
$order->paid_at = time(); $order->paid_money = DB::raw('`paid_money` + ' . $money); $order->save();
//增加销量,库存在拍下时已经减了
AgentProduct::query() ->where('id', $order->agent_product_id) ->increment('sale', $order->num); Product::query() ->where('id', $order->product_id) ->increment('sale', $order->num);
//资金流水
UserMoneyLog::query()->create([ 'user_id' => $order->user_id, 'agent_id' => $order->agent_id, 'money' => -$money, 'order_id' => $order->id, 'type' => 1, 'desc' => DB::raw("LEFT('购买产品:{$order->title}', 250)"), 'transaction_id' => $message['transaction_id'], //微信支付订单号
'created_at' => time(), //模型没有updated_at,无法自动写入时间
]);
DB::commit(); return true; } catch (Exception $e) { DB::rollBack(); $fail('Unknown error'); } } // 支付失败
else if ($message['result_code'] === 'FAIL') { return true; } }
// 希望微信重试
$fail('Unknown error 2'); }); } catch (InvalidSignException | Exception $e) { $this->log($e->getMessage() . $e->getFile() . $e->getLine()); return 'error'; }
return $response; }
//退款通知
public function refund() { $agent_id = request()->route('agent_id'); $agent = Agent::find($agent_id);
$config = config('wechat.payment.default'); $config = array_merge($config, [ 'app_id' => $agent->appid, 'mch_id' => $agent->mchid, 'key' => $agent->mchkey, ]); $app = Factory::payment($config); try { $response = $app->handleRefundedNotify(function ($message, $reqInfo, $fail) { // 记录一下本地调试
$this->log(['message' => $message, 'reqInfo' => $reqInfo], 'refund'); // 请求成功
if ($message['return_code'] === 'SUCCESS') { //订单号带有status后缀,主要是为了区分定金支付和尾款支付,前24位才是真正的订单号
$order_no = substr($reqInfo['out_trade_no'], 0, 24); $order = Order::query() ->where(['order_no' => $order_no]) ->first();
//如果已经处理过则不再处理
if ($order->status == OrderStatus::REFUNDED) { return true; }
//如果已经存在相关的退款记录,也不再处理
$exist_log = UserMoneyLog::where([ 'user_id' => $order->user_id, 'order_id' => $order->id, 'type' => 2, 'transaction_id' => $reqInfo['transaction_id'], ])->first(); if ($exist_log) { return true; }
$log = UserMoneyLog::query() ->where([ 'user_id' => $order->user_id, 'order_id' => $order->id, 'type' => 1, 'transaction_id' => $reqInfo['transaction_id'], ])->first();
if (!$log) { $fail('找不到交易信息'); }
// 退款成功
if ($reqInfo['refund_status'] === 'SUCCESS') { DB::beginTransaction(); try { //更新订单状态为退款成功
$order->status = OrderStatus::REFUNDED; $order->save(); //记录日志
UserMoneyLog::create([ 'user_id' => $order->user_id, 'agent_id' => $order->agent_id, 'money' => $reqInfo['settlement_refund_fee'] / 100, 'order_id' => $order->id, 'type' => 2, 'desc' => DB::raw("LEFT('退款:{$order->title}', 250)"), 'transaction_id' => $reqInfo['transaction_id'], 'created_at' => time(), ]);
// 退库存
Product::query() ->where('id', $order->product_id) ->increment('stock', $order->num);
DB::commit(); return true; } catch (\Exception $e) { DB::rollBack(); $fail('Unknown error'); } } }
$fail('Unknown error 2'); }); } catch (Exception $e) { return 'error'; }
return $response; }
//保存消息,用于调试
private function log($write_data, $type = 'notify') { $dir = storage_path('wxpay'); if (!is_dir($dir)) { mkdir($dir); } $filename = $dir . '/' . $type . '_' . date('Y-m-d-H') . '.log'; $data = '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL; $data .= '[message]: ' . (is_array($write_data) ? json_encode($write_data) : $write_data) . PHP_EOL . PHP_EOL; file_put_contents($filename, $data, FILE_APPEND); }}
|