setter
parent
848bcf2801
commit
a028686fb4
|
@ -362,107 +362,12 @@ class Coupon extends Base
|
||||||
$data["distribution_user_code"] = $distributionUserCode;//分销人user_code
|
$data["distribution_user_code"] = $distributionUserCode;//分销人user_code
|
||||||
}
|
}
|
||||||
|
|
||||||
$receiveCoupon = CouponRepository::getInstance()->receiveCoupon($data);
|
|
||||||
|
|
||||||
$couponMain->save(["received_count"=>Db::raw("received_count + 1")]);
|
$couponMain->save(["received_count"=>Db::raw("received_count + 1")]);
|
||||||
|
|
||||||
$deductionAmount = (($couponMain->deduction_money / 100) * $couponMain->commission_dis_distribution);
|
$receiveCoupon = CouponRepository::getInstance()->receiveCoupon($data);
|
||||||
|
|
||||||
//是否开启分销 并且有分销人 给分销人发红包
|
|
||||||
if(
|
|
||||||
$couponMain->business->is_assign == BusinessModel::COMMON_OFF
|
|
||||||
&&
|
|
||||||
!empty($distributionUserCode)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
$distributionUser = Account::findOne([["user_code","=",$distributionUserCode]]);
|
|
||||||
if(empty($distributionUser)){
|
|
||||||
Db::rollback();
|
|
||||||
return $this->json(4001,"分享人不存在");
|
|
||||||
}
|
|
||||||
// 2. 写入优惠券流水
|
|
||||||
$couponBillData = [
|
|
||||||
"coupon_main_id" => $couponMain->id,
|
|
||||||
"coupon_id" => $receiveCoupon->id,
|
|
||||||
"user_code" => $account->user_code,
|
|
||||||
"business_code" => $couponMain->business->code,
|
|
||||||
"agency_code" => $couponMain->business->agency_code,
|
|
||||||
"commission_agency" => $couponMain->commission_agency,
|
|
||||||
"commission_admin" => $couponMain->commission_admin,
|
|
||||||
"commission_consumer" => $couponMain->commission_consumer,
|
|
||||||
"commission_dis_admin" => $couponMain->commission_dis_admin,
|
|
||||||
"commission_dis_distribution" => $couponMain->commission_dis_distribution,
|
|
||||||
"commission_dis_distributed" => $couponMain->commission_dis_distributed,
|
|
||||||
"money" => $couponMain->money,
|
|
||||||
"agency_money" => 0,
|
|
||||||
"admin_money" => 0,
|
|
||||||
"consumer_money" => 0,
|
|
||||||
"distribution_money" => $deductionAmount,
|
|
||||||
"lat" => $lat,
|
|
||||||
"lng" => $lng,
|
|
||||||
"create_time" => date("Y-m-d H:i:s" ,$time),
|
|
||||||
];
|
|
||||||
$amount = round($deductionAmount,2); //四舍五入 精确到分
|
|
||||||
|
|
||||||
CouponBill::create($couponBillData);
|
|
||||||
|
|
||||||
$payment = WechatPay::getInstance();
|
|
||||||
$distributionRedpack = Redpack::getDistributionRedpack($distributionUserCode,$couponMainId,$account->user_code);
|
|
||||||
//如果付款过一次了 就用同一个订单号发起
|
|
||||||
if(!empty($distributionRedpack)){
|
|
||||||
$mch_billno = $distributionRedpack->mch_billno;
|
|
||||||
}else{
|
|
||||||
$mch_billno = createUuid();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$toBalanceData = [
|
|
||||||
'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
|
|
||||||
'openid' => $distributionUser->open_id,
|
|
||||||
'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
|
|
||||||
'amount' => $amount * 100, //单位为分,不小于300
|
|
||||||
'desc' => '优惠券分销奖励',
|
|
||||||
];
|
|
||||||
|
|
||||||
// 写入红包
|
|
||||||
$redpackData = [
|
|
||||||
"coupon_id" => 0,
|
|
||||||
"mch_billno" => $mch_billno,
|
|
||||||
"openid" => $distributionUser->open_id,
|
|
||||||
"user_code" => $distributionUser->user_code,
|
|
||||||
"money" => $amount * 100, //单位为分,不小于300
|
|
||||||
"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();
|
|
||||||
}
|
|
||||||
$redpackData['failure_reason'] = json_encode($result,JSON_UNESCAPED_UNICODE);
|
|
||||||
//否则失败 记录失败原因
|
|
||||||
Log::info("企业发起付款【领券分销者红包】失败:" . json_encode($result,JSON_UNESCAPED_UNICODE));
|
|
||||||
//默认回滚
|
|
||||||
Db::rollback();
|
|
||||||
//失败也要写入红包记录
|
|
||||||
Redpack::create($redpackData);
|
|
||||||
return $this->json(5003, "领取失败,发放分享者红包失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
Db::commit();
|
Db::commit();
|
||||||
return $this->json();
|
return $this->json();
|
||||||
}catch (RepositoryException $e){
|
}catch (RepositoryException $e){
|
||||||
|
@ -594,56 +499,66 @@ class Coupon extends Base
|
||||||
|
|
||||||
$deductionMoney = $coupon->couponMain->deduction_money;
|
$deductionMoney = $coupon->couponMain->deduction_money;
|
||||||
|
|
||||||
|
$returnConsumerMoney = 0;//需要返回提示的金额
|
||||||
|
|
||||||
//可分配金额 如果是普通商家
|
//未开启分销状态
|
||||||
if($business->model == BusinessModel::model_ordinary) {
|
$agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency);
|
||||||
$agencyMoney = (($deductionMoney/100) * $coupon->couponMain->commission_agency);
|
$adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin);
|
||||||
$adminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_admin);
|
$consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer);
|
||||||
$consumerMoney = (($deductionMoney/100) * $coupon->couponMain->commission_consumer);
|
$agencyMoney = round($agencyMoney,2); //四舍五入 精确到分
|
||||||
|
$adminMoney = round($adminMoney,2); //四舍五入 精确到分
|
||||||
$agencyMoney = round($agencyMoney,2); //四舍五入 精确到分
|
$consumerMoney = round($consumerMoney,2); //四舍五入 精确到分
|
||||||
$adminMoney = round($adminMoney,2); //四舍五入 精确到分
|
|
||||||
$consumerMoney = round($consumerMoney,2); //四舍五入 精确到分
|
|
||||||
|
|
||||||
|
|
||||||
//开启了分销 覆盖 平台应得金额 和消费者应得金额 并且取消 渠道商打款
|
//开启分销状态
|
||||||
if(
|
$disAdminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_admin);
|
||||||
$business->is_assign == BusinessModel::COMMON_OFF
|
$disDistributionMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distribution);
|
||||||
||
|
$disDistributedMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distributed);
|
||||||
(!empty($coupon->distribution_user_code)&&$coupon->is_distribution_coupon == CouponMain::COMMON_ON)
|
$disAgencyMoney = 0 ;//渠道商就没得钱
|
||||||
){
|
|
||||||
$redpackUser = Account::findOne([["user_code","=",$coupon->distribution_user_code]]);
|
|
||||||
|
|
||||||
$disAdminMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_admin);
|
$disAdminMoney = round($disAdminMoney,2); //四舍五入 精确到分
|
||||||
//$disDistributionMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distribution);
|
$disDistributionMoney = round($disDistributionMoney,2); //四舍五入 精确到分
|
||||||
$disDistributedMoney = (($deductionMoney/100) * $coupon->couponMain->commission_dis_distributed);
|
$disDistributedMoney = round($disDistributedMoney,2); //四舍五入 精确到分
|
||||||
//渠道商就没得钱
|
|
||||||
$agencyMoney = 0 ;
|
$consumerRedpackUser = $account;
|
||||||
$adminMoney = round($disAdminMoney,2); //四舍五入 精确到分
|
$distributionRedpackUser = [];
|
||||||
//$disDistributionMoney = round($disDistributionMoney,2); //四舍五入 精确到分
|
$agencyRedpackUser = [];
|
||||||
$consumerMoney = round($disDistributedMoney,2); //四舍五入 精确到分
|
|
||||||
}else{
|
//开启了分销
|
||||||
$redpackUser = $account;
|
if(
|
||||||
|
$business->is_assign == BusinessModel::COMMON_OFF
|
||||||
|
||
|
||||||
|
(!empty($coupon->distribution_user_code)&&$coupon->is_distribution_coupon == CouponMain::COMMON_ON)
|
||||||
|
){
|
||||||
|
$distributionRedpackUser = Account::findOne([["user_code","=",$coupon->distribution_user_code]]);
|
||||||
|
//如果没有分销人 就全是平台 和分销人的
|
||||||
|
if(empty($distributionRedpackUser)){
|
||||||
|
$confirmAdminMoney = $disAdminMoney+$disDistributionMoney;
|
||||||
|
$confirmDistributionMoney = 0;
|
||||||
|
$confirmDistributedMoney = $disDistributedMoney;
|
||||||
|
$confirmAgencyMoney = 0;
|
||||||
|
}else{//存在分销人 就走分销人
|
||||||
|
$confirmAdminMoney = $disAdminMoney;
|
||||||
|
$confirmDistributionMoney = $disDistributionMoney;
|
||||||
|
$confirmDistributedMoney = $disDistributedMoney;
|
||||||
|
$confirmAgencyMoney = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
//开启了分销 覆盖 平台应得金额 和消费者应得金额 并且取消 渠道商打款
|
$confirmAdminMoney = $adminMoney;
|
||||||
if(
|
$confirmDistributionMoney = 0;
|
||||||
$business->is_assign == BusinessModel::COMMON_OFF
|
$confirmDistributedMoney = $consumerMoney;
|
||||||
||
|
$confirmAgencyMoney = $adminMoney;
|
||||||
(!empty($coupon->distribution_user_code)&&$coupon->is_distribution_coupon == CouponMain::COMMON_ON)
|
if(isset($business->agency) && $business->agency){
|
||||||
){
|
$agencyRedpackUser = Account::findOne([
|
||||||
$redpackUser = Account::findOne([["user_code","=",$coupon->distribution_user_code]]);
|
["business_code","=",$business->agency->code],
|
||||||
}else{
|
["type","=",Account::type_business],
|
||||||
$redpackUser = $account;
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$adminMoney = 0;
|
|
||||||
$consumerMoney = round($deductionMoney,2) ;
|
|
||||||
$agencyMoney = 0; //四舍五入 精确到分
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//开始数据操作
|
//开始数据操作
|
||||||
Db::startTrans();
|
Db::startTrans();
|
||||||
try {
|
try {
|
||||||
|
@ -659,40 +574,42 @@ class Coupon extends Base
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
// 2. 写入优惠券流水
|
|
||||||
$couponBill = CouponBill::getUserOne( $account->user_code,$coupon->couponMain->id,$coupon->id);
|
// $couponBill = CouponBill::getUserOne($account->user_code,$coupon->couponMain->id,$coupon->id);
|
||||||
if(empty($couponBill)){
|
// if(empty($couponBill)){
|
||||||
$couponBillData = [
|
//2. 写入优惠券流水
|
||||||
"coupon_main_id" => $coupon->couponMain->id,
|
$couponBillData = [
|
||||||
"coupon_id" => $coupon->id,
|
"coupon_main_id" => $coupon->couponMain->id,
|
||||||
"user_code" => $account->user_code,
|
"coupon_id" => $coupon->id,
|
||||||
"business_code" => $business->code,
|
"user_code" => $account->user_code,
|
||||||
"agency_code" => $business->agency_code,
|
"business_code" => $business->code,
|
||||||
"commission_agency" => $coupon->couponMain->commission_agency,
|
"agency_code" => $business->agency_code,
|
||||||
"commission_admin" => $coupon->couponMain->commission_admin,
|
"commission_agency" => $coupon->couponMain->commission_agency,
|
||||||
"commission_consumer" => $coupon->couponMain->commission_consumer,
|
"commission_admin" => $coupon->couponMain->commission_admin,
|
||||||
"commission_dis_admin" => $coupon->couponMain->commission_dis_admin,
|
"commission_consumer" => $coupon->couponMain->commission_consumer,
|
||||||
"commission_dis_distribution" => $coupon->couponMain->commission_dis_distribution,
|
"commission_dis_admin" => $coupon->couponMain->commission_dis_admin,
|
||||||
"commission_dis_distributed" => $coupon->couponMain->commission_dis_distributed,
|
"commission_dis_distribution" => $coupon->couponMain->commission_dis_distribution,
|
||||||
"money" => $coupon->couponMain->money,
|
"commission_dis_distributed" => $coupon->couponMain->commission_dis_distributed,
|
||||||
"agency_money" => $agencyMoney,
|
"money" => $coupon->couponMain->money,
|
||||||
"admin_money" => $adminMoney,
|
"agency_money" => $confirmAgencyMoney,
|
||||||
"consumer_money" => $consumerMoney,
|
"admin_money" => $confirmAdminMoney,
|
||||||
"lat" => $lat,
|
"consumer_money" => $confirmDistributedMoney,
|
||||||
"lng" => $lng,
|
"distribution_money" => $confirmDistributionMoney,
|
||||||
"create_time" => date("Y-m-d H:i:s" ,$time),
|
"lat" => $lat,
|
||||||
];
|
"lng" => $lng,
|
||||||
$couponBill = CouponBill::create($couponBillData);
|
"create_time" => date("Y-m-d H:i:s" ,$time),
|
||||||
}else{
|
];
|
||||||
$couponBillData = [
|
$couponBill = CouponBill::create($couponBillData);
|
||||||
"agency_money" => $agencyMoney,
|
// }else{
|
||||||
"admin_money" => $adminMoney,
|
// $couponBillData = [
|
||||||
"consumer_money" => $consumerMoney,
|
// "agency_money" => $agencyMoney,
|
||||||
"lat" => $lat,
|
// "admin_money" => $adminMoney,
|
||||||
"lng" => $lng,
|
// "consumer_money" => $consumerMoney,
|
||||||
];
|
// "lat" => $lat,
|
||||||
$couponBill->save($couponBillData);
|
// "lng" => $lng,
|
||||||
}
|
// ];
|
||||||
|
// $couponBill->save($couponBillData);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
// 3. 写入商家扣费记录
|
// 3. 写入商家扣费记录
|
||||||
|
@ -710,163 +627,190 @@ class Coupon extends Base
|
||||||
];
|
];
|
||||||
Deduction::create($deductionData);
|
Deduction::create($deductionData);
|
||||||
|
|
||||||
//4. 商家扣钱
|
|
||||||
$business->save(["balance"=>Db::raw("balance - " . $deductionMoney)]);
|
|
||||||
|
|
||||||
|
//4. 商家扣钱 普通商家才扣钱 否则不扣钱
|
||||||
|
if($business->model == BusinessModel::model_ordinary){
|
||||||
|
$business->save(["balance"=>Db::raw("balance - " . $deductionMoney)]);
|
||||||
|
}
|
||||||
|
|
||||||
$payment = WechatPay::getInstance();
|
$payment = WechatPay::getInstance();
|
||||||
|
|
||||||
//5. 渠道商加钱 【只要指派了渠道商 并且没有分销人 渠道商才能加钱 红包方式直接打款 成功失败不管】
|
//5. 渠道商加钱 【只要指派了渠道商 并且没有分销人 渠道商才能加钱 红包方式直接打款 成功失败不管】
|
||||||
if($business->is_assign == BusinessModel::COMMON_ON && (empty($coupon->distribution_user_code))){
|
//$business->agency->inc("balance",$agencyMoney)->update();
|
||||||
if(isset($business->agency) && $business->agency){
|
//修改成直接大款到关联的用户
|
||||||
//$business->agency->inc("balance",$agencyMoney)->update();
|
if($confirmAgencyMoney > 0 && !empty($agencyRedpackUser)) {
|
||||||
if($agencyMoney > 0) {
|
$agencyRedpack = Redpack::getDistributionRedpack( Redpack::userTypeAgency,$agencyRedpackUser->user_code,$coupon->id);
|
||||||
//找到渠道商关联的用户
|
//如果付款过一次了 就用同一个订单号发起
|
||||||
$agencyUser = Account::findOne([
|
if(!empty($agencyRedpack)){
|
||||||
["business_code","=",$business->agency->code],
|
$mch_billno = $agencyRedpack->mch_billno;
|
||||||
["type","=",Account::type_business],
|
|
||||||
]);
|
|
||||||
if(empty($agencyUser)){
|
|
||||||
Db::rollback();
|
|
||||||
return $this->json(4001,"渠道商用户不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
$agencyRedpack = Redpack::getDistributionRedpack($agencyUser->user_code,$coupon->couponMain->id,$account->user_code);
|
|
||||||
//如果付款过一次了 就用同一个订单号发起
|
|
||||||
if(!empty($agencyRedpack)){
|
|
||||||
$mch_billno = $agencyRedpack->mch_billno;
|
|
||||||
}else{
|
|
||||||
$mch_billno = createUuid();
|
|
||||||
}
|
|
||||||
|
|
||||||
$agencyAmount = $agencyMoney * 100 ;
|
|
||||||
|
|
||||||
$agencyToBalanceData = [
|
|
||||||
'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
|
|
||||||
'openid' => $agencyUser->open_id,
|
|
||||||
'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
|
|
||||||
'amount' => $agencyAmount , //单位为分,不小于300
|
|
||||||
'desc' => '用户核销优惠券,渠道商奖励',
|
|
||||||
];
|
|
||||||
|
|
||||||
// 写入红包
|
|
||||||
$agencyRedpackData = [
|
|
||||||
"coupon_id" => 0,
|
|
||||||
"mch_billno" => $mch_billno,
|
|
||||||
"openid" => $agencyUser->open_id,
|
|
||||||
"user_code" => $agencyUser->user_code,
|
|
||||||
"money" => $agencyAmount, //单位为分,不小于300
|
|
||||||
"create_time" => date("Y-m-d H:i:s",$time),
|
|
||||||
"pay_time" => '0000-00-00 00:00:00',
|
|
||||||
|
|
||||||
"user_type" => Redpack::userTypeAgency,
|
|
||||||
"distributed_user_code" => $account->user_code,
|
|
||||||
"distributed_coupon_main_id" => $coupon->couponMain->id,
|
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
//发起支付
|
|
||||||
$agencyResult = $payment->transfer->toBalance($agencyToBalanceData);
|
|
||||||
|
|
||||||
//var_dump($result);
|
|
||||||
|
|
||||||
//付款成功才提交!!!!!伪装成功
|
|
||||||
if( isset($agencyResult['payment_no']) ){
|
|
||||||
$agencyRedpackData['pay_time'] = date("Y-m-d H:i:s",$time);
|
|
||||||
}else{
|
|
||||||
$agencyRedpackData['failure_reason'] = json_encode($agencyResult,JSON_UNESCAPED_UNICODE);
|
|
||||||
//否则失败 记录失败原因
|
|
||||||
Log::info("企业发起付款【渠道商红包】失败:" . json_encode($agencyResult,JSON_UNESCAPED_UNICODE));
|
|
||||||
}
|
|
||||||
// 不管成功还是失败 都要写入
|
|
||||||
Redpack::create($agencyRedpackData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//6. 分销者用户提现到零钱 写入红包记录
|
|
||||||
if($consumerMoney > 0 && !empty($redpackUser)){
|
|
||||||
//如果付款过一次了 就查询付款状态
|
|
||||||
if(isset($coupon->redpack) && $coupon->redpack){
|
|
||||||
$mch_billno = $coupon->redpack->mch_billno;
|
|
||||||
// $result = $payment->transfer->queryBalanceOrder($coupon->redpack->mch_billno);
|
|
||||||
// //var_dump($result);
|
|
||||||
// //企业付款成功
|
|
||||||
// if(isset($result["status"]) && $result["status"]=="SUCCESS"){
|
|
||||||
// Db::commit();
|
|
||||||
// return $this->json(
|
|
||||||
// 0,
|
|
||||||
// "优惠券签到使用成功,您获得{$consumerMoney}元红包,已存入您的零钱",
|
|
||||||
// [
|
|
||||||
// "redpack" =>$consumerMoney
|
|
||||||
// ]);
|
|
||||||
// }
|
|
||||||
// Log::info("查询企业付款失败:".json_encode($result,JSON_UNESCAPED_UNICODE));
|
|
||||||
}else{
|
}else{
|
||||||
$mch_billno = createUuid();
|
$mch_billno = createUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
$amount = $consumerMoney * 100;
|
$agencyPayAmount = $confirmAgencyMoney * 100 ;
|
||||||
|
|
||||||
$toBalanceData = [
|
$agencyToBalanceData = [
|
||||||
'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
|
'partner_trade_no' => $mch_billno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
|
||||||
'openid' => $redpackUser->open_id,
|
'openid' => $agencyRedpackUser->open_id,
|
||||||
'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
|
'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
|
||||||
'amount' => $amount, //单位为分,不小于300
|
'amount' => $agencyPayAmount , //单位为分,不小于300
|
||||||
|
'desc' => '用户核销优惠券,渠道商奖励',
|
||||||
|
];
|
||||||
|
|
||||||
|
// 写入红包
|
||||||
|
$agencyRedpackData = [
|
||||||
|
"coupon_id" => $coupon->id,
|
||||||
|
"mch_billno" => $mch_billno,
|
||||||
|
"openid" => $agencyRedpackUser->open_id,
|
||||||
|
"user_code" => $agencyRedpackUser->user_code,
|
||||||
|
"money" => $agencyPayAmount, //单位为分,不小于300
|
||||||
|
"create_time" => date("Y-m-d H:i:s",$time),
|
||||||
|
"pay_time" => '0000-00-00 00:00:00',
|
||||||
|
"user_type" => Redpack::userTypeAgency,
|
||||||
|
];
|
||||||
|
|
||||||
|
//发起支付
|
||||||
|
$agencyResult = $payment->transfer->toBalance($agencyToBalanceData);
|
||||||
|
//付款成功才提交!!!!!伪装成功
|
||||||
|
if( isset($agencyResult['payment_no']) ){
|
||||||
|
$agencyRedpackData['pay_time'] = date("Y-m-d H:i:s",$time);
|
||||||
|
}else{
|
||||||
|
$agencyRedpackData['failure_reason'] = json_encode($agencyResult,JSON_UNESCAPED_UNICODE);
|
||||||
|
//否则失败 记录失败原因
|
||||||
|
Log::info("企业发起付款【渠道商红包】失败:" . json_encode($agencyResult,JSON_UNESCAPED_UNICODE));
|
||||||
|
}
|
||||||
|
// 不管成功还是失败 都要写入
|
||||||
|
if(empty($agencyRedpack)){
|
||||||
|
Redpack::create($agencyRedpackData);
|
||||||
|
}else{
|
||||||
|
$agencyRedpack->save($agencyRedpackData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//6. 核销用户提现到零钱 写入红包记录
|
||||||
|
if($confirmDistributedMoney > 0 && !empty($consumerRedpackUser)){
|
||||||
|
$distributedRedpack = Redpack::getDistributionRedpack( Redpack::userTypeDistributed,$agencyRedpackUser->user_code,$coupon->id);
|
||||||
|
//如果付款过一次了 就查询付款状态
|
||||||
|
if(!empty($distributedRedpack)){
|
||||||
|
$consumerMchBillno = $distributedRedpack->mch_billno;
|
||||||
|
}else{
|
||||||
|
$consumerMchBillno = createUuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
$consumerAmount = $confirmDistributedMoney * 100;
|
||||||
|
|
||||||
|
$consumerToBalanceData = [
|
||||||
|
'partner_trade_no' => $consumerMchBillno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
|
||||||
|
'openid' => $consumerRedpackUser->open_id,
|
||||||
|
'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
|
||||||
|
'amount' => $consumerAmount, //单位为分,不小于300
|
||||||
'desc' => '验证优惠券签到奖励',
|
'desc' => '验证优惠券签到奖励',
|
||||||
];
|
];
|
||||||
|
|
||||||
// 写入红包
|
// 写入红包
|
||||||
$redpackData = [
|
$consumerRedpackData = [
|
||||||
"coupon_id" => $coupon->id,
|
"coupon_id" => $coupon->id,
|
||||||
"mch_billno" => $mch_billno,
|
"mch_billno" => $consumerMchBillno,
|
||||||
"openid" => $redpackUser->open_id,
|
"openid" => $consumerRedpackUser->open_id,
|
||||||
"user_code" => $redpackUser->user_code,
|
"user_code" => $consumerRedpackUser->user_code,
|
||||||
"money" => $amount, //单位为分,不小于300
|
"money" => $consumerAmount, //单位为分,不小于300
|
||||||
"create_time" => date("Y-m-d H:i:s",$time),
|
"create_time" => date("Y-m-d H:i:s",$time),
|
||||||
"pay_time" => '0000-00-00 00:00:00',
|
"pay_time" => '0000-00-00 00:00:00',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
//发起支付
|
//发起支付
|
||||||
$result = $payment->transfer->toBalance($toBalanceData);
|
$consumerResult = $payment->transfer->toBalance($consumerToBalanceData);
|
||||||
|
//付款成功才提交!!!!!伪装成功
|
||||||
|
if( isset($consumerResult['payment_no']) ){
|
||||||
|
$returnConsumerMoney = $confirmDistributedMoney;
|
||||||
|
$consumerRedpackData['pay_time'] = date("Y-m-d H:i:s",$time);
|
||||||
|
}else{
|
||||||
|
$consumerRedpackData['failure_reason'] = json_encode($consumerResult,JSON_UNESCAPED_UNICODE);
|
||||||
|
//否则失败 记录失败原因
|
||||||
|
Log::info("企业发起付款【渠道商红包】失败:" . json_encode($consumerResult,JSON_UNESCAPED_UNICODE));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(empty($distributedRedpack)){
|
||||||
|
Redpack::create($consumerRedpackData);
|
||||||
|
}else{
|
||||||
|
$distributedRedpack->save($consumerRedpackData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//7 分销者用户提现到零钱 写入红包记录
|
||||||
|
if($confirmDistributionMoney > 0 && !empty($distributionRedpackUser)){
|
||||||
|
|
||||||
|
$distributionRedpack = Redpack::getDistributionRedpack( Redpack::userTypeDistribution,$agencyRedpackUser->user_code,$coupon->id);
|
||||||
|
//如果付款过一次了 就用同一个订单号发起
|
||||||
|
if(!empty($distributionRedpack)){
|
||||||
|
$distributionMchBillno = $distributionRedpack->mch_billno;
|
||||||
|
}else{
|
||||||
|
$distributionMchBillno = createUuid();
|
||||||
|
}
|
||||||
|
|
||||||
|
$distributionToBalanceData = [
|
||||||
|
'partner_trade_no' => $distributionMchBillno,// 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
|
||||||
|
'openid' => $distributionRedpackUser->open_id,
|
||||||
|
'check_name' => 'NO_CHECK',// NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
|
||||||
|
'amount' => $confirmDistributionMoney * 100, //单位为分,不小于300
|
||||||
|
'desc' => '优惠券分销奖励',
|
||||||
|
];
|
||||||
|
|
||||||
|
// 写入红包
|
||||||
|
$distributionRedpackData = [
|
||||||
|
"coupon_id" => 0,
|
||||||
|
"mch_billno" => $distributionMchBillno,
|
||||||
|
"openid" => $distributionRedpackUser->open_id,
|
||||||
|
"user_code" => $distributionRedpackUser->user_code,
|
||||||
|
"money" => $confirmDistributionMoney * 100, //单位为分,不小于300
|
||||||
|
"create_time" => date("Y-m-d H:i:s",$time),
|
||||||
|
"pay_time" => '0000-00-00 00:00:00',
|
||||||
|
"user_type" => Redpack::userTypeDistribution,
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
//发起支付
|
||||||
|
$distributionResult = $payment->transfer->toBalance($distributionToBalanceData);
|
||||||
|
|
||||||
//var_dump($result);
|
//var_dump($result);
|
||||||
|
|
||||||
//付款成功才提交!!!!!伪装成功
|
//付款成功才提交!!!!!伪装成功
|
||||||
if( isset($result['payment_no']) ){
|
if( isset($distributionResult['payment_no']) ){
|
||||||
$redpackData['pay_time'] = date("Y-m-d H:i:s",$time);
|
$distributionRedpackData['pay_time'] = date("Y-m-d H:i:s",$time);
|
||||||
Redpack::create($redpackData);
|
if(empty($distributionRedpack)){
|
||||||
Db::commit();
|
Redpack::create($distributionRedpackData);
|
||||||
return $this->json(
|
}else{
|
||||||
0,
|
$distributionRedpack->save($distributionRedpackData);
|
||||||
"优惠券签到使用成功,您获得{$consumerMoney}元红包,已存入您的零钱",
|
}
|
||||||
[
|
|
||||||
"redpack" =>$consumerMoney,
|
}else{
|
||||||
"user_coupon_id" =>$couponId,
|
$distributionRedpackData['failure_reason'] = json_encode($distributionResult,JSON_UNESCAPED_UNICODE);
|
||||||
]);
|
//否则失败 记录失败原因
|
||||||
|
Log::info("企业发起付款【领券分销者红包】失败:" . json_encode($distributionResult,JSON_UNESCAPED_UNICODE));
|
||||||
|
//默认回滚
|
||||||
|
Db::rollback();
|
||||||
|
//失败也要写入红包记录
|
||||||
|
if(empty($distributionRedpack)){
|
||||||
|
Redpack::create($distributionRedpackData);
|
||||||
|
}else{
|
||||||
|
$distributionRedpack->save($distributionRedpackData);
|
||||||
|
}
|
||||||
|
return $this->json(5003, "领取失败");
|
||||||
}
|
}
|
||||||
$redpackData['failure_reason'] = json_encode($result,JSON_UNESCAPED_UNICODE);
|
|
||||||
//否则失败 记录失败原因
|
|
||||||
Log::info("企业发起付款【消费者红包】失败:" . json_encode($result,JSON_UNESCAPED_UNICODE));
|
|
||||||
//默认回滚
|
|
||||||
Db::rollback();
|
|
||||||
//失败也要写入红包记录
|
|
||||||
Redpack::create($redpackData);
|
|
||||||
return $this->json(5003, "验证失败,发放红包失败");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Db::commit();
|
Db::commit();
|
||||||
return $this->json(
|
return $this->json(
|
||||||
0,
|
0,
|
||||||
"优惠券签到使用成功,您获得{$consumerMoney}元红包,已存入您的零钱",
|
"优惠券签到使用成功,您获得{$returnConsumerMoney}元红包,已存入您的零钱",
|
||||||
[
|
[
|
||||||
"redpack" =>$consumerMoney,
|
"redpack" =>$returnConsumerMoney,
|
||||||
"user_coupon_id" =>$couponId,
|
"user_coupon_id" =>$couponId,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
}catch (RepositoryException $e){
|
}catch (RepositoryException $e){
|
||||||
Db::rollback();
|
Db::rollback();
|
||||||
return $this->json(5001, "Repository服务器错误");
|
return $this->json(5001, "Repository服务器错误");
|
||||||
|
|
|
@ -5,23 +5,22 @@ namespace app\model;
|
||||||
class Redpack extends Base
|
class Redpack extends Base
|
||||||
{
|
{
|
||||||
|
|
||||||
const userTypeDistribution = "distribution";//领券分销者
|
public const userTypeDistribution = "distribution";//分销者
|
||||||
const userTypeDistributed = "distributed";//核销分销者
|
public const userTypeDistributed = "distributed";//被分销者
|
||||||
const userTypeAgency = "agency";// 渠道商
|
public const userTypeAgency = "agency"; // 渠道商
|
||||||
/**
|
/**
|
||||||
* 获取分销红包记录
|
* 获取分销红包记录
|
||||||
* @param $distributionUserCode 分销人user_code
|
* @param string $userType 红包获取人的角色
|
||||||
* @param $couponMainId 分销的couponMain 优惠券id
|
* @param string $distributionUserCode 分销人/渠道商/被分销者 user_code
|
||||||
* @param $distributedUserCode 被分销人user_code
|
* @param mixed $couponMainId 分销的couponMain 优惠券id
|
||||||
* 以上三个参数就可以确定 分销数据的唯一性
|
* 以上三个参数就可以确定 分销数据的唯一性
|
||||||
* */
|
* */
|
||||||
public static function getDistributionRedpack($distributionUserCode,$couponMainId,$distributedUserCode)
|
public static function getDistributionRedpack($userType,$distributionUserCode,$couponMainId)
|
||||||
{
|
{
|
||||||
return self::where([
|
return self::where([
|
||||||
["user_type","=",self::userTypeDistribution],
|
["user_type","=",$userType],
|
||||||
["user_code","=",$distributionUserCode],
|
["user_code","=",$distributionUserCode],
|
||||||
["distributed_user_code","=",$distributedUserCode],
|
["coupon_id","=",$couponMainId],
|
||||||
["distributed_coupon_main_id","=",$distributedUserCode],
|
|
||||||
])
|
])
|
||||||
->find();
|
->find();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue