cxhxy/app/api/model/food/Record.php
test_service d3170b4d1c 1
2023-12-01 15:43:29 +08:00

1 line
3.4 KiB
PHP

<?php
namespace app\api\model\food;
use app\common\model\food\Record as RecordModel;
use app\api\model\Setting as SettingModel;
use hema\wechat\Pay as WxPay;
use think\facade\Cache;
/**
* 交易记录模型
*/
class Record extends RecordModel
{
/**
* 获取充值订单详情 - 微信充值回调
*/
public function payDetail(string $order_no)
{
return Cache::get($order_no);
}
/**
* 更新充值付款状态 - 微信充值回调
*/
public function updatePayStatus(string $transaction_id, array $data)
{
$gift_money = 0;//赠送金额
$coupon = [];//赠送优惠券
if(isset($data['recharge_plan_id']) AND $data['recharge_plan_id'] > 0){
if($plan = RechargePlan::detail($data['recharge_plan_id'])){
if($plan['gift_type']['value'] == 10){
//优惠券
for($n=0;$n<$plan['gift_money'];$n++){
//计算优惠券到期时间
switch ($plan['coupon']['valid_time']['value']) {
case '10':
$expiretime = strtotime(date('Y-m-d').'23:59:59');
break;
case '20':
$expiretime = strtotime("+1 week");
break;
case '30':
$expiretime = strtotime("+1 month");
break;
case '40':
$expiretime = strtotime("+1 year");
break;
default:
$expiretime = time();
break;
}
$coupon[] = [
'coupon_id' => $plan['coupon']['coupon_id'],
'type' => $plan['coupon']['type']['value'],
'rule' => $plan['coupon']['rule']['value'],
'expiretime' => $expiretime,
'shop_id' => $data['shop_id'],
'user_id' => $data['user_id'],
'applet_id' => $data['applet_id']
];
}
}else{
$gift_money = $plan['gift_money']; //赠送现金余额
}
}
}
//更新用户余额信息
$user = User::get($data['user_id']);
$user->money = ['inc', $data['money']+$gift_money];//增加余额
$user->save();
//添加交易记录
//是否赠送优惠券
if(sizeof($coupon) > 0){
$model = new CouponUser;
$model->saveAll($coupon);
}
//微信 - 账单分账
if($data['type'] == 30){
$wxpay = new WxPay(SettingModel::getItem('wxpay',$data['applet_id']));
$wxpay->divide([
'out_order_no' => $data['order_no'],
'transaction_id' => $transaction_id,
'total' => $data['money'],
],$data['applet_id']);//进行分账
}
//$data['transaction_id'] = $transaction_id;
$arr[0] = $data;
if($gift_money > 0){
$arr[1] = $data;
$arr[1]['money'] = $gift_money;
$arr[1]['mode'] = 40;//赠送
$arr[1]['type'] = 10;//余额
}
$this->saveAll($arr);
return true;
}
}