diff --git a/app/Admin/Controllers/OrderController.php b/app/Admin/Controllers/OrderController.php
index 35bb983a..dcee79ec 100644
--- a/app/Admin/Controllers/OrderController.php
+++ b/app/Admin/Controllers/OrderController.php
@@ -93,14 +93,13 @@ class OrderController extends AdminController
10=>'已取消',
])
);
- $grid->column('pay_way')->using(OrderModel::$payWayText)->label([
- 'wxpay'=>'success',
- 'alipay'=>'primary',
- 'balance'=>'warning',
- 'wallet'=>'warning',
- 'offline'=>'danger',
- 'none'=>'#b3b9bf',
- ]);
+ $grid->column('pay_way')->display(function ($v) {
+ if ($v) {
+ return ' '.$v->getMallOrderText();
+ }
+
+ return '';
+ });
$grid->column('pay_at');
// $grid->column('consignee_name');
// $grid->column('consignee_telephone');
@@ -177,14 +176,27 @@ class OrderController extends AdminController
return bcdiv($v, 100, 2);
})->prepend('¥');
$show->field('created_at');
- $show->field('pay_way')->using(OrderModel::$payWayText)->showLabel([
- '微信支付'=>'success',
- '支付宝'=>'primary',
- '钱包'=>'warning',
- '余额'=>'warning',
- '线下支付'=>'danger',
- '无'=>'#b3b9bf',
- ]);
+ $show->html(function () {
+ $content = '';
+
+ if ($this->pay_way) {
+ $content = ' '.$this->pay_way->getMallOrderText();
+ }
+
+ return <<
+
+ 支付方式
+
+
+
+
+HTML;
+ });
$show->field('pay_at');
$show->field('tags')->as(function () {
return $this->tags->pluck('name');
diff --git a/app/Admin/Services/OrderService.php b/app/Admin/Services/OrderService.php
index 4f053ba5..5a22d39d 100644
--- a/app/Admin/Services/OrderService.php
+++ b/app/Admin/Services/OrderService.php
@@ -2,10 +2,10 @@
namespace App\Admin\Services;
+use App\Enums\PayWay;
use App\Exceptions\BizException;
use App\Models\Order;
use App\Models\OrderLog;
-use App\Models\PayLog;
use App\Services\OrderService as EndpointOrderService;
class OrderService
@@ -43,7 +43,7 @@ class OrderService
if ($order->isPending()) {
//操作订单状态-需要调整为统一支付方法
$orderService = new EndpointOrderService();
- $orderService->pay($order, PayLog::PAY_WAY_OFFLINE);
+ $orderService->pay($order, PayWay::Offline);
//记录操作日志
OrderLog::create([
diff --git a/app/Console/Commands/OrderRefundCommand.php b/app/Console/Commands/OrderRefundCommand.php
index 877b8b44..14457001 100644
--- a/app/Console/Commands/OrderRefundCommand.php
+++ b/app/Console/Commands/OrderRefundCommand.php
@@ -43,7 +43,7 @@ class OrderRefundCommand extends Command
OrderRefundLog::pending()->chunkById(200, function ($logs) use (&$page) {
foreach ($logs as $log) {
try {
- $method = 'refundBy'.Str::studly($log->order->pay_way);
+ $method = 'refundBy'.Str::studly($log->order->pay_way->value);
if (! method_exists($this, $method)) {
throw new BizException('退款方式暂不支持');
@@ -79,7 +79,7 @@ class OrderRefundCommand extends Command
* @param \App\Models\OrderRefundLog $log
* @return void
*/
- protected function refundByWxpay(OrderRefundLog $log)
+ protected function refundByWxpayApp(OrderRefundLog $log)
{
$order = $log->order;
@@ -106,7 +106,7 @@ class OrderRefundCommand extends Command
* @param \App\Models\OrderRefundLog $log
* @return void
*/
- protected function refundByAlipay(OrderRefundLog $log)
+ protected function refundByAlipayApp(OrderRefundLog $log)
{
$order = $log->order;
diff --git a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php
index 23828e90..ad7e15c3 100644
--- a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php
+++ b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php
@@ -7,6 +7,7 @@ use App\Endpoint\Api\Http\Controllers\Controller;
use App\Endpoint\Api\Http\Resources\OrderPackageResource;
use App\Endpoint\Api\Http\Resources\OrderResource;
use App\Endpoint\Api\Http\Resources\OrderResourceCollection;
+use App\Enums\PayWay;
use App\Events\OrderPaid;
use App\Exceptions\BizException;
use App\Helpers\Paginator as PaginatorHelper;
@@ -185,12 +186,16 @@ class OrderController extends Controller
'pay_way' => ['bail', 'required'],
]);
+ if (is_null($payway = PayWay::tryFrom($input['pay_way']))) {
+ throw new BizException('支付方式 非法');
+ }
+
$user = $request->user();
- return DB::transaction(function () use ($id, $user, $input) {
+ return DB::transaction(function () use ($id, $user, $payway) {
$order = $user->orders()->findOrFail($id);
- return (new OrderService())->pay($order, $input['pay_way']);
+ return (new OrderService())->pay($order, $payway);
});
}
diff --git a/app/Enums/PayWay.php b/app/Enums/PayWay.php
index a35bc630..27eab37b 100644
--- a/app/Enums/PayWay.php
+++ b/app/Enums/PayWay.php
@@ -10,7 +10,7 @@ enum PayWay: string {
case WxpayApp = 'wxpay_app';
case WxpayH5 = 'wxpay_h5';
case WxpayJs = 'wxpay_jsapi';
- case WxpayMp = 'wxpay_mp';
+ case WxpayMp = 'wxpay_mini_program';
// 阿里支付
case AlipayApp = 'alipay_app';
@@ -18,8 +18,10 @@ enum PayWay: string {
{
return match ($this) {
static::Offline => '#5b69bc',
+ static::Balance => '#dda451',
static::Wallet => '#ff8acc',
static::WxpayApp, static::WxpayH5, static::WxpayJs, static::WxpayMp => '#21b978',
+ static::AlipayApp => '#3085d6',
default => '#ea5455',
};
}
@@ -35,6 +37,18 @@ enum PayWay: string {
};
}
+ public function getMallOrderText()
+ {
+ return match ($this) {
+ static::Offline => '线下',
+ static::Balance => '余额',
+ static::Wallet => '可提',
+ static::WxpayApp, static::WxpayH5, static::WxpayJs, static::WxpayMp => '微信支付',
+ static::AlipayApp => '支付宝',
+ default => 'Unknown',
+ };
+ }
+
public function getDealerOrderText()
{
return match ($this) {
diff --git a/app/Enums/WxpayTradeType.php b/app/Enums/WxpayTradeType.php
new file mode 100644
index 00000000..78c6a8c8
--- /dev/null
+++ b/app/Enums/WxpayTradeType.php
@@ -0,0 +1,25 @@
+ static::App,
+ PayWay::WxpayH5 => static::H5,
+ PayWay::WxpayJs, PayWay::WxpayMp => static::JSAPI,
+ default => null,
+ };
+ }
+}
diff --git a/app/Models/Order.php b/app/Models/Order.php
index 6a0cd238..6521458e 100644
--- a/app/Models/Order.php
+++ b/app/Models/Order.php
@@ -3,6 +3,7 @@
namespace App\Models;
use App\Constants\OrderStatus;
+use App\Enums\PayWay;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
@@ -51,6 +52,7 @@ class Order extends Model
* @var array
*/
protected $casts = [
+ 'pay_way' => PayWay::class,
'pay_at' => 'datetime',
'completed_at' => 'datetime',
'auto_complete_at' => 'datetime',
@@ -93,14 +95,6 @@ class Order extends Model
'is_settlable',
];
- public static $payWayText = [
- self::PAY_WAY_WXPAY => '微信支付',
- self::PAY_WAY_ALIPAY => '支付宝',
- self::PAY_WAY_WALLET => '钱包',
- self::PAY_WAY_BALANCE => '余额',
- self::PAY_WAY_OFFLINE => '线下支付',
- ];
-
/**
* 仅查询支付过期的订单
*/
diff --git a/app/Models/PayLog.php b/app/Models/PayLog.php
index ab1e3286..3f0b269e 100644
--- a/app/Models/PayLog.php
+++ b/app/Models/PayLog.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Enums\PayWay;
use Illuminate\Database\Eloquent\Model;
class PayLog extends Model
@@ -13,15 +14,6 @@ class PayLog extends Model
public const STATUS_SUCCESS = 1; // 成功
public const STATUS_FAILED = 2; // 失败
- public const PAY_WAY_WXPAY_APP = 'wxpay_app'; // 微信支付 - App 支付
- public const PAY_WAY_WXPAY_JSAPI = 'wxpay_jsapi'; // 微信支付 - JSAPI 支付
- public const PAY_WAY_WXPAY_MINI = 'wxpay_mini'; // 微信支付 - 小程序支付
- public const PAY_WAY_WXPAY_H5 = 'wxpay_h5'; // 微信支付 - H5 支付
- public const PAY_ALIPAY_APP = 'alipay_app'; // 支付宝 - app 支付
- public const PAY_WAY_WALLET = 'wallet'; // 钱包付款(可提现)
- public const PAY_WAY_BALANCE = 'balance'; // 余额支付
- public const PAY_WAY_OFFLINE = 'offline'; // 线下支付
-
/**
* @var array
*/
@@ -33,6 +25,7 @@ class PayLog extends Model
* @var array
*/
protected $casts = [
+ 'pay_way' => PayWay::class,
'pay_at' => 'datetime',
];
@@ -72,10 +65,10 @@ class PayLog extends Model
public function isWxpay(): bool
{
return in_array($this->pay_way, [
- static::PAY_WAY_WXPAY_APP,
- static::PAY_WAY_WXPAY_JSAPI,
- static::PAY_WAY_WXPAY_MINI,
- static::PAY_WAY_WXPAY_H5,
+ PayWay::WxpayApp,
+ PayWay::WxpayJs,
+ PayWay::WxpayH5,
+ PayWay::WxpayMp,
]);
}
@@ -87,7 +80,7 @@ class PayLog extends Model
public function isAlipay(): bool
{
return in_array($this->pay_way, [
- static::PAY_ALIPAY_APP,
+ PayWay::AlipayApp,
]);
}
@@ -98,7 +91,7 @@ class PayLog extends Model
*/
public function isOffline(): bool
{
- return $this->pay_way === static::PAY_WAY_OFFLINE;
+ return $this->pay_way === PayWay::Offline;
}
/**
@@ -108,7 +101,7 @@ class PayLog extends Model
*/
public function isWallet(): bool
{
- return $this->pay_way === static::PAY_WAY_WALLET;
+ return $this->pay_way === PayWay::Wallet;
}
/**
@@ -118,6 +111,6 @@ class PayLog extends Model
*/
public function isBalance(): bool
{
- return $this->pay_way === static::PAY_WAY_BALANCE;
+ return $this->pay_way === PayWay::Balance;
}
}
diff --git a/app/Services/Dealer/OrderService.php b/app/Services/Dealer/OrderService.php
index 7ee01cca..1daa9946 100644
--- a/app/Services/Dealer/OrderService.php
+++ b/app/Services/Dealer/OrderService.php
@@ -12,7 +12,6 @@ use App\Models\DealerOrderAllocateLog;
use App\Models\DealerOrderProduct;
use App\Models\DealerProduct;
use App\Models\DealerUserProductLog;
-use App\Models\PayLog;
use App\Models\ShippingAddress;
use App\Models\User;
use App\Services\PayService;
@@ -290,7 +289,7 @@ class OrderService
try {
$payLog = $order->payLogs()->create([
'pay_sn' => serial_number(),
- 'pay_way' => $payWay->value,
+ 'pay_way' => $payWay,
]);
} catch (QueryException $e) {
if (strpos($e->getMessage(), 'Duplicate entry') === false) {
@@ -304,7 +303,7 @@ class OrderService
];
switch ($payLog->pay_way) {
- case PayLog::PAY_WAY_OFFLINE:
+ case PayWay::Offline:
(new PayService())->handleSuccess($payLog, [
'pay_image' => $payImage,
'pay_info' => $order->getConsignorPayInfo() ?? null,
@@ -315,7 +314,7 @@ class OrderService
break;
- case PayLog::PAY_WAY_WALLET:
+ case PayWay::Wallet:
$walletService = new WalletService();
// 扣除打款人余额
@@ -346,7 +345,7 @@ class OrderService
break;
- case PayLog::PAY_WAY_WXPAY_H5:
+ case PayWay::WxpayH5:
$app = EasyWeChatFactory::payment(config('wechat.payment.yzk'));
$data = (new WeChatPayService($app))->pay([
diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php
index a015a248..58bcb29c 100644
--- a/app/Services/OrderService.php
+++ b/app/Services/OrderService.php
@@ -5,12 +5,13 @@ namespace App\Services;
use App\Endpoint\Api\Http\Resources\ProductSkuSimpleResource;
use App\Endpoint\Api\Http\Resources\ShippingAddressResource;
use App\Endpoint\Api\Http\Resources\UserCouponResource;
+use App\Enums\PayWay;
+use App\Enums\WxpayTradeType;
use App\Exceptions\BizException;
use App\Exceptions\ShippingNotSupportedException;
use App\Models\DistributionPreIncomeJob;
use App\Models\Order;
use App\Models\OrderProduct;
-use App\Models\PayLog;
use App\Models\ProductGift;
use App\Models\ProductSku;
use App\Models\ShippingAddress;
@@ -145,7 +146,7 @@ class OrderService
$coupon?->markAsUse();
if ($order->total_amount === 0) {
- $this->pay($order, PayLog::PAY_WAY_BALANCE);
+ $this->pay($order, PayWay::Balance);
$order->refresh();
}
@@ -753,12 +754,12 @@ class OrderService
* 订单付款
*
* @param \App\Models\Order $order
- * @param string $payWay
+ * @param \App\Enums\PayWay $payWay
* @return mixed
*
* @throws \App\Exceptions\WeChatPayException
*/
- public function pay(Order $order, string $payWay)
+ public function pay(Order $order, PayWay $payWay)
{
if (! $order->isPending()) {
throw new BizException('订单状态不是待付款');
@@ -791,15 +792,15 @@ class OrderService
$data = null;
} elseif ($payLog->isWxpay()) {
- if (! isset(WeChatPayService::$tradeTypes[$payLog->pay_way])) {
- throw new BizException('支付方式不支持');
+ if (is_null($tradeType = WxpayTradeType::tryFromPayWay($payLog->pay_way))) {
+ throw new BizException('支付方式 非法');
}
$data = (new WeChatPayService())->pay([
'body' => app_settings('app.app_name').'-商城订单',
'out_trade_no' => $payLog->pay_sn,
'total_fee' => $order->total_amount,
- 'trade_type' => WeChatPayService::$tradeTypes[$payLog->pay_way],
+ 'trade_type' => $tradeType->value,
]);
} elseif ($payLog->isAlipay()) {
$data = app(AlipayService::class)->pay([
diff --git a/app/Services/PayService.php b/app/Services/PayService.php
index a7e6abc2..fb7e4183 100644
--- a/app/Services/PayService.php
+++ b/app/Services/PayService.php
@@ -66,26 +66,9 @@ class PayService
throw new BizException('订单已完成');
}
- // 支付方式
- $payWay = $payLog->pay_way;
-
- if ($payLog->isWxpay()) {
- // 微信支付
- $payWay = Order::PAY_WAY_WXPAY;
- } elseif ($payLog->isWallet()) {
- // 可提支付
- $payWay = Order::PAY_WAY_WALLET;
- } elseif ($payLog->isBalance()) {
- // 余额支付
- $payWay = Order::PAY_WAY_BALANCE;
- } elseif ($payLog->isAlipay()) {
- // 支付宝
- $payWay = Order::PAY_WAY_ALIPAY;
- }
-
$payable->update([
'pay_sn' => $payLog->pay_sn,
- 'pay_way' => $payWay,
+ 'pay_way' => $payLog->pay_way,
'pay_at' => $payLog->pay_at,
'out_trade_no' => $payLog->out_trade_no,
'status' => Order::STATUS_PAID,
diff --git a/app/Services/WeChatPayService.php b/app/Services/WeChatPayService.php
index a4289169..978432d1 100644
--- a/app/Services/WeChatPayService.php
+++ b/app/Services/WeChatPayService.php
@@ -3,7 +3,6 @@
namespace App\Services;
use App\Exceptions\WeChatPayException;
-use App\Models\PayLog;
use Closure;
use EasyWeChat\Factory;
use EasyWeChat\Payment\Application;
@@ -20,16 +19,6 @@ class WeChatPayService
public const TRADE_TYPE_NATIVE = 'NATIVE'; // Native支付
public const TRADE_TYPE_H5 = 'MWEB'; // H5支付
- /**
- * @var array
- */
- public static $tradeTypes = [
- PayLog::PAY_WAY_WXPAY_APP => self::TRADE_TYPE_APP,
- PayLog::PAY_WAY_WXPAY_JSAPI => self::TRADE_TYPE_JSAPI,
- PayLog::PAY_WAY_WXPAY_MINI => self::TRADE_TYPE_JSAPI,
- PayLog::PAY_WAY_WXPAY_H5 => self::TRADE_TYPE_H5,
- ];
-
/**
* @var \EasyWeChat\Payment\Application
*/
@@ -66,10 +55,6 @@ class WeChatPayService
$params['trade_type'] = static::TRADE_TYPE_APP;
}
- if (! in_array($params['trade_type'], static::$tradeTypes)) {
- throw new WeChatPayException(sprintf('交易类型 [%s] 暂不支持', $params['trade_type']), $params);
- }
-
$result = $this->app->order->unify($params);
if (data_get($result, 'return_code') !== 'SUCCESS') {