|
@@ -1,11 +1,18 @@
|
|
|
<?php
|
|
|
|
|
|
-namespace app\common\Service;
|
|
|
+namespace app\common\Service\Pay;
|
|
|
|
|
|
|
|
|
use think\Db;
|
|
|
use think\Exception;
|
|
|
use app\common\model\User;
|
|
|
+use app\common\exception\BusinessException;
|
|
|
+use app\common\Enum\OrderEnum;
|
|
|
+use app\common\Service\OrderService;
|
|
|
+use addons\epay\library\Service;
|
|
|
+use Yansongda\Pay\Exceptions\GatewayException;
|
|
|
+use app\common\Service\Order\OrderGoodsService;
|
|
|
+use app\common\model\OrderAction;
|
|
|
/**
|
|
|
* 支付服务类
|
|
|
* 封装订单创建相关逻辑
|
|
@@ -13,6 +20,72 @@ use app\common\model\User;
|
|
|
class PayOperService
|
|
|
{
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @ 支付
|
|
|
+ * @param string $orderid
|
|
|
+ * @param int $user_id
|
|
|
+ * @param string $paytype
|
|
|
+ * @param string $method
|
|
|
+ * @param string $openid
|
|
|
+ * @param string $notifyurl
|
|
|
+ * @param string $returnurl
|
|
|
+ * @return \addons\epay\library\Collection|\addons\epay\library\RedirectResponse|\addons\epay\library\Response|null
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public static function pay($orderSn = "", $userId = 0, $paytype="", $method = '', $openid = '', $notifyurl = null, $returnurl = null)
|
|
|
+ {
|
|
|
+ $request = \think\Request::instance();
|
|
|
+ $order = OrderService::getDetailByOrderSn($orderSn);
|
|
|
+ if (!$order) {
|
|
|
+ throw new BusinessException('订单不存在!');
|
|
|
+ }
|
|
|
+ if (!$order->canPayHandle()) {
|
|
|
+ throw new BusinessException('该订单无法支付!');
|
|
|
+ }
|
|
|
+
|
|
|
+ //支付金额为0,无需支付
|
|
|
+ if ($order->amount == 0) {
|
|
|
+ // 走orderService 更新订单状态方法 穿订单状态 已支付 和支付时间
|
|
|
+ $Order = OrderService::updateOrderStatus($order->id, $userId, OrderEnum::STATUS_PAY);
|
|
|
+ if ($Order) {
|
|
|
+ return $Order;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $order_sn = $order->order_sn;
|
|
|
+
|
|
|
+ $order->allowField(true)->save(['pay_type' => $paytype, 'open_id' => $openid]);
|
|
|
+
|
|
|
+ $response = null;
|
|
|
+ $epay = get_addon_info('epay');
|
|
|
+
|
|
|
+ if ($epay && $epay['state']) {
|
|
|
+ $notifyurl = $notifyurl ? $notifyurl : $request->root(true) . '/api/shop/pay/notify/type/notify/paytype/' . $paytype;
|
|
|
+ $returnurl = $returnurl ? $returnurl : $request->root(true) . '/api/shop/pay/notify/type/return/paytype/' . $paytype . '/order_sn/' . $order_sn;
|
|
|
+
|
|
|
+ //保证取出的金额一致,不一致将导致订单重复错误
|
|
|
+ $amount = sprintf("%.2f", $order->amount);
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'orderid' => $order_sn,
|
|
|
+ 'type' => $paytype,
|
|
|
+ 'title' => "支付{$amount}元",
|
|
|
+ 'notifyurl' => $notifyurl,
|
|
|
+ 'returnurl' => $returnurl,
|
|
|
+ 'method' => $method,
|
|
|
+ 'openid' => $openid
|
|
|
+ ];
|
|
|
+ try {
|
|
|
+ $response = Service::submitOrder($params);
|
|
|
+ } catch (GatewayException $e) {
|
|
|
+ throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败,请稍后重试");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new \Exception("请在后台安装配置微信支付宝整合插件");
|
|
|
+ }
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
/**
|
|
|
*
|
|
|
* @ 订单结算
|
|
@@ -21,40 +94,29 @@ class PayOperService
|
|
|
* @param string $transactionid 流水号
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public static function settle($order_sn, $payamount, $transactionid = '')
|
|
|
+ public static function settle($orderSn, $payamount, $transactionid = '')
|
|
|
{
|
|
|
- $order = self::with(['orderGoods'])->where('order_sn', $order_sn)->find();
|
|
|
- if (!$order || $order->paystate == 1) {
|
|
|
+ $order = OrderService::getDetailByOrderSn($orderSn);
|
|
|
+ if (!$order->canPayHandle()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if ($payamount != $order->saleamount) {
|
|
|
- \think\Log::write("[shop][pay][{$order_sn}][订单支付金额不一致]");
|
|
|
+ if ($payamount != $order->amount) {
|
|
|
+ \think\Log::write("支付失败,订单支付金额不一致[{$orderSn}][{$payamount}][{$order->amount}]");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 启动事务
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
- $order->paystate = 1;
|
|
|
- $order->transactionid = $transactionid;
|
|
|
- $order->payamount = $payamount;
|
|
|
- $order->paytime = time();
|
|
|
- $order->paytype = !$order->paytype ? 'system' : $order->paytype;
|
|
|
- $order->method = !$order->method ? 'system' : $order->method;
|
|
|
+ $order->status = OrderEnum::STATUS_PAY;
|
|
|
+ // $order->transaction_id = $transactionid;
|
|
|
+ $order->pay_amount = $payamount;
|
|
|
+ $order->pay_time = time();
|
|
|
$order->save();
|
|
|
- if ($order->payamount == $order->saleamount) {
|
|
|
+ if ($order->pay_amount == $order->amount) {
|
|
|
//支付完成后,商品销量+1
|
|
|
- foreach ($order->order_goods as $item) {
|
|
|
- $goods = $item->goods;
|
|
|
- $sku = $item->sku;
|
|
|
- if ($goods) {
|
|
|
- $goods->setInc('sales', $item->nums);
|
|
|
- }
|
|
|
- if ($sku) {
|
|
|
- $sku->setInc('sales', $item->nums);
|
|
|
- }
|
|
|
- }
|
|
|
+ OrderGoodsService::setGoodsSalesInc($orderSn);
|
|
|
}
|
|
|
// 提交事务
|
|
|
Db::commit();
|
|
@@ -64,7 +126,7 @@ class PayOperService
|
|
|
return false;
|
|
|
}
|
|
|
//记录操作
|
|
|
- OrderAction::push($order_sn, '系统', '订单支付成功');
|
|
|
+ OrderAction::push($orderSn, '系统', '订单支付成功');
|
|
|
return true;
|
|
|
}
|
|
|
|