Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
service
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
aimeiyue
service
Commits
75115354
Commit
75115354
authored
Apr 27, 2025
by
yink
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2025-04-27更新分账逻辑备份
parent
53940f55
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
356 additions
and
745 deletions
+356
-745
app/Http/Controllers/Api/OrderController copy.php
+0
-683
app/Http/Controllers/Api/OrderDivideRecordController.php
+52
-12
app/Models/Adapay.php
+110
-48
app/Models/OrderDivideRecord-分佣备份.php
+191
-0
app/Models/OrderDivideRecord.php
+2
-2
app/Models/OrderInfo.php
+1
-0
No files found.
app/Http/Controllers/Api/OrderController copy.php
deleted
100644 → 0
View file @
53940f55
<?php
namespace
App\Http\Controllers\Api
;
use
App\Command\Log
;
use
App\Models\Pay
;
use
App\Models\UserAddress
;
use
App\Models\Store
;
use
App\Models\Good
as
GoodModel
;
use
App\Models\GoodSku
;
use
App\Models\OrderGoods
;
use
App\Models\OrderInfo
as
OrderInfoModel
;
use
App\Models\MerchantGoodSku
;
use
Dcat\Admin\Grid\Displayers\Orderable
;
use
Illuminate\Http\Request
;
use
Illuminate\Support\Facades\DB
;
use
League\CommonMark\Node\Query\OrExpr
;
class
OrderController
extends
BaseController
{
//立即购买-确认订单
public
function
CheckoutBuyOrder
(
Request
$request
)
{
$userObj
=
$request
->
user
();
$merchant_id
=
$userObj
->
merchant_id
??
0
;
$goods_id
=
$request
->
goods_id
;
$num
=
$request
->
num
??
1
;
$attr_name
=
$request
->
attr_name
??
''
;
$goodsObj
=
GoodModel
::
find
(
$goods_id
);
if
(
!
$goodsObj
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
$attr_val
=
''
;
$goods_price
=
0
;
if
(
$attr_name
)
{
$attrArr
=
explode
(
","
,
$attr_name
);
utf8_array_asort
(
$attrArr
);
$attrStr
=
join
(
"、"
,
$attrArr
);
$attr_sn
=
md5
(
$attrStr
);
$arrObj
=
GoodSku
::
where
(
'goods_id'
,
$goods_id
)
->
where
(
'attr_sn'
,
$attr_sn
)
->
first
();
$attr_val
=
$arrObj
?
$arrObj
->
attr_val
:
''
;
$attr_id
=
$arrObj
?
$arrObj
->
id
:
0
;
$goods_price
=
(
$arrObj
&&
$goodsObj
->
goods_type
&&
$merchant_id
)
?
$arrObj
->
cg_price
:
$arrObj
->
market_price
;
}
$data
=
[];
$data
[
'goods_id'
]
=
$goods_id
;
$data
[
'goods_name'
]
=
$goodsObj
->
goods_name
;
$data
[
'goods_img'
]
=
$goodsObj
->
cover_img
?
togetherFilePath
(
$goodsObj
->
cover_img
)
:
''
;
$data
[
'num'
]
=
$num
;
$data
[
'attr_id'
]
=
$attr_id
;
$data
[
'goods_price'
]
=
$goods_price
;
$data
[
'attr_name'
]
=
$attr_val
;
return
$this
->
JsonResponse
(
$data
);
}
//创建订单--立即购买
public
function
CreateBuyOrder
(
Request
$request
)
{
$orderObj
=
new
OrderInfoModel
();
$userObj
=
$request
->
user
();
$user_id
=
$userObj
->
id
;
$merchant_id
=
(
int
)
$userObj
->
merchant_id
;
$goods_id
=
$request
->
goods_id
;
$num
=
$request
->
num
??
1
;
$attr_id
=
$request
->
attr_id
??
0
;
$delivery_type
=
$request
->
delivery_type
;
// 收货方式 1:代收点 2:送货上门
$store_id
=
$request
->
store_id
??
0
;
//代收点 门店ID
//----收货地址----
$address
=
$request
->
address
??
''
;
//详情地址
$area
=
$request
->
area
??
''
;
$phone
=
$request
->
phone
?
$request
->
phone
:
$userObj
->
phone
;
$consignee
=
$request
->
consignee
??
''
;
//$address_id = $request->address_id ?? 0; //地址ID
$goodsObj
=
GoodModel
::
find
(
$goods_id
);
if
(
!
$goodsObj
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
// $userAddress = '';
// if ($address_id) {
// $userAddress = UserAddress::where("id", $address_id)->first();
// }
$order_sn
=
$this
->
getOrderSn
();
DB
::
beginTransaction
();
try
{
$total_price
=
0
;
$orderGoods
=
[];
//商品信息
$goodObj
=
GoodModel
::
find
(
$goods_id
);
$good_price
=
0
;
//商品单价
//判断是否有规格
$attr_val
=
''
;
if
(
$attr_id
)
{
$arrObj
=
GoodSku
::
where
([
'id'
=>
$attr_id
,
'goods_id'
=>
$goods_id
])
->
first
();
if
(
!
$arrObj
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
else
{
if
(
$arrObj
->
stock
<
$num
)
{
return
$this
->
JsonResponse
(
''
,
'该商品库存不足'
,
500
);
}
}
$attr_val
=
$arrObj
?
$arrObj
->
attr_val
:
''
;
$good_price
=
(
$arrObj
&&
$goodsObj
->
goods_type
&&
$merchant_id
)
?
$arrObj
->
cg_price
:
$arrObj
->
market_price
;
}
//商品总价
$total_price
=
(
$good_price
*
$num
);
$tmp
=
[];
$tmp
[
'goods_id'
]
=
$goods_id
;
$tmp
[
'goods_number'
]
=
$num
;
$tmp
[
'goods_price'
]
=
$good_price
;
$tmp
[
'goods_attr'
]
=
$attr_val
;
$tmp
[
'attr_id'
]
=
$attr_id
;
$tmp
[
'goods_name'
]
=
$goodObj
->
goods_name
;
$tmp
[
'goods_img'
]
=
$goodObj
->
cover_img
;
$tmp
[
'merchant_id'
]
=
$merchant_id
;
$tmp
[
'created_at'
]
=
date
(
"Y-m-d H:i:s"
);
array_push
(
$orderGoods
,
$tmp
);
//订单信息
$orderObj
->
order_sn
=
$order_sn
;
$orderObj
->
user_id
=
$user_id
;
$orderObj
->
area
=
$area
;
$orderObj
->
address
=
$address
;
$orderObj
->
consignee
=
$consignee
;
$orderObj
->
mobile
=
$phone
;
$orderObj
->
goods_amount
=
$total_price
;
$orderObj
->
order_amount
=
$total_price
;
$orderObj
->
goods_sn
=
$goodObj
->
goods_sn
;
$orderObj
->
delivery_type
=
$delivery_type
;
$orderObj
->
merchant_id
=
$merchant_id
;
$orderObj
->
store_id
=
$store_id
;
if
(
$orderObj
->
save
())
{
$order_id
=
$orderObj
->
id
;
foreach
(
$orderGoods
as
$key
=>
$item
)
{
$orderGoods
[
$key
][
'order_id'
]
=
$order_id
;
}
DB
::
table
(
"li_order_goods"
)
->
insert
(
$orderGoods
);
}
DB
::
commit
();
//15分钟取消订单
//$this->dispatch(new CancelOrder($orderObj, 900));
return
$this
->
JsonResponse
([
'order_id'
=>
$orderObj
->
id
]);
}
catch
(
\Exception
$exception
)
{
Log
::
add
(
'创建预支付订单失败'
,
$exception
->
getMessage
());
DB
::
rollBack
();
return
$this
->
JsonResponse
(
''
,
'创建预支付订单失败'
,
201
);
}
}
//创建订单
public
function
CreateOrder
(
Request
$request
)
{
$orderObj
=
new
OrderInfoModel
();
$userObj
=
$request
->
user
();
$user_id
=
$userObj
->
id
;
$merchant_id
=
$userObj
->
merchant_id
;
$catKey
=
$request
->
catKey
??
''
;
//购物车KeyID拼接 5_1,6_2
$delivery_type
=
$request
->
delivery_type
;
// 收货方式 1:代收点 2:送货上门
$store_id
=
$request
->
store_id
??
0
;
//代收点 门店ID
//----收货地址----
$address
=
$request
->
address
??
''
;
//详情地址
$area
=
$request
->
area
??
''
;
$phone
=
$request
->
phone
?
$request
->
phone
:
$userObj
->
phone
;
$consignee
=
$request
->
consignee
??
''
;
if
(
!
$catKey
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
$shoppingCart
=
$userObj
->
shopping_cart
?
json_decode
(
$userObj
->
shopping_cart
,
true
)
:
[];
if
(
empty
(
$shoppingCart
))
{
return
$this
->
JsonResponse
(
''
,
'购物车无资源'
,
500
);
}
$userAddress
=
''
;
// if ($address_id) {
// $userAddress = UserAddress::where(['uid' => $user_id, 'id' => $address_id])->first();
// if (!$userAddress) {
// return $this->JsonResponse('', '请选择收货地址', 500);
// }
// }
$order_sn
=
$this
->
getOrderSn
();
DB
::
beginTransaction
();
try
{
$total_price
=
0
;
$idsArr
=
explode
(
","
,
$catKey
);
$orderGoods
=
[];
$goods_name
=
''
;
foreach
(
$idsArr
as
$key
)
{
$cartRow
=
isset
(
$shoppingCart
[
$key
])
?
$shoppingCart
[
$key
]
:
[];
if
(
!
$cartRow
)
{
continue
;
}
//商品信息
$goods_id
=
$cartRow
[
'goods_id'
];
$attr_id
=
$cartRow
[
'attr_id'
];
$goodsObj
=
GoodModel
::
find
(
$goods_id
);
$skuObj
=
GoodSku
::
find
(
$attr_id
);
if
(
!
$goodsObj
||
!
$skuObj
)
{
unset
(
$shoppingCart
[
$key
]);
continue
;
}
else
{
if
(
$skuObj
->
stock
<
$cartRow
[
'num'
])
{
return
$this
->
JsonResponse
(
''
,
'该商品库存不足!'
,
500
);
}
}
$goods_price
=
(
$merchant_id
&&
$goodsObj
->
goods_type
)
?
$skuObj
->
cg_price
:
$skuObj
->
market_price
;
$goods_img
=
isset
(
$goodsObj
->
cover_img
)
?
$goodsObj
->
cover_img
:
''
;
$goods_name
.=
$goodsObj
->
goods_name
.
"、"
;
$tmp
=
[];
$tmp
[
'goods_id'
]
=
$goods_id
;
$tmp
[
'goods_number'
]
=
$cartRow
[
'num'
];
$tmp
[
'goods_price'
]
=
$goods_price
;
$tmp
[
'goods_attr'
]
=
$cartRow
[
'attr_txt'
];
$tmp
[
'attr_id'
]
=
$attr_id
;
$tmp
[
'goods_name'
]
=
$goodsObj
->
goods_name
;
$tmp
[
'goods_img'
]
=
$goods_img
;
$tmp
[
'merchant_id'
]
=
$merchant_id
;
$tmp
[
'created_at'
]
=
date
(
"Y-m-d H:i:s"
);
array_push
(
$orderGoods
,
$tmp
);
//总价
$total_price
+=
((
float
)
$goods_price
*
(
int
)
$cartRow
[
'num'
]);
//删除购物车商品
unset
(
$shoppingCart
[
$key
]);
}
if
(
$total_price
==
0
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
$orderObj
->
order_sn
=
$order_sn
;
$orderObj
->
user_id
=
$user_id
;
$orderObj
->
address
=
$address
;
$orderObj
->
area
=
$area
;
$orderObj
->
consignee
=
$consignee
;
$orderObj
->
mobile
=
$phone
;
$orderObj
->
goods_amount
=
$total_price
;
$orderObj
->
order_amount
=
$total_price
;
//$orderObj->goods_sn = $goodObj->goods_sn;
$orderObj
->
delivery_type
=
$delivery_type
;
$orderObj
->
merchant_id
=
$merchant_id
;
$orderObj
->
store_id
=
$store_id
;
if
(
$orderObj
->
save
())
{
$order_id
=
$orderObj
->
id
;
foreach
(
$orderGoods
as
$key
=>
$item
)
{
$orderGoods
[
$key
][
'order_id'
]
=
$order_id
;
}
DB
::
table
(
"li_order_goods"
)
->
insert
(
$orderGoods
);
}
else
{
return
$this
->
JsonResponse
(
''
,
'创建购物车订单失败'
,
201
);
}
$userObj
->
shopping_cart
=
empty
(
$shoppingCart
)
?
null
:
json_encode
(
$shoppingCart
,
JSON_UNESCAPED_UNICODE
);
$userObj
->
save
();
DB
::
commit
();
return
$this
->
JsonResponse
([
'order_id'
=>
$orderObj
->
id
]);
}
catch
(
\Exception
$exception
)
{
Log
::
add
(
'创建购物车订单失败'
,
$exception
->
getMessage
());
DB
::
rollBack
();
return
$this
->
JsonResponse
(
''
,
'创建购物车订单失败'
,
201
);
}
}
private
function
getOrderSn
()
{
$order_sn
=
''
;
$flag
=
0
;
do
{
//'20231229875256';
$order_sn
=
date
(
'Ymd'
)
.
mt_rand
(
1000
,
9999
)
.
substr
(
implode
(
""
,
array_map
(
'ord'
,
str_split
(
substr
(
uniqid
(),
7
,
13
),
1
))),
0
,
8
);
$rowObj
=
OrderInfoModel
::
where
(
'order_sn'
,
$order_sn
)
->
first
();
$flag
=
$rowObj
?
0
:
1
;
}
while
(
$flag
<
1
);
return
$order_sn
;
}
//订单支付
public
function
pay
(
Request
$request
)
{
$order_id
=
$request
->
order_id
??
0
;
$openid
=
$request
->
user
()
->
openid
;
DB
::
beginTransaction
();
try
{
$res
=
''
;
$order
=
OrderInfoModel
::
find
(
$order_id
);
if
(
$order
->
pay_cs
>
0
)
{
// $order_record = $order->order_record ?? $order->order_sn;
// $order->order_sn = $this->getOrderSn();
// $order->order_record = $order_record . "|" . $order->order_sn;
// $order->save();
}
$orderGoodsObj
=
OrderGoods
::
where
(
"order_id"
,
$order_id
)
->
first
();
$order_title
=
$orderGoodsObj
?
$orderGoodsObj
->
goods_name
:
''
;
$res
=
Pay
::
pay
(
$order_title
,
$order
->
order_sn
,
$order
->
order_amount
,
$openid
);
$order
->
pay_cs
+=
1
;
$order
->
save
();
DB
::
commit
();
return
$this
->
JsonResponse
(
$res
);
}
catch
(
\Exception
$exception
)
{
Log
::
add
(
'拉起微信支付失败'
,
$exception
->
getMessage
());
return
$this
->
JsonResponse
(
''
,
'拉起微信支付失败'
,
201
);
}
}
//付款回调
public
function
payNotify
(
Request
$request
)
{
$fields
=
$request
->
query
->
all
();
return
$this
->
JsonResponse
(
Pay
::
payNotify
(
$fields
));
}
// 已完成:用户点击确认收货或发货后3天,自动变更为已完成状态
// 售后:已完成状态下48小时内展示申请售后按钮,可申请售后,如超过48小时,则不展示申请售后按钮
public
function
orderList
(
Request
$request
)
{
$status
=
$request
->
order_status
??
-
1
;
$page
=
$request
->
page
??
1
;
$limit
=
$request
->
limit
??
10
;
$sql
=
OrderInfoModel
::
where
([
'user_id'
=>
$request
->
user
()
->
id
,
'deleted_at'
=>
null
]);
if
(
$status
>=
0
)
{
$sql
=
$sql
->
where
([
'order_status'
=>
$status
]);
}
$total
=
$sql
->
count
();
$data
=
[
'total'
=>
$total
,
'total_page'
=>
ceil
(
$total
/
$limit
),
'list'
=>
[]
];
$listData
=
$sql
->
offset
((
$page
-
1
)
*
$limit
)
->
limit
(
$limit
)
->
orderBy
(
'created_at'
,
'DESC'
)
->
get
();
if
(
$listData
->
toArray
())
{
foreach
(
$listData
as
$item
)
{
//订单商品
$order_goods
=
[];
$ogoods
=
OrderGoods
::
where
([
"order_id"
=>
$item
->
id
])
->
limit
(
1
)
->
get
();
foreach
(
$ogoods
as
$key
=>
$valObj
)
{
$tmp
=
[];
$tmp
[
'og_id'
]
=
$valObj
->
id
;
$tmp
[
'goods_id'
]
=
$valObj
->
goods_id
;
$tmp
[
'goods_name'
]
=
$valObj
->
goods_name
;
$tmp
[
'goods_number'
]
=
$valObj
->
goods_number
;
$tmp
[
'goods_attr'
]
=
$valObj
->
goods_attr
;
$tmp
[
'goods_price'
]
=
$valObj
->
goods_price
;
$tmp
[
'goods_img'
]
=
$valObj
->
goods_img
?
togetherFilePath
(
$valObj
->
goods_img
)
:
''
;
$tmp
[
'is_comment'
]
=
$valObj
->
is_comment
;
array_push
(
$order_goods
,
$tmp
);
}
$data
[
'list'
][]
=
[
'order_id'
=>
$item
->
id
,
'order_sn'
=>
$item
->
order_sn
,
'order_status'
=>
$item
->
order_status
,
'status_txt'
=>
OrderInfoModel
::
STATUS_OPTIONS
[
$item
->
order_status
],
//'is_apply' => $is_apply,
//'service_status' => $item->service_status,
'order_amount'
=>
$item
->
order_amount
,
'verification_code'
=>
$item
->
verification_code
??
''
,
'created_at'
=>
date
(
"Y-m-d H:i"
,
strtotime
(
$item
->
created_at
)),
'order_goods'
=>
$order_goods
];
}
}
return
$this
->
JsonResponse
(
$data
);
}
public
function
canceOrder
(
Request
$request
)
{
$order_id
=
$request
->
order_id
??
null
;
$model
=
OrderInfoModel
::
find
(
$order_id
);
if
(
!
$model
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
if
(
$model
->
user_id
!=
$request
->
user
()
->
id
)
{
return
$this
->
JsonResponse
(
''
,
'非本人订单'
,
201
);
}
if
(
!
in_array
(
$model
->
order_status
,
[
0
,
1
]))
{
return
$this
->
JsonResponse
(
''
,
'不满足取消条件'
.
$model
->
order_status
,
500
);
}
DB
::
beginTransaction
();
try
{
//代到货订单--退库存
if
(
$model
->
order_status
==
1
)
{
//更新商品库存
$goodsList
=
OrderGoods
::
where
(
"order_id"
,
$order_id
)
->
get
();
foreach
(
$goodsList
as
$item
)
{
$gid
=
$item
->
goods_id
;
$attr_id
=
$item
->
attr_id
;
$mer_id
=
$item
->
merchant_id
;
$goods_number
=
$item
->
goods_number
;
$goodsObj
=
GoodModel
::
find
(
$gid
);
//更新商品规格库存
$attrObj
=
GoodSku
::
find
(
$attr_id
);
$attr_stock
=
$attrObj
->
stock
+
$goods_number
;
$attrObj
->
stock
=
$attr_stock
;
$attrObj
->
save
();
//更新商品sku
$skuArr
=
json_decode
(
$goodsObj
->
sku
,
true
);
if
(
$skuArr
[
'sku'
])
{
foreach
(
$skuArr
[
'sku'
]
as
$kk
=>
$vv
)
{
if
(
isset
(
$vv
[
'attr_sn'
])
&&
$vv
[
'attr_sn'
]
==
$attrObj
->
attr_sn
)
{
$skuArr
[
'sku'
][
$kk
][
'stock'
]
=
$attr_stock
;
}
}
$goodsObj
->
sku
=
json_encode
(
$skuArr
,
JSON_UNESCAPED_UNICODE
);
$goodsObj
->
save
();
}
//商户规格库存
$mgsObj
=
MerchantGoodSku
::
where
([
'goods_id'
=>
$gid
,
'attr_id'
=>
$attr_id
,
'merchant_id'
=>
$mer_id
])
->
first
();
if
(
$mer_id
&&
$mgsObj
)
{
$changeStock
=
$mgsObj
->
stock
+
$goods_number
;
$mgsObj
->
stock
=
$changeStock
;
$mgsObj
->
save
();
}
}
}
$model
->
order_status
=
7
;
$model
->
save
();
DB
::
commit
();
}
catch
(
\Exception
$exception
)
{
DB
::
rollBack
();
Log
::
add
(
'取消订单失败'
,
$exception
->
getMessage
());
return
$this
->
JsonResponse
(
''
,
'取消订单失败'
,
201
);
}
return
$this
->
JsonResponse
(
''
);
}
public
function
delOrder
(
Request
$request
)
{
$order_id
=
$request
->
order_id
??
null
;
$model
=
OrderInfoModel
::
find
(
$order_id
);
if
(
!
$model
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
if
(
$model
->
user_id
!=
$request
->
user
()
->
id
)
{
return
$this
->
JsonResponse
(
''
,
'非本人订单'
,
201
);
}
$model
->
delete
();
return
$this
->
JsonResponse
(
''
);
}
//确认收货(订单完成)
// public function completeOrder(Request $request)
// {
// $order_id = $request->order_id ?? null;
// if (!$order_id) {
// return $this->JsonResponse('', '参数错误', 201);
// }
// $model = OrderInfoModel::find($order_id);
// if ($model->user_id != $request->user()->id) {
// return $this->JsonResponse('', '非本人订单', 201);
// }
// $model->shipping_status = 2;
// $model->order_status = 2;
// $model->received_at = date("Y-m-d H:i:s");
// $model->save();
// return $this->JsonResponse('');
// }
//扫码核销
public
function
scanCodeVerifi
(
Request
$request
)
{
$verObj
=
$request
->
user
();
$code
=
$request
->
code
??
''
;
$orderObj
=
OrderInfoModel
::
where
(
'verification_code'
,
$code
)
->
first
();
if
(
!
$orderObj
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
Log
::
add
(
'核销操作5'
,
[
$orderObj
->
verification_at
]);
if
(
!
$orderObj
->
verification_at
)
{
$orderObj
->
order_status
=
3
;
$orderObj
->
verifier_id
=
$verObj
->
id
;
$orderObj
->
verifier
=
$verObj
->
name
;
$orderObj
->
verification_at
=
date
(
"Y-m-d H:i:s"
);
$orderObj
->
save
();
}
else
{
return
$this
->
JsonResponse
(
''
,
'该码已核销,无需多次重复扫码'
,
500
);
}
Log
::
add
(
'核销操作'
,
$orderObj
->
toArray
());
return
$this
->
JsonResponse
(
''
);
}
//扫码核销展示详情
public
function
scanCodeDetail
(
Request
$request
)
{
$code
=
$request
->
code
??
''
;
$orderObj
=
OrderInfoModel
::
where
(
'verification_code'
,
$code
)
->
first
();
if
(
!
$orderObj
)
{
Log
::
add
(
'核销码'
,
$code
);
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
$order_id
=
$orderObj
->
id
;
//商品信息
$order_goods
=
$this
->
getOrderGoods
(
$order_id
);
//快递待收点、收货地址
$delivery
=
[];
if
(
$orderObj
->
address
)
{
$delivery
[
'contacts'
]
=
$orderObj
->
consignee
;
$delivery
[
'phone'
]
=
$orderObj
->
mobile
;
$delivery
[
'area'
]
=
$orderObj
->
area
.
"("
.
$orderObj
->
address
.
")"
;
$delivery
[
'lat'
]
=
''
;
$delivery
[
'lng'
]
=
''
;
}
if
(
$orderObj
->
store_id
)
{
$sObj
=
Store
::
find
(
$orderObj
->
store_id
);
$lat_lng
=
$sObj
->
lat_lng
;
//121.47,31.23
$latlngArr
=
$lat_lng
?
explode
(
","
,
$lat_lng
)
:
[];
$delivery
[
'contacts'
]
=
$sObj
->
contacts
;
$delivery
[
'phone'
]
=
$sObj
->
phone
;
$delivery
[
'address'
]
=
$sObj
->
address
;
$delivery
[
'lat'
]
=
isset
(
$latlngArr
[
0
])
?
$latlngArr
[
0
]
:
''
;
$delivery
[
'lng'
]
=
isset
(
$latlngArr
[
1
])
?
$latlngArr
[
1
]
:
''
;
}
$data
=
[
'id'
=>
$order_id
,
'order_sn'
=>
$orderObj
->
order_sn
,
'user_id'
=>
$orderObj
->
user_id
,
'order_amount'
=>
$orderObj
->
order_amount
,
//'mobile' => $orderObj->mobile,
'delivery'
=>
$delivery
,
'delivery_type'
=>
$orderObj
->
delivery_type
,
'delivery_typename'
=>
(
$orderObj
->
delivery_type
==
1
)
?
'快递代收点'
:
'送货上门'
,
'created_at'
=>
date
(
"Y-m-d H:i:s"
,
strtotime
(
$orderObj
->
created_at
)),
'order_goods'
=>
$order_goods
];
return
$this
->
JsonResponse
(
$data
);
}
//订单详情
public
function
OrderInfo
(
Request
$request
)
{
$order_id
=
$request
->
order_id
??
0
;
$orderObj
=
OrderInfoModel
::
find
(
$order_id
);
if
(
!
$orderObj
)
{
return
$this
->
JsonResponse
(
''
,
'参数错误'
,
201
);
}
$user_id
=
$request
->
user
()
->
id
;
if
(
$orderObj
->
user_id
!=
$user_id
)
{
return
$this
->
JsonResponse
(
''
,
'非本人订单'
,
201
);
}
//商品信息
$order_goods
=
$this
->
getOrderGoods
(
$order_id
);
//快递待收点、收货地址
$delivery
=
[];
if
(
$orderObj
->
consignee
)
{
//$addressObj = UserAddress::find($orderObj->address_id);
$delivery
[
'contacts'
]
=
$orderObj
->
consignee
;
$delivery
[
'phone'
]
=
$orderObj
->
mobile
;
$delivery
[
'address'
]
=
$orderObj
->
address
;
$delivery
[
'area'
]
=
$orderObj
->
area
;
// $delivery['lat'] = $addressObj->lat;
// $delivery['lng'] = $addressObj->lng;
}
if
(
$orderObj
->
store_id
)
{
$sObj
=
Store
::
find
(
$orderObj
->
store_id
);
$lat_lng
=
$sObj
->
lat_lng
;
//121.47,31.23
$latlngArr
=
$lat_lng
?
explode
(
","
,
$lat_lng
)
:
[];
$delivery
[
'contacts'
]
=
$sObj
->
contacts
;
$delivery
[
'phone'
]
=
$sObj
->
phone
;
$delivery
[
'address'
]
=
$sObj
->
address
;
$delivery
[
'lat'
]
=
isset
(
$latlngArr
[
0
])
?
$latlngArr
[
0
]
:
''
;
$delivery
[
'lng'
]
=
isset
(
$latlngArr
[
1
])
?
$latlngArr
[
1
]
:
''
;
}
//订单状态
$order_status
=
$orderObj
->
order_status
;
$status_txt
=
OrderInfoModel
::
STATUS_OPTIONS
[
$order_status
];
$data
=
[
'id'
=>
$orderObj
->
id
,
'order_sn'
=>
$orderObj
->
order_sn
,
'user_id'
=>
$orderObj
->
user_id
,
'order_amount'
=>
$orderObj
->
order_amount
,
'mobile'
=>
$orderObj
->
mobile
,
'order_status'
=>
$order_status
,
'status_txt'
=>
$status_txt
,
'verification_code'
=>
$orderObj
->
verification_code
??
''
,
'delivery'
=>
$delivery
,
'delivery_type'
=>
$orderObj
->
delivery_type
,
'delivery_typename'
=>
(
$orderObj
->
delivery_type
==
1
)
?
'快递代收点'
:
'送货上门'
,
'created_at'
=>
date
(
"Y-m-d H:i:s"
,
strtotime
(
$orderObj
->
created_at
)),
'order_goods'
=>
$order_goods
];
return
$this
->
JsonResponse
(
$data
);
}
//订单商品信息
private
function
getOrderGoods
(
$oid
)
{
$order_goods
=
[];
$ogoods
=
OrderGoods
::
where
([
"order_id"
=>
$oid
])
->
get
();
foreach
(
$ogoods
as
$key
=>
$valObj
)
{
$tmp
=
[];
$tmp
[
'og_id'
]
=
$valObj
->
id
;
$tmp
[
'goods_id'
]
=
$valObj
->
goods_id
;
$tmp
[
'goods_name'
]
=
$valObj
->
goods_name
;
$tmp
[
'goods_number'
]
=
$valObj
->
goods_number
;
$tmp
[
'goods_attr'
]
=
$valObj
->
goods_attr
;
$tmp
[
'goods_price'
]
=
$valObj
->
goods_price
;
$tmp
[
'is_comment'
]
=
$valObj
->
is_comment
;
$tmp
[
'goods_img'
]
=
$valObj
->
goods_img
?
togetherFilePath
(
$valObj
->
goods_img
)
:
''
;
array_push
(
$order_goods
,
$tmp
);
}
return
$order_goods
;
}
//购物车确认订单
public
function
CheckoutCartOrder
(
Request
$request
)
{
$cartKey
=
$request
->
cartKey
??
''
;
//商品ID拼接 5_1,6_1
if
(
!
$cartKey
)
{
return
$this
->
JsonResponse
(
''
,
'请选择购物车商品'
,
500
);
}
$gidArr
=
explode
(
","
,
$cartKey
);
$user
=
$request
->
user
();
$shoppingCart
=
$user
->
shopping_cart
?
json_decode
(
$user
->
shopping_cart
,
true
)
:
[];
$data
=
[];
foreach
(
$shoppingCart
as
$kk
=>
$item
)
{
$tmp
=
[];
if
(
!
in_array
(
$kk
,
$gidArr
))
{
continue
;
}
$goodsObj
=
GoodModel
::
find
(
$item
[
'goods_id'
]);
$tmp
[
'catKey'
]
=
$kk
;
$tmp
[
'goods_id'
]
=
$item
[
'goods_id'
];
$tmp
[
'goods_name'
]
=
$goodsObj
->
goods_name
;
$tmp
[
'num'
]
=
$item
[
'num'
];
$tmp
[
'goods_price'
]
=
sprintf
(
'%.2f'
,
$item
[
'goods_price'
]);
$tmp
[
'attr_name'
]
=
$item
[
'attr_txt'
];
$tmp
[
'goods_img'
]
=
$goodsObj
->
cover_img
?
togetherFilePath
(
$goodsObj
->
cover_img
)
:
''
;
$tmp
[
'attr_id'
]
=
$item
[
'attr_id'
];
array_push
(
$data
,
$tmp
);
}
return
$this
->
JsonResponse
(
$data
);
}
//商户端首页统计
public
function
orderCollect
(
Request
$request
)
{
$muser
=
$request
->
user
();
$merchant_id
=
$muser
->
merchant_id
;
//订单状态 0:待付款 1:待到货 2:待领取 3: 待评价 4:已完成 7:已取消 8:已退款
$where
=
[
'merchant_id'
=>
$merchant_id
];
$firstDayOfMonth
=
date
(
'Y-m-01 00:00:00'
);
// 00:00:00
$lastDayOfMonth
=
date
(
"Y-m-t 23:59:59"
,
time
());
//本月数据统计
$currentMonth
=
[];
$currentMonth
[
'buyCount'
]
=
OrderInfoModel
::
where
(
$where
)
->
where
(
'pay_status'
,
1
)
->
whereBetween
(
'created_at'
,
[
$firstDayOfMonth
,
$lastDayOfMonth
])
->
count
();
$currentMonth
[
'pickedCount'
]
=
OrderInfoModel
::
where
(
$where
)
->
where
(
'pay_status'
,
1
)
->
whereIn
(
'order_status'
,
[
3
,
4
])
->
whereBetween
(
'created_at'
,
[
$firstDayOfMonth
,
$lastDayOfMonth
])
->
count
();
$currentMonth
[
'waitCount'
]
=
OrderInfoModel
::
where
(
$where
)
->
where
(
'pay_status'
,
1
)
->
where
(
"order_status"
,
2
)
->
whereBetween
(
'created_at'
,
[
$firstDayOfMonth
,
$lastDayOfMonth
])
->
count
();
$currentStock
=
MerchantGoodSku
::
where
(
$where
)
->
sum
(
'stock'
);
$currentMonth
[
'stockCount'
]
=
$currentStock
;
//上月数据统计
$lastMonth
=
[];
if
(
date
(
'd'
)
<
10
)
{
}
$firstDayOfLastMonth
=
date
(
'Y-m-01 00:00:00'
,
strtotime
(
'-1 month'
,
time
()));
$ninthDayOfLastMonth
=
date
(
'Y-m-t 23:59:59'
,
strtotime
(
'-1 month'
,
time
()));
$lastMonth
[
'buyCount'
]
=
OrderInfoModel
::
where
(
$where
)
->
where
(
'pay_status'
,
1
)
->
whereBetween
(
'created_at'
,
[
$firstDayOfLastMonth
,
$ninthDayOfLastMonth
])
->
count
();
$lastMonth
[
'pickedCount'
]
=
OrderInfoModel
::
where
(
$where
)
->
where
(
'pay_status'
,
1
)
->
whereIn
(
'order_status'
,
[
3
,
4
])
->
whereBetween
(
'created_at'
,
[
$firstDayOfLastMonth
,
$ninthDayOfLastMonth
])
->
count
();
$lastMonth
[
'waitCount'
]
=
OrderInfoModel
::
where
(
$where
)
->
where
(
'pay_status'
,
1
)
->
where
(
"order_status"
,
2
)
->
whereBetween
(
'created_at'
,
[
$firstDayOfLastMonth
,
$ninthDayOfLastMonth
])
->
count
();
//本月订单商品销量
$goods_number
=
OrderGoods
::
where
([
'merchant_id'
=>
$merchant_id
,
'is_pay'
=>
1
])
->
whereBetween
(
'created_at'
,
[
$firstDayOfMonth
,
$lastDayOfMonth
])
->
sum
(
'goods_number'
);
$lastMonth
[
'stockCount'
]
=
$goods_number
+
$currentStock
;
return
$this
->
JsonResponse
([
'current'
=>
$currentMonth
?
$currentMonth
:
new
\stdClass
(),
'last'
=>
$lastMonth
?
$lastMonth
:
new
\stdClass
()]);
}
}
app/Http/Controllers/Api/OrderDivideRecordController.php
View file @
75115354
<?php
namespace
App\Http\Controllers\Api
;
use
App\Command\Log
;
use
App\Models\Carousel
;
use
App\Models\User
;
...
...
@@ -7,6 +9,10 @@
use
App\Models\OrderDivideRecord
;
use
Illuminate\Http\Request
;
use
NwVVVS\AdapayCore\AdaPay\Payment
;
use
Illuminate\Support\Facades\DB
;
use
App\Models\Adapay
;
use
App\Models\OrderInfo
;
class
OrderDivideRecordController
extends
BaseController
{
...
...
@@ -45,15 +51,15 @@ public function manualDivide(Request $request)
{
$payment_confirm
=
new
\NwVVVS\AdapaySdk\PaymentConfirm
();
//支付确认
$member_id
=
$request
->
member_id
??
0
;
//汇付会员id
$user_id
=
$request
->
user_id
??
0
;
//用户
$amount
=
$request
->
amount
;
//分账金额·
$payment_id
=
$request
->
payment_id
;
//支付记录表中的payment_id,每笔支付订单可以分账多次
if
(
!
$amount
||!
$payment_id
||
!
$user_id
)
{
$member_id
=
$request
->
member_id
??
0
;
//汇付会员id
$user_id
=
$request
->
user_id
??
0
;
//用户
$amount
=
$request
->
amount
;
//分账金额·
$payment_id
=
$request
->
payment_id
;
//支付记录表中的payment_id,每笔支付订单可以分账多次
if
(
!
$amount
||
!
$payment_id
||
!
$user_id
)
{
return
$this
->
JsonResponse
(
''
,
'参数 amount、payment_id 不能为空'
,
201
);
}
//参数介绍member_id:汇付会员id,amount:分账金额,fee_flag:是否内扣手续费
$div_members
=
[
'member_id'
=>
$member_id
,
'amount'
=>
sprintf
(
"%.2f"
,
$amount
),
'fee_flag'
=>
'Y'
];
$confirm_amt
=
sprintf
(
"%.2f"
,
$amount
);
// 确保金额格式化为两位小数
...
...
@@ -64,7 +70,7 @@ public function manualDivide(Request $request)
"order_no"
=>
'payconfirm_'
.
date
(
"YmdHis"
)
.
rand
(
100000
,
999999
),
"confirm_amt"
=>
$confirm_amt
,
// 使用格式化后的金额
"description"
=>
""
,
"div_members"
=>
$div_members
,
"div_members"
=>
$div_members
,
);
// $payment_params['div_members'] = $div_members;
// $payment_params['confirm_amt'] = $confirm_amt;
...
...
@@ -73,13 +79,13 @@ public function manualDivide(Request $request)
# 发起支付确认创建
$payment_confirm
->
create
(
$payment_params
);
# 对支付确认创建结果进行处理
$logData
=
[
'request_params'
=>
$payment_params
,
'response'
=>
$payment_confirm
->
result
];
if
(
$payment_confirm
->
isError
())
{
//失败处理
Log
::
addByName
(
'manualDivide手动分账'
,
json_encode
(
$logData
,
JSON_UNESCAPED_UNICODE
));
...
...
@@ -88,12 +94,12 @@ public function manualDivide(Request $request)
//成功处理
$result
=
$payment_confirm
->
result
;
Log
::
addByName
(
'manualDivide手动分账'
,
json_encode
(
$logData
,
JSON_UNESCAPED_UNICODE
));
if
(
$result
[
'status'
]
==
'succeeded'
)
{
//写入支付确认信息
$hfResult
=
(
new
HfPayconfirm
())
->
add
(
$payment_params
,
$result
[
'fee_amt'
]);
$logData
[
'hf_result'
]
=
$hfResult
;
//新增分佣记录
OrderDivideRecord
::
addRecord
(
og_id
:
''
,
// og_id (这里没有商品ID信息,可以传空字符串)
...
...
@@ -107,7 +113,7 @@ public function manualDivide(Request $request)
isExistAccount
:
1
,
// isExistAccount (假设已实名)
payconfirm_no
:
$payment_params
[
'order_no'
]
// payconfirm_no (使用生成的订单号)
);
return
$this
->
JsonResponse
(
$logData
);
}
else
{
return
$this
->
JsonResponse
(
$logData
,
'支付确认处理中'
,
202
);
...
...
@@ -115,4 +121,38 @@ public function manualDivide(Request $request)
}
return
$this
->
JsonResponse
([]);
}
//后面改成消息队列,先单线程运行,快速开发
//轮训更新订单冻结状态,将冻结完成的订单进行分账
public
function
updateOrderStatusToDiv
(
Request
$request
)
{
//查询完成冻结的订单,分账
$orderList
=
DB
::
getList
(
'SELECT id,order_sn,user_id,order_amount,start_freeze_time
FROM li_order_info
WHERE deleted_at IS NULL
AND pay_status = 1
AND is_div = 0
AND start_freeze_time is not null
AND start_freeze_time < NOW() - INTERVAL 6 DAY
AND order_status IN (3, 4);
'
);
if
(
$orderList
)
{
foreach
(
$orderList
as
$kk
=>
$order
)
{
$orderObj
=
OrderInfo
::
find
(
$order
->
id
);
//分账
if
(
Adapay
::
handlePaymentConfirmAndDivide
(
$orderObj
->
order_no
,
$orderObj
->
id
))
{
$orderObj
->
is_div
=
1
;
$orderObj
->
save
();
}
else
{
//分账失败,记录日志
Log
::
addByName
(
'updateOrderStatusToDiv分账失败'
,
json_encode
(
$order
,
JSON_UNESCAPED_UNICODE
));
}
}
}
return
$this
->
JsonResponse
([]);
}
}
app/Models/Adapay.php
View file @
75115354
...
...
@@ -62,10 +62,10 @@ public function pay($order_title, $order_sn, $order_amount, $openid)
}
}
//支付回调-分账
//支付回调-分账
--》改成冻结,6天后解冻分账
public
function
payNotify
(
$params
=
[])
{
$payment_confirm
=
new
\NwVVVS\AdapaySdk\PaymentConfirm
();
//支付确认
$adapay_tools
=
new
\NwVVVS\AdapaySdk\AdapayTools
();
//验证签名
$post_data
=
json_decode
(
$params
[
'data'
],
1
);
$post_data_str
=
json_encode
(
$post_data
,
JSON_UNESCAPED_UNICODE
);
...
...
@@ -136,10 +136,10 @@ public function payNotify($params = [])
//支付记录
// 创建新的PaymentRecord对象
$pay_cord
=
new
PaymentRecord
();
// 查询是否已存在相同order_sn的支付记录
$cordLog
=
$pay_cord
->
where
([
'order_sn'
=>
$message
[
'order_no'
]])
->
first
();
// 如果不存在记录,则创建新记录
if
(
!
$cordLog
)
{
// 设置支付记录的各项属性
...
...
@@ -149,7 +149,7 @@ public function payNotify($params = [])
$pay_cord
->
party_order_id
=
$message
[
'party_order_id'
];
// 合作方订单ID
$pay_cord
->
money
=
$message
[
'real_amt'
];
// 实际支付金额
$pay_cord
->
uid
=
$orderObj
->
user_id
;
// 用户ID
// 保存支付记录到数据库
$pay_cord
->
save
();
}
...
...
@@ -167,51 +167,11 @@ public function payNotify($params = [])
return
false
;
}
$orderObj
->
freeze_stat
=
'UNFREEZE'
;
$orderObj
->
start_freeze_time
=
date
(
'Y-m-d H:i:s'
);
$orderObj
->
save
();
//交易记录
$prObj
=
PaymentRecord
::
where
(
'order_sn'
,
$order_no
)
->
first
();
# 支付确认参数设置
$payment_params
=
array
(
"payment_id"
=>
$prObj
->
payment_id
,
"order_no"
=>
'payconfirm_'
.
date
(
"YmdHis"
)
.
rand
(
100000
,
999999
),
"confirm_amt"
=>
$prObj
->
money
,
"description"
=>
""
,
"div_members"
=>
""
//分账参数列表 默认是数组List
);
DB
::
beginTransaction
();
try
{
//分账列表
$divResult
=
OrderDivideRecord
::
divide
(
$orderObj
->
id
,
$payment_params
[
'order_no'
]);
//返回分账参数列表
$payment_params
[
'div_members'
]
=
$divResult
[
'div_members'
];
$payment_params
[
'confirm_amt'
]
=
$divResult
[
'confirm_amt'
];
Log
::
add
(
'发起分账支付确认-向汇付'
.
$order_no
,
$payment_params
);
# 发起支付确认创建
$payment_confirm
->
create
(
$payment_params
);
# 对支付确认创建结果进行处理
if
(
$payment_confirm
->
isError
())
{
//失败处理
Log
::
add
(
'支付确认失败'
,
$payment_confirm
->
result
);
$result
=
$payment_confirm
->
result
;
throw
new
Exception
(
$result
[
'error_msg'
]);
}
else
{
//成功处理
Log
::
add
(
'支付确认成功'
,
$payment_confirm
->
result
);
$result
=
$payment_confirm
->
result
;
if
(
$result
[
'status'
]
==
'succeeded'
)
{
Log
::
add
(
'分账成功'
,
[
'order_sn'
=>
$order_no
]);
//写入支付确认信息
(
new
HfPayconfirm
())
->
add
(
$payment_params
,
$result
[
'fee_amt'
]);
}
}
DB
::
commit
();
}
catch
(
\Exception
$e
)
{
Log
::
add
(
'支付确认对象失败'
,
$e
->
getMessage
());
DB
::
rollBack
();
return
false
;
}
// 调用汇付接口分账-6天后在解冻分账
//$this->handlePaymentConfirmAndDivide($order_no, $orderObj, $payment_confirm);
}
return
true
;
}
...
...
@@ -455,4 +415,106 @@ public function queryBalance($account_params)
/**
* 处理支付确认和分账逻辑
* @param string $order_no 订单号
* @param OrderInfo $orderObj 订单对象
* @param \NwVVVS\AdapaySdk\PaymentConfirm $payment_confirm 支付确认对象
* @return bool 处理结果
* @throws Exception 处理失败时抛出异常
*/
public
static
function
handlePaymentConfirmAndDivide
(
$order_no
,
$order_id
)
{
$payment_confirm
=
new
\NwVVVS\AdapaySdk\PaymentConfirm
();
//支付确认
//交易记录
$prObj
=
PaymentRecord
::
where
(
'order_sn'
,
$order_no
)
->
first
();
# 支付确认参数设置
$payment_params
=
array
(
"payment_id"
=>
$prObj
->
payment_id
,
"order_no"
=>
'payconfirm_'
.
date
(
"YmdHis"
)
.
rand
(
100000
,
999999
),
"confirm_amt"
=>
$prObj
->
money
,
"description"
=>
""
,
"div_members"
=>
""
//分账参数列表 默认是数组List
);
DB
::
beginTransaction
();
try
{
//分账列表
$divResult
=
OrderDivideRecord
::
divide
(
$order_id
,
$payment_params
[
'order_no'
]);
//返回分账参数列表
$payment_params
[
'div_members'
]
=
$divResult
[
'div_members'
];
$payment_params
[
'confirm_amt'
]
=
$divResult
[
'confirm_amt'
];
Log
::
add
(
'发起分账支付确认-向汇付'
.
$order_no
,
$payment_params
);
# 发起支付确认创建
$payment_confirm
->
create
(
$payment_params
);
# 对支付确认创建结果进行处理
if
(
$payment_confirm
->
isError
())
{
//失败处理
Log
::
add
(
'支付确认失败'
,
$payment_confirm
->
result
);
$result
=
$payment_confirm
->
result
;
throw
new
Exception
(
$result
[
'error_msg'
]);
}
else
{
//成功处理
Log
::
add
(
'支付确认成功'
,
$payment_confirm
->
result
);
$result
=
$payment_confirm
->
result
;
if
(
$result
[
'status'
]
==
'succeeded'
)
{
Log
::
add
(
'分账成功'
,
[
'order_sn'
=>
$order_no
]);
//写入支付确认信息
(
new
HfPayconfirm
())
->
add
(
$payment_params
,
$result
[
'fee_amt'
]);
}
}
DB
::
commit
();
return
true
;
}
catch
(
\Exception
$e
)
{
Log
::
add
(
'支付确认对象失败'
,
$e
->
getMessage
());
DB
::
rollBack
();
return
false
;
}
}
}
app/Models/OrderDivideRecord-分佣备份.php
0 → 100644
View file @
75115354
<?php
<?php
namespace
App\Models
;
use
App\Command\Log
;
use
Dcat\Admin\Traits\HasDateTimeFormatter
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Illuminate\Database\Eloquent\Model
;
class
OrderDivideRecord
extends
Model
{
use
HasDateTimeFormatter
;
use
SoftDeletes
;
protected
$table
=
'order_divide_record'
;
public
const
COMMISSION_TYPE
=
[
1
=>
'直推佣金'
,
//需求调整,不在分账,改成积分,支付时抵扣
2
=>
'间推佣金'
,
//需求调整,不在分账,改成积分,支付时抵扣
3
=>
'用户取货佣金'
,
4
=>
'手动调账分账'
,
];
/**
* 收益分配
* order_id 订单ID
* payconfirm_no 发起确认支付,订单流水号
*/
public
static
function
divide
(
$order_id
,
$payconfirm_no
=
''
)
{
$div_members
=
[];
//汇付 分账对象
$orderObj
=
OrderInfo
::
find
(
$order_id
);
$merchant_id
=
$orderObj
->
merchant_id
;
//绑定的商户
$buyer_id
=
$orderObj
->
user_id
;
//下单用户ID
$userObj
=
User
::
find
(
$buyer_id
);
$spuid
=
$userObj
->
spuid
;
//直推分享人
$second_spuid
=
$userObj
->
second_spuid
;
//间推分享人
$total_amount
=
0
;
//订单商品总价
$first_proportion
=
$second_proportion
=
$merchant_proportion
=
''
;
//佣金比例
$sp_ogid
=
''
;
//参与分佣商品ID
$commissionPreData
=
[
'first_member_id'
=>
''
,
'first_amount'
=>
0
,
'second_member_id'
=>
''
,
'second_amount'
=>
0
,
'merchant_member_id'
=>
''
,
'merchant_amount'
=>
0
,
];
//直推是否实名
$isFirstRealName
=
0
;
if
(
$spuid
)
{
$isFirstRealName
=
HfSettleAccount
::
where
([
'member_type'
=>
0
,
'mid'
=>
$spuid
])
->
count
();
}
//间推是否实名
$isSecondRealName
=
0
;
if
(
$second_spuid
)
{
$isSecondRealName
=
HfSettleAccount
::
where
([
'member_type'
=>
0
,
'mid'
=>
$second_spuid
])
->
count
();
}
//商户是否实名
$isMerchantRealName
=
0
;
if
(
$merchant_id
)
{
$hfCompanyMObj
=
HfCompanyMember
::
where
(
'merchant_id'
,
$merchant_id
)
->
first
();
$isMerchantRealName
=
(
$hfCompanyMObj
->
status
==
'succeeded'
)
?
1
:
0
;
}
$ogList
=
OrderGoods
::
where
(
"order_id"
,
$order_id
)
->
get
();
//订单商品
foreach
(
$ogList
as
$kk
=>
$item
)
{
$goods_amount
=
$item
->
goods_price
*
$item
->
goods_number
;
$total_amount
+=
$goods_amount
;
//商品信息
$goodObj
=
Good
::
find
(
$item
->
goods_id
);
$merchant_commission
=
$goodObj
->
merchant_commission
;
$first_commission
=
$goodObj
->
first_commission
;
$second_commission
=
$goodObj
->
second_commission
;
$first_proportion
.=
$goodObj
->
first_commission
.
","
;
$second_proportion
.=
$goodObj
->
second_commission
.
","
;
$merchant_proportion
.=
$goodObj
->
merchant_commission
.
","
;
$sp_ogid
=
$item
->
id
.
","
;
//直推佣金
if
(
$spuid
&&
$first_commission
>=
1
&&
$first_commission
<
100
)
{
$divide_price
=
number_format
(
$goods_amount
*
(
$first_commission
/
100
),
2
);
$spObj
=
User
::
find
(
$spuid
);
//汇付参数
if
(
$spObj
->
member_id
)
{
$spObj
->
total_revenue
+=
$divide_price
;
//总余额记录
$spObj
->
save
();
//self::addRecord($item->id, $order_id, $goods_amount, $divide_price, $first_commission, $spuid, $user_id, 1, $isExistAccount);
$commissionPreData
[
'first_member_id'
]
=
$spObj
->
member_id
;
$commissionPreData
[
'first_amount'
]
+=
$divide_price
;
}
}
//间推佣金
if
(
$second_spuid
&&
$second_commission
>=
1
&&
$second_commission
<
100
)
{
$divide_price
=
number_format
(
$goods_amount
*
(
$second_commission
/
100
),
2
);
$spObj
=
User
::
find
(
$second_spuid
);
//汇付参数
if
(
$spObj
->
member_id
)
{
$spObj
->
total_revenue
+=
$divide_price
;
$spObj
->
save
();
$commissionPreData
[
'second_member_id'
]
=
$spObj
->
member_id
;
$commissionPreData
[
'second_amount'
]
+=
$divide_price
;
}
}
//商户分佣记录
if
(
$merchant_id
&&
$merchant_commission
>=
1
&&
$merchant_commission
<
100
)
{
$divide_price
=
number_format
(
$goods_amount
*
(
$merchant_commission
/
100
),
2
);
//收益直接到商户账户
$merObj
=
Merchant
::
find
(
$merchant_id
);
//汇付参数
$member_id
=
$hfCompanyMObj
->
member_id
;
if
(
$member_id
)
{
$merObj
->
total_revenue
+=
$divide_price
;
$merObj
->
save
();
$commissionPreData
[
'merchant_member_id'
]
=
$member_id
;
$commissionPreData
[
'merchant_amount'
]
+=
$divide_price
;
}
}
}
//组合分账参数
//直推佣金
if
(
$spuid
&&
$first_commission
>=
1
&&
$first_commission
<
100
&&
$commissionPreData
[
'first_amount'
]
>
0
)
{
self
::
addRecord
(
$sp_ogid
,
$order_id
,
$orderObj
->
goods_amount
,
$commissionPreData
[
'first_amount'
],
$first_proportion
,
$spuid
,
$buyer_id
,
1
,
$isFirstRealName
,
$payconfirm_no
);
if
(
$isFirstRealName
)
{
array_push
(
$div_members
,
[
'member_id'
=>
$commissionPreData
[
'first_member_id'
],
'amount'
=>
sprintf
(
"%.2f"
,
$commissionPreData
[
'first_amount'
]),
'fee_flag'
=>
'N'
]);
}
}
//间推佣金
if
(
$second_spuid
&&
$second_commission
>=
1
&&
$second_commission
<
100
&&
$commissionPreData
[
'second_amount'
]
>
0
)
{
self
::
addRecord
(
$sp_ogid
,
$order_id
,
$orderObj
->
goods_amount
,
$commissionPreData
[
'second_amount'
],
$second_proportion
,
$second_spuid
,
$buyer_id
,
2
,
$isSecondRealName
,
$payconfirm_no
);
if
(
$isSecondRealName
)
{
array_push
(
$div_members
,
[
'member_id'
=>
$commissionPreData
[
'second_member_id'
],
'amount'
=>
sprintf
(
"%.2f"
,
$commissionPreData
[
'second_amount'
]),
'fee_flag'
=>
'N'
]);
}
}
//商户佣金
if
(
$merchant_id
&&
$merchant_commission
>=
1
&&
$merchant_commission
<
100
&&
$commissionPreData
[
'merchant_amount'
]
>
0
)
{
self
::
addRecord
(
$sp_ogid
,
$order_id
,
$orderObj
->
goods_amount
,
$commissionPreData
[
'merchant_amount'
],
$merchant_proportion
,
$merchant_id
,
$buyer_id
,
3
,
$isMerchantRealName
,
$payconfirm_no
);
if
(
$isMerchantRealName
)
{
array_push
(
$div_members
,
[
'member_id'
=>
$commissionPreData
[
'merchant_member_id'
],
'amount'
=>
sprintf
(
"%.2f"
,
$commissionPreData
[
'merchant_amount'
]),
'fee_flag'
=>
'N'
]);
}
}
//平台本身
$aimeiyuePrice
=
$total_amount
-
$commissionPreData
[
'first_amount'
]
-
$commissionPreData
[
'second_amount'
]
-
$commissionPreData
[
'merchant_amount'
];
self
::
addRecord
(
$sp_ogid
,
$order_id
,
$orderObj
->
goods_amount
,
$aimeiyuePrice
,
''
,
0
,
$buyer_id
,
0
,
1
,
$payconfirm_no
);
array_push
(
$div_members
,
[
'member_id'
=>
0
,
'amount'
=>
sprintf
(
"%.2f"
,
$aimeiyuePrice
),
'fee_flag'
=>
'Y'
]);
//确认分佣金额
if
(
!
$isFirstRealName
)
{
$commissionPreData
[
'first_amount'
]
=
0
;
}
if
(
!
$isSecondRealName
)
{
$commissionPreData
[
'second_amount'
]
=
0
;
}
if
(
!
$isMerchantRealName
)
{
$commissionPreData
[
'merchant_amount'
]
=
0
;
}
$confirm_amt
=
$aimeiyuePrice
+
$commissionPreData
[
'first_amount'
]
+
$commissionPreData
[
'second_amount'
]
+
$commissionPreData
[
'merchant_amount'
];
return
[
'div_members'
=>
$div_members
,
'confirm_amt'
=>
sprintf
(
"%.2f"
,
$confirm_amt
)];
}
//新增分账记录
public
static
function
addRecord
(
$og_id
,
$order_id
,
$goods_amount
,
$divide_price
,
$commission
=
''
,
$um_id
=
0
,
$buyer_id
,
$sh_type
=
0
,
$isExistAccount
,
$payconfirm_no
)
{
$recordObj
=
new
self
();
$recordObj
->
order_id
=
$order_id
;
$recordObj
->
user_id
=
$buyer_id
;
$recordObj
->
og_id
=
trim
(
$og_id
,
','
);
$recordObj
->
order_price
=
$goods_amount
;
$recordObj
->
divide_price
=
$divide_price
;
$recordObj
->
proportion
=
trim
(
$commission
,
','
);
$recordObj
->
um_id
=
$um_id
;
$recordObj
->
sh_type
=
$sh_type
;
$recordObj
->
is_div
=
$isExistAccount
?
1
:
0
;
//是否分账
$recordObj
->
payconfirm_no
=
$isExistAccount
?
$payconfirm_no
:
''
;
$recordObj
->
save
();
Log
::
add
(
'订单分佣记录'
,
$recordObj
->
toArray
());
}
public
function
order
()
{
return
$this
->
belongsTo
(
OrderInfo
::
class
,
'order_id'
,
'id'
);
}
public
function
users
()
{
return
$this
->
belongsTo
(
User
::
class
,
'user_id'
,
'id'
);
}
}
app/Models/OrderDivideRecord.php
View file @
75115354
...
...
@@ -14,8 +14,8 @@ class OrderDivideRecord extends Model
protected
$table
=
'order_divide_record'
;
public
const
COMMISSION_TYPE
=
[
1
=>
'直推佣金'
,
2
=>
'间推佣金'
,
1
=>
'直推佣金'
,
//需求调整,不在分账,改成积分,支付时抵扣
2
=>
'间推佣金'
,
//需求调整,不在分账,改成积分,支付时抵扣
3
=>
'用户取货佣金'
,
4
=>
'手动调账分账'
,
];
...
...
app/Models/OrderInfo.php
View file @
75115354
...
...
@@ -23,6 +23,7 @@ class OrderInfo extends Model
4
=>
'已完成'
,
7
=>
'已取消'
,
//8 => '已退款',
//9 => '已收货',//分账资金冻结7天,用户确认收货后汇付会立即解冻资金,t+1后资金分账,所以需要平台自己控制6天冻结,不分账,这期间可以退款
];
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment