107 行
3.2 KiB
PHP
107 行
3.2 KiB
PHP
<?php
|
||
|
||
namespace app\common\library;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 订单发货(支付成功后的权益发放)
|
||
*
|
||
* 统一入口,避免「微信回调」和「后台补单」两处逻辑不一致:
|
||
* member → 发放会员(Member::grantMembership,内部含分销结算)
|
||
* recharge → 余额入账(Order::creditRecharge)
|
||
* payview → 写入付费查看解锁记录
|
||
* post → 信息本身已落库待审,无需额外动作
|
||
*
|
||
* 调用方必须已保证订单是「刚由 pending 变为 paid」,本类不再做状态判断。
|
||
*/
|
||
class OrderFulfill
|
||
{
|
||
/**
|
||
* @param array $order fc_order 行(至少含 id/order_no/user_id/type/amount/related_id)
|
||
* @return void
|
||
* @throws \Throwable
|
||
*/
|
||
public static function handle(array $order)
|
||
{
|
||
// 支付成功:核销订单锁定的优惠券(余额支付在业务侧已核销,这里幂等)
|
||
CouponService::markUsed(isset($order['order_no']) ? (string)$order['order_no'] : '');
|
||
|
||
$type = isset($order['type']) ? (string)$order['type'] : '';
|
||
|
||
if ($type === 'member') {
|
||
// 会员发放内部已包含分销结算
|
||
(new \app\api\controller\Member())->grantMembership($order);
|
||
return;
|
||
}
|
||
|
||
if ($type === 'recharge') {
|
||
// 充值订单不分佣(DistributionService 内部也会忽略 recharge)
|
||
(new \app\api\controller\Order())->creditRecharge($order);
|
||
return;
|
||
}
|
||
|
||
if ($type === 'payview') {
|
||
self::unlockPayView($order);
|
||
}
|
||
|
||
if ($type === 'post') {
|
||
self::fulfillPost($order);
|
||
}
|
||
|
||
// 其它消费类型统一结算分销
|
||
DistributionService::settleOrder($order);
|
||
}
|
||
|
||
/**
|
||
* 付费发帖支付成功后激活支付状态
|
||
* 将 fc_post 的 is_paid 置为 1,确保未付款信息不会流出展示
|
||
*
|
||
* @param array $order
|
||
* @return void
|
||
*/
|
||
protected static function fulfillPost(array $order)
|
||
{
|
||
$postId = (int)(isset($order['related_id']) ? $order['related_id'] : 0);
|
||
if ($postId <= 0) {
|
||
return;
|
||
}
|
||
|
||
Db::name('fc_post')->where('id', $postId)->update([
|
||
'is_paid' => 1,
|
||
'updatetime' => time(),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 付费查看解锁(user_id + post_id 唯一索引保证幂等)
|
||
*
|
||
* @param array $order
|
||
* @return void
|
||
*/
|
||
protected static function unlockPayView(array $order)
|
||
{
|
||
$postId = (int)(isset($order['related_id']) ? $order['related_id'] : 0);
|
||
if ($postId <= 0) {
|
||
return;
|
||
}
|
||
|
||
$exists = Db::name('fc_pay_view')
|
||
->where('user_id', (int)$order['user_id'])
|
||
->where('post_id', $postId)
|
||
->find();
|
||
if ($exists) {
|
||
return;
|
||
}
|
||
|
||
Db::name('fc_pay_view')->insert([
|
||
'user_id' => (int)$order['user_id'],
|
||
'post_id' => $postId,
|
||
'pay_type' => isset($order['pay_type']) ? (string)$order['pay_type'] : 'wxpay',
|
||
'amount' => round((float)(isset($order['amount']) ? $order['amount'] : 0), 2),
|
||
'order_id' => (int)$order['id'],
|
||
'createtime' => time(),
|
||
]);
|
||
}
|
||
}
|