diff --git a/composer.json b/composer.json index 73523b4..27275af 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,8 @@ "RuLong\\Bonus\\": "packages/bonus/src/", "RuLong\\Identity\\": "packages/identity/src/", "XuanChen\\Coupon\\": "packages/coupon/src/", - "XuanChen\\UnionPay\\": "packages/unionpay/src/" + "XuanChen\\UnionPay\\": "packages/unionpay/src/", + "XuanChen\\Sinopec\\": "packages/sinopec/src/" }, "classmap": [ "database/seeds", diff --git a/packages/sinopec/composer.json b/packages/sinopec/composer.json index ac5e154..f167c49 100644 --- a/packages/sinopec/composer.json +++ b/packages/sinopec/composer.json @@ -1,6 +1,6 @@ { - "name": "xuanchen/coupon", - "description": "卡券相关", + "name": "xuanchen/sinopec", + "description": "中石化相关", "license": "MIT", "authors": [ { @@ -14,13 +14,13 @@ }, "autoload": { "psr-0": { - "XuanChen\\Coupon": "src/" + "XuanChen\\Sinopec": "src/" } }, "extra": { "laravel": { "providers": [ - "XuanChen\\Coupon\\ServiceProvider" + "XuanChen\\Sinopec\\ServiceProvider" ] } } diff --git a/packages/sinopec/src/Action/PinganAction.php b/packages/sinopec/src/Action/PinganAction.php deleted file mode 100644 index 8dc68b1..0000000 --- a/packages/sinopec/src/Action/PinganAction.php +++ /dev/null @@ -1,55 +0,0 @@ -setCode($this->redemptionCode) - ->setUser($this->user) - ->setOutletId($this->outletId) - ->setTotal($this->total) - ->setOrderId($this->orderid) - ->start(); - } - - /** - * Notes: 查询卡券详情 - * @Author: 玄尘 - * @Date : 2020/6/29 15:15 - * @return mixed - */ - public function detail() - { - $info = (new Query)->setOutletId($this->outletId) - ->setCode($this->redemptionCode) - ->start(); - - return $info; - } - - //发券 - function grant() - { - return '没这个接口'; - } - - //作废 - function destroy() - { - return '没这个接口'; - } - -} diff --git a/packages/sinopec/src/Action/YsdAction.php b/packages/sinopec/src/Action/YsdAction.php deleted file mode 100644 index 6817c8d..0000000 --- a/packages/sinopec/src/Action/YsdAction.php +++ /dev/null @@ -1,91 +0,0 @@ -setActivityId($this->activityId) - ->setOutletId($this->outletId) - ->setMobile($this->mobile) - ->start(); - } - - /** - * Notes: 查询卡券详情 - * @Author: 玄尘 - * @Date : 2020/6/29 15:15 - * @return mixed - */ - public function detail() - { - $query_coupon = (new YsdQuery)->setOutletId($this->outletId) - ->setCode($this->redemptionCode) - ->start(); - - if (!is_string($query_coupon)) { - return [ - 'name' => $query_coupon->activity->title, - 'code' => $query_coupon->code, - 'full' => $query_coupon->full, - 'price' => $query_coupon->price, - 'status' => $query_coupon->status, - 'used_at' => (string)$query_coupon->used_at, - 'startTime' => (string)$query_coupon->start_at, - 'endTime' => (string)$query_coupon->end_at, - - ]; - } - - return $query_coupon; - } - - /** - * Notes: 作废 - * @Author: 玄尘 - * @Date : 2020/7/21 11:32 - */ - public function destroy() - { - return $res = (new YsdDestroy)->setCode($this->redemptionCode) - ->setOutletId($this->outletId) - ->start(); - } - - /** - * Notes: 核销执行入口 - * @Author: 玄尘 - * @Date : 2020/6/29 14:49 - * @return mixed - */ - public function start() - { - return $res = (new YsdVerification)->setCode($this->redemptionCode) - ->setUser($this->user) - ->setOutletId($this->outletId) - ->setTotal($this->total) - ->setOrderId($this->orderid) - ->start(); - } - -} diff --git a/packages/sinopec/src/Action/pingan/PingAnInit.php b/packages/sinopec/src/Action/pingan/PingAnInit.php deleted file mode 100644 index 1065ac0..0000000 --- a/packages/sinopec/src/Action/pingan/PingAnInit.php +++ /dev/null @@ -1,205 +0,0 @@ -this_type = config('pingan.this_type'); - $pingan = config('pingan.' . $this->this_type); - - $this->baseUri = $pingan['Uri']; - $this->tokenUri = $pingan['tokenUri']; - $this->client_id = $pingan['client_id']; - $this->grant_type = $pingan['grant_type']; - $this->userName = $pingan['userName']; - $this->client_secret = $pingan['client_secret']; - $this->aes_code = $pingan['AES_CODE']; - } - - /** - * 获取access_token - * @return void [type] [description] - */ - public function getToken() - { - //从数据库里找token - $token = PinganToken::where('type', $this->this_type)->orderBy('id', 'desc')->first(); - - if ($token) { - $access_token = $token->access_token; - $expires_in = $token->expires_in; - $get_token_time = $token->get_token_time; - $diffMinutes = $get_token_time->diffInMinutes(now(), false); - if ($diffMinutes < $expires_in) { - $this->access_token = $access_token; - } else { - $this->getAjaxToken(); - } - } else { - $this->getAjaxToken(); - } - } - - /** - * 获取毫秒级别的时间戳 - */ - public function getMsecTime() - { - [$msec, $sec] = explode(' ', microtime()); - $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); - $msectime = explode('.', $msectime); - - return $msectime[0]; - } - - /** - * 请求平台 access_token - * @return void [type] [description] - */ - public function getAjaxToken() - { - $params = [ - 'client_id' => $this->client_id, - 'grant_type' => $this->grant_type, - 'client_secret' => $this->client_secret, - ]; - - try { - $log = $this->createLog($this->tokenUri, 'POST', $params, 'pingan'); - - $client = new Client(); - $response = $client->request('POST', $this->tokenUri, [ - 'form_params' => $params, - ]); - $body = $response->getBody(); - $content = $body->getContents(); - $result = json_decode($content, true); - - $this->updateLog($log, $result); //更新日志 - - if ($result['ret'] > 0) { - $this->error = $result['msg']; - } else { - $data = $result['data']; - PinganToken::create([ - 'type' => $this->this_type, - 'access_token' => $data['access_token'], - 'expires_in' => $data['expires_in'], - 'get_token_time' => now(), - ]); - $this->access_token = $data['access_token']; - $this->error = ''; - } - } catch (RequestException $e) { - $this->error = $e->getMessage(); - $this->updateLog($log, [$this->error]); //更新日志 - } - - } - - /** - * 通用获取数据接口 - * @param [type] $url 请求地址 - * @param array $query 传递参数 - * @param array $json 需要传的json数据 - * @param string $method 方式 - * @return array|mixed [type] [description] - */ - public function getPingAnData($url, $query = [], $json = [], $method = 'POST') - { - $this->getToken(); - - if ($this->error) { - return $this->error; - } - - $postData = [ - 'query' => array_merge([ - 'access_token' => $this->access_token, - 'request_id' => $this->getMsecTime(), - 'userName' => $this->userName, - ], $query), - 'json' => $json, - 'headers' => [ - 'Content-Type' => 'application/json;charset=utf-8', - 'accept' => 'application/json;charset=utf-8', - ], - ]; - - $log = $this->createLog($url, $method, $postData, 'pingan'); //日志 - - try { - $client = new Client(); - $response = $client->request($method, $url, $postData); - $body = $response->getBody(); - $content = $body->getContents(); - $result = json_decode($content, true); - - if ($result['ret'] > 0) { - $retData = $result['msg']; - } else { - $retData = $result['data']; - } - $this->updateLog($log, $retData);//更新日志 - - return $retData; - } catch (RequestException $e) { - $this->updateLog($log, [$e->getMessage()]);//更新日志 - - return ['ret' => '99999', $e->getMessage()]; - } - } - - //加密 - public function encrypt($str) - { - if (is_array($str)) { - $str = json_encode($str); - } - $data = openssl_encrypt($str, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA); - - return base64_encode($data); - } - - //解密 - public function decrypt($str) - { - $encrypted = base64_decode($str); - - return openssl_decrypt($encrypted, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA); - } - -} diff --git a/packages/sinopec/src/Action/pingan/Query.php b/packages/sinopec/src/Action/pingan/Query.php deleted file mode 100644 index edd1734..0000000 --- a/packages/sinopec/src/Action/pingan/Query.php +++ /dev/null @@ -1,45 +0,0 @@ -outletId)->first(); - - if (!$outlet) { - throw new \Exception('网点编号错误,未查询到网点信息'); - } - - $url = $this->baseUri . 'partner/v2/coupondetail'; - $params = [ - 'redemptionCode' => $this->redemptionCode, - 'outletNo' => $outlet->PaOutletId, - 'thirdOutletNo' => $outlet->outlet_id, - ]; - - $res = $this->getPingAnData($url, $params); - - if (!is_array($res)) { - throw new \Exception($res); - } - - if ($res['code'] != 200) { - throw new \Exception($res['message']); - } - - return collect($res['data']); - } catch (\Exception $e) { - return $e->getMessage(); - } - - } - -} \ No newline at end of file diff --git a/packages/sinopec/src/Action/pingan/Verification.php b/packages/sinopec/src/Action/pingan/Verification.php deleted file mode 100644 index bdcafbb..0000000 --- a/packages/sinopec/src/Action/pingan/Verification.php +++ /dev/null @@ -1,298 +0,0 @@ -HasCheck(); - if ($res !== false) { - return $res; - } - - //查询卡券信息 - $this->query_coupon = (new Query)->setOutletId($this->outletId) - ->setCode($this->redemptionCode) - ->start(); - if (is_string($this->query_coupon)) { - return $this->query_coupon; - } - - //校验卡券 - $ticket = $this->checkCoupon(); - if (!is_array($ticket)) { - return $ticket; - } - - //检查可核销次数 - $ret = $this->CheckCount(); - if ($ret !== true) { - return $ret; - } - - //增加核销记录 - $coupon = $this->AddCoupon(); - if (is_string($coupon)) { - return $coupon; - } - - try { - $params = [ - 'couponNo' => $coupon->redemptionCode, - 'partnerOrderId' => date('ymdHis') . sprintf("%0" . strlen(999999) . "d", mt_rand(0, 999999)), - 'outletId' => $coupon->PaOutletId, - 'productId' => $coupon->productId, - 'timestamp' => $this->getMsecTime(), - ]; - - $url = $this->baseUri . 'partner/redemption'; - $str = $this->encrypt($params); - $res = $this->getPingAnData($url, [], ['data' => $str]); - - if (!is_array($res)) { - $coupon->remark = $res; - $coupon->status = 3; - $coupon->save(); - throw new \Exception($res); - } - - if ($res['code'] != 200) { - $coupon->remark = $res['code'] . '--' . $res['message']; - $coupon->status = 3; - $coupon->save(); - throw new \Exception($res['message']); - } - - $data = $res['data']; - - $coupon->remark = $data['message']; - $coupon->status = ($data['status'] == 1) ? 2 : 3; - $coupon->save(); - - //返回的数据 - $resdata = [ - 'price' => $coupon->price, - 'name' => $coupon->couponName, - 'total' => $coupon->total, - ]; - - //核销成功 执行分润 - $coupon->profit(); - - return $resdata; - - } catch (Exception $e) { - $coupon->status = 3; - $coupon->remark = '核销失败 ' . $e->getMessage(); - $coupon->save(); - - return $coupon->remark; - } - - } - - /** - * Notes: 如可核销记录 - * @Author: 玄尘 - * @Date : 2020/7/21 15:03 - * @return string - */ - public function AddCoupon() - { - try { - $couponData = [ - 'user_id' => $this->user->id, - 'type' => Coupon::TYPE_PINGAN, - 'outletId' => $this->outletId, - 'orderid' => $this->orderid, - 'PaOutletId' => $this->queryData['PaOutletId'], - 'redemptionCode' => $this->redemptionCode, - 'productId' => $this->queryData['productId'], - 'thirdPartyGoodsId' => $this->queryData['thirdPartyGoodsId'], - 'couponName' => $this->query_coupon['couponName'], - 'price' => $this->ticket['price'], - 'total' => $this->total, - 'profit' => $this->ticket['profit'], - 'status' => 0, - 'startTime' => $this->query_coupon['startTime'], - 'endTime' => $this->query_coupon['endTime'], - ]; - - return $coupon = Coupon::create($couponData); - - } catch (Exception $e) { - return $e->getMessage(); - } - - } - - /** - * Notes: 检查卡券信息 - * @Author: 玄尘 - * @Date : 2020/6/29 16:40 - * @return string - */ - public function checkCoupon() - { - - //检查卡券是否已被核销 - if ($this->query_coupon['status'] > 0) { - if (isset(config('pingan.coupon_status')[$this->query_coupon['status']])) { - return '核销失败,平安券' . config('pingan.coupon_status')[$this->query_coupon['status']]; - } - - return '核销失败,平安券不可用。'; - } - - $startTime = Carbon::parse($this->query_coupon['startTime']); - $endTime = Carbon::parse($this->query_coupon['endTime']); - $now = now(); - - if ($startTime->gt($now)) { - return '核销失败,平安券未开始使用。'; - } - - if ($now->gt($endTime)) { - return '核销失败,平安券已过期。'; - } - - //查找适配的商品id 和 网点id - $pinganData = $this->getPinganProduct(); - - if (is_string($pinganData)) { - return $pinganData; - } - - //获取相关优惠信息 - return $this->checkCode(); - - } - - /** - * 校验平安券编号 - * @return array|string [type] [description] - */ - public function checkCode() - { - $code = $this->user->code->where('code', $this->queryData['thirdPartyGoodsId'])->first(); - if (!$code) { - return "核销失败,未找到此项平安券规则,请联系管理人员检查渠道配置。"; - } - - $ticket = explode('-', $this->queryData['thirdPartyGoodsId']); - - if (!is_array($ticket) || count($ticket) != 3) { - return "核销失败,平安券规则格式不正确。"; - } - - $full = $ticket[1]; //full100 - $price = $ticket[2]; - preg_match('/\d+/', $full, $result); - - if (empty($result) || !is_array($result)) { - return "核销失败,平安券规则格式不正确。"; - } - - if (!is_numeric($this->total)) { - return "核销失败,订单金额必须是数字"; - } - if ($result[0] > $this->total) { - return '核销失败,订单金额不足,平安券不可使用。'; - } - - $this->ticket = [ - 'total' => $result[0], - 'price' => $price, - 'profit' => $code->profit, - ]; - - return $this->ticket; - } - - /** - * 查找平安商品id - * @author 玄尘 2020-04-04 - * @return array|string [type] [description] - */ - public function getPinganProduct() - { - //查询网点是否存在 - $outlet = User::where('outlet_id', $this->outletId)->first(); - - if (!$outlet) { - return '核销失败,网点编号错误,未查询到网点信息'; - } - - $PaOutletId = $outlet->PaOutletId; - $outlet_id = $outlet->outlet_id; - $profitOfferItemVersion = $this->query_coupon['profitOfferItemVersion']; - - if (!$PaOutletId && $profitOfferItemVersion != 1) { - return '核销失败,参数错误,渠道信息缺少平安网点id'; - } - - $productItemList = $this->query_coupon['productItemList']; - - if (!is_array($productItemList) || !is_array($productItemList[0])) { - return '核销失败,平安券数据有误,可能是未配置网点。'; - } - - //循环查找 - $first = ''; - foreach ($productItemList as $key => $item) { - $productId = $item['productId']; - $thirdPartyGoodsId = $item['thirdPartyGoodsId']; - $outletList = $item['outletList']; - if (!is_array($outletList) || !is_array($outletList[0])) { - return '核销失败,网点信息有误!请检查平安券配置信息。'; - break; - } - - $outletList = collect($outletList); - //判断是新版还是旧版 - if ($profitOfferItemVersion) { - //新版通过第三方查询 - $first = $outletList->firstWhere('thirdOutletNo', $outlet_id); - - if ($first) { - break; - } - } else { - //旧版通过平安网点查询 - $first = $outletList->firstWhere('outletNo', $PaOutletId); - if ($first) { - break; - } - } - } - - if (!$first) { - return '核销失败,未找到可用网点信息。'; - } - - if (!$thirdPartyGoodsId) { - return '核销失败,平安券编号规则有误。'; - } - - if (!$productId) { - return '核销失败,未查询到平安券商品id。'; - } - - return $this->queryData = [ - 'thirdPartyGoodsId' => $thirdPartyGoodsId, - 'productId' => $productId, - 'PaOutletId' => $first['outletNo'], - ]; - - } - -} diff --git a/packages/sinopec/src/Action/sinopec/ZhyCreate.php b/packages/sinopec/src/Action/sinopec/ZhyCreate.php index c599f77..9a290b6 100644 --- a/packages/sinopec/src/Action/sinopec/ZhyCreate.php +++ b/packages/sinopec/src/Action/sinopec/ZhyCreate.php @@ -1,11 +1,12 @@ "ceshi2", "voucherSource" => "S01", "voucherStartTime" => "2019-12-13", - "voucherStartTime_type" => "VOUCHER_START_TIME_TYPE_ABSOLUTE_EFFECTIVE_TIME" - ] + "voucherStartTime_type" => "VOUCHER_START_TIME_TYPE_ABSOLUTE_EFFECTIVE_TIME", + ], ], "limitFrequency" => "", "mabIsRefund" => "", @@ -53,8 +54,8 @@ class ZhyCreate implements CheckCouponContracts "ruleCode" => "$01-VC-1573468710381900", "ruleDetail" => "123123", "ruleName" => "ceshi2", - "voucherType" => "C01" - ] + "voucherType" => "C01", + ], ], "tradLimit" => [ [ @@ -67,9 +68,9 @@ class ZhyCreate implements CheckCouponContracts "operator" => "", "startValue" => "", "tradingUnit" => "", - "transactionType" => "" - ] - ] + "transactionType" => "", + ], + ], ]; } catch (\Exception $e) { diff --git a/packages/sinopec/src/Action/sinopec/ZhyQuery.php b/packages/sinopec/src/Action/sinopec/ZhyQuery.php index 2692f0d..726c12f 100644 --- a/packages/sinopec/src/Action/sinopec/ZhyQuery.php +++ b/packages/sinopec/src/Action/sinopec/ZhyQuery.php @@ -1,11 +1,12 @@ "ceshi2", "voucherSource" => "S01", "voucherStartTime" => "2019-12-13", - "voucherStartTime_type" => "VOUCHER_START_TIME_TYPE_ABSOLUTE_EFFECTIVE_TIME" - ] + "voucherStartTime_type" => "VOUCHER_START_TIME_TYPE_ABSOLUTE_EFFECTIVE_TIME", + ], ], "limitFrequency" => "", "mabIsRefund" => "", @@ -53,8 +54,8 @@ class ZhyQuery implements CheckCouponContracts "ruleCode" => "$01-VC-1573468710381900", "ruleDetail" => "123123", "ruleName" => "ceshi2", - "voucherType" => "C01" - ] + "voucherType" => "C01", + ], ], "tradLimit" => [ [ @@ -67,9 +68,9 @@ class ZhyQuery implements CheckCouponContracts "operator" => "", "startValue" => "", "tradingUnit" => "", - "transactionType" => "" - ] - ] + "transactionType" => "", + ], + ], ]; } catch (\Exception $e) { diff --git a/packages/sinopec/src/Action/ysd/YsdDestroy.php b/packages/sinopec/src/Action/ysd/YsdDestroy.php deleted file mode 100644 index dd4c4f0..0000000 --- a/packages/sinopec/src/Action/ysd/YsdDestroy.php +++ /dev/null @@ -1,61 +0,0 @@ -redemptionCode) { - try { - if (!$this->outletId) { - throw new \Exception('缺少网点id'); - } - - $info = ActivityCoupon::where('code', $this->redemptionCode)->first(); - - if (!$info) { - throw new \Exception('未查询到卡券信息'); - } - - $outlet = User::where('outlet_id', $this->outletId)->first(); - - if (empty($outlet)) { - return '作废失败,未查询到此网点信息。'; - } - - $grants = $info->activity->grants()->pluck('user_id'); - if ($grants->isEmpty()) { - return '作废失败,此活动还没有配置可发券渠道,请联系相关人员进行配置。'; - } - - if (!in_array($outlet->parent_id, $grants->toArray())) { - return '作废失败,您没有权限作废此优惠券。'; - } - - if (!$info->canDestroy()) { - throw new \Exception('作废失败,' . $info->status_text . '不能操作'); - } - - $info->status = ActivityCoupon::STATUS_CLOSE; - $info->save(); - - event(new ConponCallback($info)); - - return true; - } catch (\Exception $e) { - return $e->getMessage(); - } - } else { - return '未获取到券码。'; - } - - } - -} \ No newline at end of file diff --git a/packages/sinopec/src/Action/ysd/YsdGrant.php b/packages/sinopec/src/Action/ysd/YsdGrant.php deleted file mode 100644 index 1c3b452..0000000 --- a/packages/sinopec/src/Action/ysd/YsdGrant.php +++ /dev/null @@ -1,59 +0,0 @@ -activityId)->first(); - if (!$activity) { - return '发券失败,没有找到这个活动。'; - } - - if (!$activity->status) { - return '发券失败,活动已经关闭。'; - } - - $outlet = User::where('outlet_id', $this->outletId)->first(); - if (!$outlet) { - return '发券失败,未查询到此网点信息。'; - } - - $grants = $activity->grants()->pluck('user_id'); - if ($grants->isEmpty()) { - return '发券失败,此活动还没有配置可发券渠道,请联系相关人员进行配置。'; - } - - if (!in_array($outlet->parent_id, $grants->toArray())) { - return '发券失败,您没有权限发此类优惠券。'; - } - - $coupon = $activity->grant($this->mobile, $this->outletId); - - if (!is_string($coupon)) { - return [ - 'name' => $activity->title, - 'code' => $coupon->code, - 'full' => $coupon->full, - 'price' => $coupon->price, - 'startTime' => $coupon->start_at->format('Y-m-d H:i:s'), - 'endTime' => $coupon->end_at->format('Y-m-d H:i:s'), - ]; - } - - return $coupon; - } catch (\Exception $e) { - return $e->getMessage(); - } - - } - -} \ No newline at end of file diff --git a/packages/sinopec/src/Action/ysd/YsdQuery.php b/packages/sinopec/src/Action/ysd/YsdQuery.php deleted file mode 100644 index 9951c6e..0000000 --- a/packages/sinopec/src/Action/ysd/YsdQuery.php +++ /dev/null @@ -1,51 +0,0 @@ -outletId)->first(); - - if (!$info) { - throw new \Exception('网点编号错误,未查询到网点信息'); - } - - $coupon = ActivityCoupon::where('code', $this->redemptionCode)->first(); - - if (!$coupon) { - throw new \Exception('卡券编号错误,未查询到卡券信息'); - } - - $activity = $coupon->activity; - if (!$activity) { - throw new \Exception('操作失败,未查到活动信息'); - } - - //获取所有可核销渠道 - $verifications = $activity->verifications()->pluck('user_id'); - - if ($verifications->isEmpty()) { - throw new \Exception('操作失败,此活动还没有配置可核券渠道,请联系相关人员进行配置。'); - } - - if (!in_array($info->parent_id, $verifications->toArray())) { - throw new \Exception('操作失败,您没有权限查询此卡券信息。'); - } - - return $coupon; - - } catch (\Exception $e) { - return $e->getMessage(); - } - - } - -} \ No newline at end of file diff --git a/packages/sinopec/src/Action/ysd/YsdVerification.php b/packages/sinopec/src/Action/ysd/YsdVerification.php deleted file mode 100644 index d1f85de..0000000 --- a/packages/sinopec/src/Action/ysd/YsdVerification.php +++ /dev/null @@ -1,195 +0,0 @@ -HasCheck(); - if ($res !== false) { - return $res; - } - - //查询卡券信息 返回model 返回string 说明报错 - $this->query_coupon = (new YsdQuery)->setOutletId($this->outletId) - ->setCode($this->redemptionCode) - ->start(); - - if (is_string($this->query_coupon)) { - return $this->query_coupon; - } - - //检查可核销次数,100元为1次。 - if ($this->query_coupon->activity && $this->query_coupon->activity->need_check) { - $ret = $this->CheckCount(); - if ($ret !== true) { - return $ret; - } - } - - //校验卡券 - $ticket = $this->checkCoupon(); - if (!is_array($ticket)) { - return $ticket; - } - - //增加核销记录 - $coupon = $this->AddCoupon(); - if (is_string($coupon)) { - return $coupon; - } - - DB::beginTransaction(); - - try { - $this->query_coupon->status = ActivityCoupon::STATUS_USED; - $this->query_coupon->used_at = now(); - $this->query_coupon->save(); - - $this->coupon->status = 2; - $this->coupon->remark = '核销成功'; - $this->coupon->save(); - //返回的数据 - $resdata = [ - 'name' => $this->coupon->couponName, - 'total' => $this->coupon->total, - 'price' => $this->coupon->price, - ]; - //核销成功 执行分润 - $this->coupon->profit(); - DB::commit(); - - event(new ConponCallback($this->query_coupon)); - - return $resdata; - } catch (Exception $e) { - DB::rollback(); - - $this->coupon->status = 3; - $this->coupon->remark = '核销失败 ' . $e->getMessage(); - $this->coupon->save(); - - return $this->coupon->remark; - } - - } - - /** - * Notes: 检查卡券信息 - * @Author: 玄尘 - * @Date : 2020/6/29 16:40 - * @return string - */ - public function checkCoupon() - { - - if (!$this->query_coupon->canRedemption()) { - return '核销失败,优惠券不可核销'; - } - - $now = now(); - - if ($this->query_coupon->start_at->gt($now)) { - return '核销失败,卡券未到可用时间。请在' . $this->query_coupon->start_at->format('Y-m-d H:i:s') . '后使用'; - } - - if ($now->gt($this->query_coupon->end_at)) { - return '核销失败,卡券已过期。'; - } - - $rule_code = $this->query_coupon->activity->rule->code; - $code = $this->user->code->where('code', $rule_code)->first(); - - if (!$code) { - return "核销失败,您没有权限使用此卡券优惠活动。"; - } - - $ticket = explode('-', $rule_code); - if (!is_array($ticket) || count($ticket) != 3) { - return "核销失败,卡券规则格式不正确"; - } - - $full = $ticket[1]; //full100 - $price = $ticket[2]; - // preg_match('/(\d{3}(\.\d+)?)/is', $full, $match); - preg_match('/\d+/', $full, $match); - - if (!is_array($match)) { - return "核销失败,卡券规则格式不正确。"; - } - - if (!is_numeric($this->total)) { - return "核销失败,订单金额必须是数字"; - } - - if ($match[0] > $this->total) { - return '核销失败,订单金额不足。'; - } - - return $this->ticket = [ - 'total' => $match[0], - 'price' => $price, - 'profit' => $code->profit, - ]; - - } - - /** - * Notes: 如可核销记录 - * @Author: 玄尘 - * @Date : 2020/7/21 15:03 - * @return string - */ - public function AddCoupon() - { - DB::beginTransaction(); - - try { - $couponData = [ - 'user_id' => $this->user->id, - 'type' => Coupon::TYPE_YSD, - 'outletId' => $this->outletId, - 'orderid' => $this->orderid, - 'PaOutletId' => '', - 'redemptionCode' => $this->redemptionCode, - 'thirdPartyGoodsId' => $this->query_coupon->activity->rule->code, - 'couponName' => $this->query_coupon->activity->title, - 'price' => $this->ticket['price'], - 'total' => $this->total, - 'profit' => $this->ticket['profit'], - 'status' => $this->query_coupon->status, - 'startTime' => $this->query_coupon->start_at, - 'endTime' => $this->query_coupon->end_at, - ]; - - $this->coupon = Coupon::create($couponData); - - DB::commit(); - - return $this->coupon; - } catch (Exception $e) { - DB::rollback(); - - return $e->getMessage(); - } - - } - -} \ No newline at end of file diff --git a/packages/sinopec/src/Contracts/CheckCouponContracts.php b/packages/sinopec/src/Contracts/CheckCouponContracts.php index d673353..f2a398c 100644 --- a/packages/sinopec/src/Contracts/CheckCouponContracts.php +++ b/packages/sinopec/src/Contracts/CheckCouponContracts.php @@ -1,6 +1,6 @@ app->runningInConsole()) { - $this->publishes([__DIR__ . '/../config/xuanchen_coupon.php' => config_path('xuanchen_coupon.php')]); + $this->publishes([__DIR__ . '/../config/sinopec.php' => config_path('sinopec.php')]); } } @@ -24,7 +24,7 @@ class ServiceProvider extends LaravelServiceProvider */ public function boot() { - $this->mergeConfigFrom(__DIR__ . '/../config/xuanchen_coupon.php', 'xuanchen_coupon'); + $this->mergeConfigFrom(__DIR__ . '/../config/sinopec.php', 'sinopec'); } diff --git a/packages/sinopec/src/Coupon.php b/packages/sinopec/src/Sinopec.php similarity index 98% rename from packages/sinopec/src/Coupon.php rename to packages/sinopec/src/Sinopec.php index 6e716b5..9c325fc 100644 --- a/packages/sinopec/src/Coupon.php +++ b/packages/sinopec/src/Sinopec.php @@ -1,6 +1,6 @@