master
wangxinglong 2022-03-15 15:10:41 +08:00
parent 12a55a13a4
commit d9e24c9e39
2 changed files with 87 additions and 1 deletions

View File

@ -352,11 +352,78 @@ class Coupon extends Base
"lng" => $lng,
"end_time" => date($couponMain->end_time . " 00:00:00"),
"edition" => couponMain::COMMON_ON,//版本 未知作用
"is_verificated" => couponMain::COMMON_OFF,//版本 未知作用
"is_verificated" => couponMain::COMMON_OFF,//是否验证
"distribution_user_code" => $distributionUserCode,//分销人user_code
];
CouponRepository::getInstance()->receiveCoupon($data);
$couponMain->save(["received_count"=>Db::raw("received_count + 1")]);
//是否开启分销 并且有分销人 给分销人发红包
if($couponMain->is_distribution == CouponMain::COMMON_ON && !empty($distributionUserCode))
{
$distributionUser = Account::findOne([["user_code","=",$distributionUserCode]]);
if(empty($distributionUser)){
Db::rollback();
return $this->json(4001,"分享人不存在");
}
$payment = WechatPay::getInstance();
$distributionRedpack = Redpack::getDistributionRedpack($distributionUserCode,$couponMainId,$account->user_code);
//如果付款过一次了 就用同一个订单号发起
if(!empty($distributionRedpack)){
$mch_billno = $distributionRedpack->mch_billno;
}else{
$mch_billno = createUuid();
}
$amount = (($couponMain->deduction_money / 100) * $couponMain->commission_dis_distribution);
$amount = round($amount,2); //四舍五入 精确到分
$toBalanceData = [
'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
'openid' => $account->open_id,
'check_name' => 'NO_CHECK',// NO_CHECK不校验真实姓名, FORCE_CHECK强校验真实姓名
'amount' => $amount, //单位为分不小于100
'desc' => '验证优惠券分销,分销者领取',
];
// 写入红包
$redpackData = [
"coupon_id" => 0,
"mch_billno" => $mch_billno,
"openid" => $distributionUser->open_id,
"user_code" => $distributionUser->user_code,
"money" => $amount,
"create_time" => date("Y-m-d H:i:s",$time),
"pay_time" => '0000-00-00 00:00:00',
"user_type" => Redpack::userTypeDistribution,
"distributed_user_code" => $account->user_code,
"distributed_coupon_main_id" => $couponMainId,
];
//发起支付
$result = $payment->transfer->toBalance($toBalanceData);
//var_dump($result);
//付款成功才提交!!!!!伪装成功
if( isset($result['payment_no']) ){
$redpackData['pay_time'] = date("Y-m-d H:i:s",$time);
Redpack::create($redpackData);
Db::commit();
return $this->json();
}
//否则失败 记录失败原因
Log::info("企业发起付款【分销者红包】失败:" . json_encode($result,JSON_UNESCAPED_UNICODE));
//默认回滚
Db::rollback();
//失败也要写入红包记录
Redpack::create($redpackData);
return $this->json(5003, "验证失败,发放红包失败");
}
Db::commit();
return $this->json();
}catch (RepositoryException $e){

View File

@ -5,4 +5,23 @@ namespace app\model;
class Redpack extends Base
{
const userTypeDistribution = "distribution";//分销者
const userTypeDistributed = "distributed";//被分销者
/**
* 获取分销红包记录
* @param $distributionUserCode 分销人user_code
* @param $couponMainId 分销的couponMain 优惠券id
* @param $distributedUserCode 被分销人user_code
* 以上三个参数就可以确定 分销数据的唯一性
* */
public static function getDistributionRedpack($distributionUserCode,$couponMainId,$distributedUserCode)
{
return self::where([
["user_type","=",self::userTypeDistribution],
["user_code","=",$distributionUserCode],
["distributed_user_code","=",$distributedUserCode],
["distributed_coupon_main_id","=",$distributedUserCode],
])
->find();
}
}