Browse Source

变量名小修改

dev
李可松 4 years ago
parent
commit
e32677e7ca
  1. 12
      app/Http/Controllers/Api/OrderController.php
  2. 2
      app/Http/Controllers/Api/VerificationController.php
  3. 27
      app/Http/Controllers/Api/WxpayController.php
  4. 2
      app/Models/Order.php
  5. 2
      app/Service/OrderStatus.php
  6. 4
      routes/api.php

12
app/Http/Controllers/Api/OrderController.php

@ -157,7 +157,7 @@ class OrderController extends Controller
if ($formData['pay_type'] == 3) { //线下支付 if ($formData['pay_type'] == 3) { //线下支付
return $this->success('操作成功,请及时联系客服线下支付'); return $this->success('操作成功,请及时联系客服线下支付');
} else { //在线支付或定金支付 } else { //在线支付或定金支付
$config = $this->payConfig($order->title, $order->order_no, $order->pay_type, $price);
$config = $this->payConfig($order->title, $order->order_no, $order->status, $price);
if (!empty($config['paySign'])) { if (!empty($config['paySign'])) {
return $this->success($config); return $this->success($config);
} else { } else {
@ -188,7 +188,7 @@ class OrderController extends Controller
//TODO 需要后台处理 //TODO 需要后台处理
$order = Order::find($formData['id']); $order = Order::find($formData['id']);
if (!in_array($order->status, [Status::PAY_EARNEST, Status::PAID, Status::PAY_RETAINAGE])) {
if (!in_array($order->status, [Status::PAY_EARNEST, Status::PAID, Status::PAID_RETAINAGE])) {
return $this->error('当前订单状态不允许退款'); return $this->error('当前订单状态不允许退款');
} }
$order->status = Status::REFUNDING; $order->status = Status::REFUNDING;
@ -253,7 +253,7 @@ class OrderController extends Controller
$price = $this->calc($order['price'], $coupon, $order['num'], $order['pay_type']); $price = $this->calc($order['price'], $coupon, $order['num'], $order['pay_type']);
} }
$config = $this->payConfig($order['title'], $order['order_no'], $order['pay_type'], $price);
$config = $this->payConfig($order['title'], $order['order_no'], $order['status'], $price);
if (!empty($config['paySign'])) { if (!empty($config['paySign'])) {
return $this->success($config); return $this->success($config);
} else { } else {
@ -262,7 +262,7 @@ class OrderController extends Controller
} }
//获取支付配置信息 //获取支付配置信息
private function payConfig($title, $order_no, $pay_type, $price)
private function payConfig($title, $order_no, $status, $price)
{ {
//用户openid //用户openid
$openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG $openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG
@ -280,9 +280,9 @@ class OrderController extends Controller
$app = Factory::payment($config); $app = Factory::payment($config);
$result = $app->order->unify([ $result = $app->order->unify([
'body' => $title, 'body' => $title,
'out_trade_no' => $order_no . '-' . $pay_type, //后面加pay_type,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
'out_trade_no' => $order_no . '-' . $status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
'total_fee' => round($price * 100), //支付金额单位为分 'total_fee' => round($price * 100), //支付金额单位为分
'notify_url' => route('wxpay_notify', ['aid' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', 'trade_type' => 'JSAPI',
'openid' => $openid, 'openid' => $openid,
]); ]);

2
app/Http/Controllers/Api/VerificationController.php

@ -22,7 +22,7 @@ class VerificationController extends Controller
if (!$order) { if (!$order) {
return $this->error('订单不存在或无权限'); return $this->error('订单不存在或无权限');
} }
if (!in_array($order->status, [OrderStatus::PAID, OrderStatus::PAY_RETAINAGE, OrderStatus::OFFLINE_UNPAID])) {
if (!in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_UNPAID])) {
return $this->error('当前订单状态不允许核销'); return $this->error('当前订单状态不允许核销');
} }
$order->status = 16; $order->status = 16;

27
app/Http/Controllers/Api/WxpayController.php

@ -1,7 +1,6 @@
<?php <?php
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Agent; use App\Models\Agent;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\Order; use App\Models\Order;
@ -20,7 +19,7 @@ class WxpayController
{ {
// {"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"} // {"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"}
$agent_id = request()->route('aid');
$agent_id = request()->route('agent_id');
$agent = Agent::find($agent_id); $agent = Agent::find($agent_id);
$config = config('wechat.payment.default'); $config = config('wechat.payment.default');
@ -41,7 +40,7 @@ class WxpayController
->first(); ->first();
//已经处理过的订单直接返回true //已经处理过的订单直接返回true
if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAY_RETAINAGE])) {
if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE])) {
return true; return true;
} }
@ -56,7 +55,7 @@ class WxpayController
if ($pay_type == OrderStatus::UNPAID) { if ($pay_type == OrderStatus::UNPAID) {
$order->status = OrderStatus::PAY_EARNEST; $order->status = OrderStatus::PAY_EARNEST;
} else if ($status == OrderStatus::PAY_EARNEST) { } else if ($status == OrderStatus::PAY_EARNEST) {
$order->status = OrderStatus::PAY_RETAINAGE;
$order->status = OrderStatus::PAID_RETAINAGE;
} }
} else if ($pay_type == 0) { } else if ($pay_type == 0) {
$order->status = OrderStatus::PAID; $order->status = OrderStatus::PAID;
@ -88,7 +87,7 @@ class WxpayController
DB::commit(); DB::commit();
return true; return true;
} catch (\Exception $e) {
} catch (Exception $e) {
DB::rollBack(); DB::rollBack();
$fail('Unknown error'); $fail('Unknown error');
} }
@ -101,7 +100,7 @@ class WxpayController
// 希望微信重试 // 希望微信重试
$fail('Order not exists.'); $fail('Order not exists.');
}); });
} catch (InvalidSignException | \Exception $e) {
} catch (InvalidSignException | Exception $e) {
$time = time(); $time = time();
$filename = storage_path('logs/wxpay_notify_') . date('Y-m-d-H', $time) . '.log'; $filename = storage_path('logs/wxpay_notify_') . date('Y-m-d-H', $time) . '.log';
$data = '[' . date('Y-m-d H:i:s', $time) . ']' . PHP_EOL; $data = '[' . date('Y-m-d H:i:s', $time) . ']' . PHP_EOL;
@ -119,11 +118,20 @@ class WxpayController
// {"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"} // {"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"}
$agent_id = request()->route('agent_id');
$agent = Agent::find($agent_id);
$config = config('wechat.payment.default'); $config = config('wechat.payment.default');
$config = array_merge($config, [
'app_id' => $agent->appid,
'mch_id' => $agent->mchid,
'key' => $agent->mchkey,
]);
$app = Factory::payment($config); $app = Factory::payment($config);
try {
$response = $app->handleRefundedNotify(function ($message, $reqInfo, $fail) { $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);
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') { if ($message['return_code'] === 'SUCCESS') {
//订单号带有pay_type后缀,主要是为了区分定金支付和尾款支付,前24位才是真正的订单号 //订单号带有pay_type后缀,主要是为了区分定金支付和尾款支付,前24位才是真正的订单号
@ -158,7 +166,7 @@ class WxpayController
'goods_id' => $order->goods_id, 'goods_id' => $order->goods_id,
]) ])
->update([ ->update([
'stock' => DB::raw('stock+'.$order->number.''),
'stock' => DB::raw('stock+' . $order->number . ''),
]); ]);
DB::commit(); DB::commit();
@ -172,6 +180,9 @@ class WxpayController
$fail('Unknown error'); $fail('Unknown error');
}); });
} catch (Exception $e) {
return 'error';
}
return $response; return $response;
} }

2
app/Models/Order.php

@ -29,7 +29,7 @@ class Order extends BaseModel
Status::UNPAID => '待付款', Status::UNPAID => '待付款',
Status::PAY_EARNEST => '已付定金', Status::PAY_EARNEST => '已付定金',
Status::PAID => '已付款', Status::PAID => '已付款',
Status::PAY_RETAINAGE => '已付尾款',
Status::PAID_RETAINAGE => '已付尾款',
Status::REFUNDING => '退款中', Status::REFUNDING => '退款中',
Status::REFUNDED => '已退款', Status::REFUNDED => '已退款',
Status::SUCCESS => '已完成', Status::SUCCESS => '已完成',

2
app/Service/OrderStatus.php

@ -13,7 +13,7 @@ class OrderStatus
const UNPAID = 0; //待付款 const UNPAID = 0; //待付款
const PAY_EARNEST = 1; //已付定金 const PAY_EARNEST = 1; //已付定金
const PAID = 2; //已付全款 const PAID = 2; //已付全款
const PAY_RETAINAGE = 3; //已付尾款
const PAID_RETAINAGE = 3; //已付尾款
const OFFLINE_UNPAID= 4; //线下支付未付款 const OFFLINE_UNPAID= 4; //线下支付未付款
//const OFFLINE_PAID = 5; //线下支付已收款 //const OFFLINE_PAID = 5; //线下支付已收款
const REFUNDING = 6; //退款中 const REFUNDING = 6; //退款中

4
routes/api.php

@ -67,8 +67,8 @@ Route::namespace('App\Http\Controllers\Api')->group(function () {
# 微信支付 # 微信支付
Route::prefix('wxpay')->group(function () { Route::prefix('wxpay')->group(function () {
Route::post('notify/{aid}', 'WxpayController@notify')->name('wxpay_notify'); //异步通知,aid为代理商ID
Route::post('refund/{aid}', 'WxpayController@refund')->name('wxpay_refund'); //退款通知,aid为代理商ID
Route::post('notify/{agent_id}', 'WxpayController@notify')->name('wxpay_notify'); //异步通知,aid为代理商ID
Route::post('refund/{agent_id}', 'WxpayController@refund')->name('wxpay_refund'); //退款通知,aid为代理商ID
}); });
# 仅用于测试 # 仅用于测试

Loading…
Cancel
Save