From d9e24c9e3958ebeb43f0fc879305aaca89961bd3 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Tue, 15 Mar 2022 15:10:41 +0800 Subject: [PATCH] setter --- app/controller/api/Coupon.php | 69 ++++++++++++++++++++++++++++++++++- app/model/Redpack.php | 19 ++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/app/controller/api/Coupon.php b/app/controller/api/Coupon.php index 0685f16..4fd9186 100644 --- a/app/controller/api/Coupon.php +++ b/app/controller/api/Coupon.php @@ -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){ diff --git a/app/model/Redpack.php b/app/model/Redpack.php index 2b68cae..c52db38 100644 --- a/app/model/Redpack.php +++ b/app/model/Redpack.php @@ -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(); + } }