/** * get coupon detail info */ public function actionOpenCoupon() { $couponId = $this->getQuery("couponId"); $memberId = $this->getQuery("memberId"); $isReceived = true; $message = ''; if (empty($couponId)) { throw new InvalidParameterException(Yii::t('common', 'parameters_missing')); } $coupon = Coupon::findByPk(new MongoId($couponId)); if (empty($coupon)) { throw new InvalidParameterException(Yii::t('product', 'membershipDiscount_is_deleted')); } // check expired $current = new MongoDate(strtotime(date('Y-m-d'))); if ($coupon->time['type'] == Coupon::COUPON_ABSOLUTE_TIME && $coupon->time['endTime'] < $current) { $isReceived = false; $message = Yii::t('product', 'coupon_expired'); } //check total if ($coupon->total <= 0) { $isReceived = false; $message = Yii::t('product', 'coupon_no_exists'); } if (!empty($memberId)) { $where = ['couponId' => new MongoId($couponId), 'member.id' => new MongoId($memberId)]; $couponNum = CouponLog::count($where); if ($couponNum >= $coupon->limit) { $isReceived = false; $message = Yii::t('product', 'coupon_is_received'); } } return array_merge($coupon->toArray(), ['isReceived' => $isReceived, 'message' => $message]); }
/** * receive copon through oauth recall this api */ public function actionReceivedCoupon() { $params = $this->getQuery(); $defaultId = -1; $message = ''; if (empty($params['couponId']) || empty($params['memberId']) || empty($params['channelId'])) { LogUtil::error(['message' => 'missing params when receive coupon', 'params' => $params], 'product'); exit; } $number = !empty($params['number']) && intval($params['number']) > 1 ? intval($params['number']) : 1; $couponId = new MongoId($params['couponId']); $coupon = Coupon::findByPk($couponId); if (empty($coupon)) { LogUtil::error(['message' => 'invalid couponIdi when receive coupon', 'params' => $params], 'product'); exit; } $memberId = new MongoId($params['memberId']); $member = Member::findByPk($memberId); if (empty($member)) { LogUtil::error(['message' => 'invalid memberId when receive coupon', 'params' => $params], 'product'); exit; } $args = ['mainDomain' => Yii::$app->request->hostInfo . '/mobile/product/coupon', 'couponId' => $params['couponId'], 'id' => $defaultId, 'memberId' => $params['memberId'], 'result' => 'fail', 'channelId' => $params['channelId']]; //check the total if ($coupon->total < $number) { $message = Yii::t('product', 'coupon_no_exists'); return $this->_redirectCouponDetail($message, $args, $params); } //check limit $couponNumber = CouponLog::count(['couponId' => $couponId, 'member.id' => $memberId]); if ($couponNumber >= $coupon->limit) { $message = Yii::t('product', 'coupon_is_received'); return $this->_redirectCouponDetail($message, $args, $params); } //check the time $current = new MongoDate(strtotime(date('Y-m-d'))); if ($coupon->time['type'] == Coupon::COUPON_RELATIVE_TIME && $coupon->time['endTime'] < $current) { $message = Yii::t('product', 'coupon_expired'); return $this->_redirectCouponDetail($message, $args, $params); } //receive coupon $where = ['total' => ['$gte' => $number], '_id' => $couponId]; $number -= 2 * $number; if (Coupon::updateAll(['$inc' => ['total' => $number]], $where)) { $membershipDiscount = MembershipDiscount::transformMembershipDiscount($coupon, $member); if (false === $membershipDiscount->save()) { //to avoid the error show to user LogUtil::error(['message' => 'Failed to save couponLog error:' . $membershipDiscount->getErrors()], 'product'); $message = Yii::t('common', 'save_fail'); return $this->_redirectCouponDetail($message, $args, $params); } $args['id'] = isset($membershipDiscount->_id) ? $membershipDiscount->_id : $defaultId; $args['result'] = 'success'; } else { $message = '优惠券库存不足!'; } return $this->_redirectCouponDetail($message, $args, $params); }