|
|
<?php
namespace App\Http\Controllers\Api;use App\Http\Controllers\Controller;use App\Models\Order;use EasyWeChat\Factory;use EasyWeChat\Kernel\Exceptions\Exception;use Illuminate\Support\Facades\DB;use App\Service\OrderStatus;
class WxpayController extends Controller{ //微信支付 支付结果通知网址
public function notify() { // {"sign": "3F99E6044C503B0E0131F95A1410B630", "appid": "wxb35ef055a4dd8ad4", "mch_id": "1606181693", "openid": "oBYj55W0gLv5MYUnsYUuJfzYzmsg", "cash_fee": "1", "fee_type": "CNY", "time_end": "20210623222330", "bank_type": "OTHERS", "nonce_str": "60d343d820e94", "total_fee": "1", "trade_type": "JSAPI", "result_code": "SUCCESS", "return_code": "SUCCESS", "is_subscribe": "N", "out_trade_no": "2842908479209865216", "transaction_id": "4200001210202106237487333085"}
$config = config('wechat.payment.default'); $app = Factory::payment($config); try { $response = $app->handlePaidNotify(function ($message, $fail) { // 请求成功
if ($message['return_code'] === 'SUCCESS') { $order = Order::query() ->where(['order_no' => $message['out_trade_no']]) ->first();
//已经处理过的订单直接返回true
if ($order && $order->paid_at) { return true; }
// 支付成功
if ($message['result_code'] === 'SUCCESS') { DB::beginTransaction(); try { //TODO 需要判断尾款和全款
$order->status = OrderStatus::PAID; $order->paid_at = time();
$snowflake = resolve('snowflake'); $confirmCode = $snowflake->id(); // TODO 这个码太长后续可以找方案搞短些
$order->confirm_code = isset($params['platform:']) && $params['platform'] ? $params['platform:'] . '|' . $confirmCode : $confirmCode;
$order->save();
// 计算销量
GoodsSpecs::query() ->where([ 'id' => $order->goods_specs_id, 'goods_id' => $order->goods_id, ]) ->update([ 'sold_stock' => DB::raw('sold_stock+' . $order->number . ''), ]);
DB::commit(); return true; } catch (\Exception $e) { DB::rollBack(); $fail('Unknown error'); }
} // 支付失败
elseif ($message['result_code'] === 'FAIL') { $order->state = UserGoodsOrder::PAID_FAIL; $order->save(); return true; } }
// 希望微信重试
$fail('Order not exists.'); }); } catch (Exception $e) { $time = time(); $filename = storage_path('/wxpay/notify/') . date('Y-m-d-H', $time) . '.log'; $data = '[' . date('Y-m-d H:i:s', $time) . ']' . PHP_EOL; $data .= '[message]:' . $e->getMessage() . PHP_EOL . PHP_EOL; file_put_contents($filename, $data, FILE_APPEND); }
return $response; }
//退款通知
public function refund() {
// {"sign": "3F99E6044C503B0E0131F95A1410B630", "appid": "wxb35ef055a4dd8ad4", "mch_id": "1606181693", "openid": "oBYj55W0gLv5MYUnsYUuJfzYzmsg", "cash_fee": "1", "fee_type": "CNY", "time_end": "20210623222330", "bank_type": "OTHERS", "nonce_str": "60d343d820e94", "total_fee": "1", "trade_type": "JSAPI", "result_code": "SUCCESS", "return_code": "SUCCESS", "is_subscribe": "N", "out_trade_no": "2842908479209865216", "transaction_id": "4200001210202106237487333085"}
$config = config('wechat.payment.default'); $app = Factory::payment($config); $response = $app->handleRefundedNotify(function ($message, $reqInfo, $fail) { // 记录一下本地调试
file_put_contents(storage_path('/wxpay/refund'.date('Y-m-d-H').'.log'), date('Y-m-d H:i:s').PHP_EOL.json_encode($message).PHP_EOL.json_encode($reqInfo), FILE_APPEND); // 请求成功
if ($message['return_code'] === 'SUCCESS') { $order = UserGoodsOrder::query() ->where(['order_sn' => $reqInfo['out_trade_no']]) ->first();
// 退款成功
if ($reqInfo['refund_status'] === 'SUCCESS') { DB::beginTransaction(); try {
$apply = RefundApply::query() ->where('refund_sn', $reqInfo['out_refund_no']) ->first();
if (!$apply) { $fail('退款单没找到'); }
$apply->state = 2; $apply->save();
$order->state = UserGoodsOrder::REFUNDED; $order->save();
// 退库存
GoodsSpecs::query() ->where([ 'id' => $order->goods_specs_id, 'goods_id' => $order->goods_id, ]) ->update([ 'stock' => DB::raw('stock+'.$order->number.''), ]);
DB::commit(); return true; } catch (\Exception $e) { DB::rollBack(); $fail('Unknown error'); } } }
$fail('Unknown error'); });
return $response; }}
|