From d8cd8ae9a68d0ce934792ec2fa254d268e53b906 Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Tue, 13 Oct 2020 08:49:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B6=E6=AE=B5=E6=80=A7=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/IdentityController.php | 2 + .../Controllers/Unionpay/IndexController.php | 7 + .../Controllers/User/IndexController.php | 5 +- app/Admin/Renderable/User/Rule.php | 23 +++ app/Admin/Renderable/User/ServerKey.php | 31 +++ app/Exceptions/ApiUnionpayException.php | 45 ---- app/Exceptions/Handler.php | 2 - app/Listeners/ConponCallbackListener.php | 2 + app/Models/ActivityCoupon.php | 22 ++ app/Models/Coupon.php | 24 +++ app/Models/UnionpayLog.php | 7 + packages/coupon/src/Action/PinganAction.php | 10 +- packages/coupon/src/Action/YsdAction.php | 14 ++ .../coupon/src/Action/ysd/YsdReversal.php | 69 +++++++ packages/coupon/src/Coupon.php | 27 ++- packages/unionpay/src/Action/Query.php | 52 ++--- packages/unionpay/src/Action/Redemption.php | 58 ++++++ packages/unionpay/src/Action/Reversal.php | 73 +++++++ .../unionpay/src/Action/UnionPayAction.php | 90 -------- packages/unionpay/src/Action/Verification.php | 194 ------------------ packages/unionpay/src/UnionPay.php | 86 +++----- 21 files changed, 420 insertions(+), 423 deletions(-) create mode 100644 app/Admin/Renderable/User/Rule.php create mode 100644 app/Admin/Renderable/User/ServerKey.php delete mode 100644 app/Exceptions/ApiUnionpayException.php create mode 100644 packages/coupon/src/Action/ysd/YsdReversal.php create mode 100644 packages/unionpay/src/Action/Redemption.php create mode 100644 packages/unionpay/src/Action/Reversal.php delete mode 100644 packages/unionpay/src/Action/UnionPayAction.php delete mode 100644 packages/unionpay/src/Action/Verification.php diff --git a/app/Admin/Controllers/IdentityController.php b/app/Admin/Controllers/IdentityController.php index c68f903..6941dc8 100644 --- a/app/Admin/Controllers/IdentityController.php +++ b/app/Admin/Controllers/IdentityController.php @@ -10,6 +10,7 @@ use RuLong\Identity\Models\Identity; class IdentityController extends AdminController { + protected $title = '用户身份配置'; protected function grid() @@ -47,6 +48,7 @@ class IdentityController extends AdminController $grid->column('remark', '说明'); $grid->column('updated_at', '修改时间'); + return $grid; } diff --git a/app/Admin/Controllers/Unionpay/IndexController.php b/app/Admin/Controllers/Unionpay/IndexController.php index 14f2eb4..e6b7fa8 100644 --- a/app/Admin/Controllers/Unionpay/IndexController.php +++ b/app/Admin/Controllers/Unionpay/IndexController.php @@ -41,6 +41,10 @@ class IndexController extends AdminController $filter->column(1 / 2, function ($filter) { $filter->equal('msg_txn_code', '交易类型')->select(config('unionpay.type')); }); + $filter->column(1 / 2, function ($filter) { + $filter->equal('req_serial_no', '流水号'); + $filter->equal('orig_req_serial_no', '原流水号'); + }); }); $grid->column('id', '#ID#'); @@ -53,6 +57,9 @@ class IndexController extends AdminController $grid->column('mkt_code', '券码'); $grid->column('msg_sys_sn', '平台流水号'); $grid->column('req_serial_no', '查询流水号'); + $grid->column('status', '状态') + ->using(UnionpayLog::STATUS) + ->label(); // $grid->column('请求参数')->display(function ($title, $column) { // return '点击展开'; diff --git a/app/Admin/Controllers/User/IndexController.php b/app/Admin/Controllers/User/IndexController.php index e717aac..2463d38 100644 --- a/app/Admin/Controllers/User/IndexController.php +++ b/app/Admin/Controllers/User/IndexController.php @@ -143,12 +143,15 @@ class IndexController extends AdminController $grid->column('分润规则')->display(function ($title, $column) { return '点击展开'; })->modal(function ($model) { + if ($model->code) { $codes = $model->code->map(function ($code) { return $code->only(['name', 'code', 'profit']); }); - return new Table(['名称', '规则项', '分润金额(元)'], $codes->toArray()); + $table = new Table(['名称', '规则项', '分润金额(元)'], $codes->toArray()); + + return trim($table->render(), '"'); } }); diff --git a/app/Admin/Renderable/User/Rule.php b/app/Admin/Renderable/User/Rule.php new file mode 100644 index 0000000..abd6969 --- /dev/null +++ b/app/Admin/Renderable/User/Rule.php @@ -0,0 +1,23 @@ +code->map(function ($code) { + return $code->only(['name', 'code', 'profit']); + }); + $table = new Table(['名称', '规则项', '分润金额(元)'], $codes->toArray()); + + return $table->render(); + } + +} \ No newline at end of file diff --git a/app/Admin/Renderable/User/ServerKey.php b/app/Admin/Renderable/User/ServerKey.php new file mode 100644 index 0000000..37e36ea --- /dev/null +++ b/app/Admin/Renderable/User/ServerKey.php @@ -0,0 +1,31 @@ +server_key ?? '---', + ], + [ + 'DES3秘钥', $user->des3key ?? '---', + ], + ]; + + $table = new Table(['名称', '参数'], $data); + + return $table->render(); + die(); + } + +} \ No newline at end of file diff --git a/app/Exceptions/ApiUnionpayException.php b/app/Exceptions/ApiUnionpayException.php deleted file mode 100644 index 2c1fa39..0000000 --- a/app/Exceptions/ApiUnionpayException.php +++ /dev/null @@ -1,45 +0,0 @@ -data = $data; - } - - /** - * @return array - */ - public function getData() - { - return $this->data; - } - - /** - * 异常输出 - */ - public function render($request) - { - return response()->json($this->getData(), 200); - } - -} \ No newline at end of file diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index a94e5d4..4d29ea2 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -13,7 +13,6 @@ class Handler extends ExceptionHandler * @var array */ protected $dontReport = [ - ApiException::class, ]; /** @@ -32,7 +31,6 @@ class Handler extends ExceptionHandler */ public function report(Exception $exception) { - info($exception->getMessage()); parent::report($exception); } diff --git a/app/Listeners/ConponCallbackListener.php b/app/Listeners/ConponCallbackListener.php index 75ac784..1ecb617 100644 --- a/app/Listeners/ConponCallbackListener.php +++ b/app/Listeners/ConponCallbackListener.php @@ -6,6 +6,7 @@ use App\Events\ConponCallback; use App\Models\ActivityCouponLog; use GuzzleHttp\Client; use Illuminate\Contracts\Queue\ShouldQueue; +use RuntimeException; class ConponCallbackListener implements ShouldQueue { @@ -55,6 +56,7 @@ class ConponCallbackListener implements ShouldQueue $data['remark'] = '接口错误'; $error = true; } + ActivityCouponLog::create($data); if ($error) { diff --git a/app/Models/ActivityCoupon.php b/app/Models/ActivityCoupon.php index b3c49fa..a128f01 100644 --- a/app/Models/ActivityCoupon.php +++ b/app/Models/ActivityCoupon.php @@ -64,4 +64,26 @@ class ActivityCoupon extends Model return $this->canRedemption(); } + /** + * Notes: 是否可以撤销 + * @Author: 玄尘 + * @Date : 2020/10/12 11:57 + * @return bool + */ + public function canReversal() + { + return $this->status == self::STATUS_USED; + } + + /** + * Notes: 撤销 + * @Author: 玄尘 + * @Date : 2020/10/12 13:56 + */ + public function reversal() + { + $this->status = self::STATUS_INIT; + $this->save(); + } + } diff --git a/app/Models/Coupon.php b/app/Models/Coupon.php index 5831f90..8d5a322 100644 --- a/app/Models/Coupon.php +++ b/app/Models/Coupon.php @@ -37,6 +37,9 @@ class Coupon extends Model case 4: return '已分润'; break; + case 5: + return '已撤销'; + break; default: return '未知状态'; break; @@ -88,6 +91,27 @@ class Coupon extends Model } } + /** + * Notes: 撤销 + * @Author: 玄尘 + * @Date : 2020/10/12 13:56 + */ + public function reversal() + { + $this->status = 5; + $this->remark = '撤销成功'; + $this->save(); + + if ($this->profit > 0) { + return $this->user->account->rule('refreeze', -$this->profit, false, [ + 'coupon_id' => $this->id, + 'redemptionCode' => $this->redemptionCode, + ]); + } else { + return '不需要操作'; + } + } + /** * Notes: 撤销分润 * @Author: 玄尘 diff --git a/app/Models/UnionpayLog.php b/app/Models/UnionpayLog.php index 4ac1bde..c0ac23e 100644 --- a/app/Models/UnionpayLog.php +++ b/app/Models/UnionpayLog.php @@ -10,6 +10,13 @@ class UnionpayLog extends Model 'out_source' => 'array', ]; + const STATUS_SUCCESS = 1; + const STATUS_ERROR = 0; + const STATUS = [ + self::STATUS_SUCCESS => '成功', + self::STATUS_ERROR => '失败', + ]; + public function coupon() { return $this->hasOne(Coupon::class, 'redemptionCode', 'mkt_code'); diff --git a/packages/coupon/src/Action/PinganAction.php b/packages/coupon/src/Action/PinganAction.php index 8dc68b1..c2610ec 100644 --- a/packages/coupon/src/Action/PinganAction.php +++ b/packages/coupon/src/Action/PinganAction.php @@ -41,13 +41,19 @@ class PinganAction extends Init implements CouponContracts } //发券 - function grant() + public function grant() { return '没这个接口'; } //作废 - function destroy() + public function destroy() + { + return '没这个接口'; + } + + //撤销 + public function reversal() { return '没这个接口'; } diff --git a/packages/coupon/src/Action/YsdAction.php b/packages/coupon/src/Action/YsdAction.php index 6817c8d..a72640d 100644 --- a/packages/coupon/src/Action/YsdAction.php +++ b/packages/coupon/src/Action/YsdAction.php @@ -5,6 +5,7 @@ namespace XuanChen\Coupon\Action; use XuanChen\Coupon\Action\ysd\YsdDestroy; use XuanChen\Coupon\Action\ysd\YsdGrant; use XuanChen\Coupon\Action\ysd\YsdQuery; +use XuanChen\Coupon\Action\ysd\YsdReversal; use XuanChen\Coupon\Action\ysd\YsdVerification; use XuanChen\Coupon\Contracts\CouponContracts; @@ -88,4 +89,17 @@ class YsdAction extends Init implements CouponContracts ->start(); } + /** + * Notes: 撤销 + * @Author: 玄尘 + * @Date : 2020/10/12 11:55 + * @return array|string + */ + public function reversal() + { + return $res = (new YsdReversal)->setCode($this->redemptionCode) + ->setOutletId($this->outletId) + ->start(); + } + } diff --git a/packages/coupon/src/Action/ysd/YsdReversal.php b/packages/coupon/src/Action/ysd/YsdReversal.php new file mode 100644 index 0000000..b1364d7 --- /dev/null +++ b/packages/coupon/src/Action/ysd/YsdReversal.php @@ -0,0 +1,69 @@ +redemptionCode) { + try { + if (!$this->outletId) { + throw new \Exception('缺少网点id'); + } + + $activityCoupon = ActivityCoupon::where('code', $this->redemptionCode)->first(); + + if (!$activityCoupon) { + throw new \Exception('未查询到卡券信息'); + } + + if (!$activityCoupon->canReversal()) { + throw new \Exception('操作失败,卡券当前状态不能操作'); + } + + $outlet = User::where('outlet_id', $this->outletId)->first(); + + if (empty($outlet)) { + return '操作失败,未查询到此网点信息。'; + } + + $grants = $activityCoupon->activity->grants()->pluck('user_id'); + if ($grants->isEmpty()) { + return '操作失败,此活动还没有配置可发券渠道,请联系相关人员进行配置。'; + } + + if (!in_array($outlet->parent_id, $grants->toArray())) { + return '操作失败,您没有权限作废此优惠券。'; + } + + $coupon = Coupon::where('redemptionCode', $this->redemptionCode) + ->where('status', 2) + ->first(); + + if ($activityCoupon && $coupon) { + //撤销 + $activityCoupon->reversal(); + //撤销 + $coupon->reversal(); + } + event(new ConponCallback($activityCoupon)); + + return true; + } catch (\Exception $e) { + return $e->getMessage(); + } + } else { + return '未获取到券码。'; + } + + } + +} \ No newline at end of file diff --git a/packages/coupon/src/Coupon.php b/packages/coupon/src/Coupon.php index db5cc97..0e321c1 100644 --- a/packages/coupon/src/Coupon.php +++ b/packages/coupon/src/Coupon.php @@ -89,7 +89,8 @@ class Coupon { $rules = config('xuanchen_coupon.rules'); if (!$rules) { - return '系统出错,未找到配置文件'; + throw new \Exception('系统出错,未找到配置文件'); + } $model = ''; @@ -141,4 +142,28 @@ class Coupon } + /** + * Notes: 冲正 撤销 已经核销的改为未核销状态 + * @Author: 玄尘 + * @Date : 2020/10/12 11:54 + * @param $redemptionCode + * @param $outletId + * @return string + */ + public static function Reversal($redemptionCode, $outletId) + { + try { + $model = self::getModelByCode($redemptionCode); + if (is_string($model)) { + return $model; + } + + return $model->setCode($redemptionCode) + ->setOutletId($outletId) + ->reversal(); + } catch (\Exception $e) { + return $e->getMessage(); + } + } + } diff --git a/packages/unionpay/src/Action/Query.php b/packages/unionpay/src/Action/Query.php index 820b35a..755f393 100644 --- a/packages/unionpay/src/Action/Query.php +++ b/packages/unionpay/src/Action/Query.php @@ -2,49 +2,41 @@ namespace XuanChen\UnionPay\Action; -use App\Models\ActivityCoupon; -use App\Models\User; +use XuanChen\Coupon\Coupon; class Query { + public $outlet_id; + + public $params; + + public $outdata; + public function start() { try { - $info = User::where('outlet_id', $this->outletId)->first(); + $res = Coupon::Query($this->params['mkt_code'], $this->outlet_id); - if (!$info) { - throw new \Exception('网点编号错误,未查询到网点信息'); + if (is_array($res)) { + $this->outdata['pos_display'] = $res['name']; + $this->outdata['discount'] = $res['price'] * 100; + $this->outdata['actual_amt'] = (int)bcsub($this->params['amount'], $res['price'] * 100); + } else { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = $res; } - $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(); + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = $e->getMessage(); } } + public function back() + { + return $this->outdata; + } + } \ No newline at end of file diff --git a/packages/unionpay/src/Action/Redemption.php b/packages/unionpay/src/Action/Redemption.php new file mode 100644 index 0000000..e7b3246 --- /dev/null +++ b/packages/unionpay/src/Action/Redemption.php @@ -0,0 +1,58 @@ +params['orig_req_serial_no']) + ->where('msg_txn_code', '002025') + ->latest() + ->first(); + if (!$query) { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = '销账失败,未查询到前置数据。'; + } else { + $this->outdata['orig_amt'] = (int)$query->in_source['amount']; //订单金额 原始金额 + $this->outdata['discount_amt'] = $query->out_source['discount']; //折扣金额 + $this->outdata['pay_amt'] = $query->out_source['actual_amt']; //折扣后金额 + + //获取银联渠道 + $user = User::find($this->agent_id); + + if ($query) { + $coupon = Coupon::Redemption($user, $query->mkt_code, $this->params['orig_amt'] / 100, $this->outlet_id, ''); + if (!is_array($coupon)) { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = $coupon; + } + } else { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。'; + } + } + + } + + public function back() + { + return $this->outdata; + } + +} \ No newline at end of file diff --git a/packages/unionpay/src/Action/Reversal.php b/packages/unionpay/src/Action/Reversal.php new file mode 100644 index 0000000..460de6c --- /dev/null +++ b/packages/unionpay/src/Action/Reversal.php @@ -0,0 +1,73 @@ +params['orig_req_serial_no']) + ->where('msg_txn_code', '002100') + ->where('status', 1) + ->latest() + ->first(); + + if ($info) { + $query = UnionpayLog::where('req_serial_no', $info->orig_req_serial_no) + ->where('msg_txn_code', '002025') + ->where('status', 1) + ->latest() + ->first(); + + $coupon = CouponModel::where('redemptionCode', $query->mkt_code) + ->where('status', 2) + ->latest() + ->first(); + + if ($query && $coupon) { + //优惠券核销成功 + if ($coupon->status == 2) { + $res = Coupon::Reversal($coupon->redemptionCode, $this->outlet_id); + if ($res !== true) { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = $res; + } + } else { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = '优惠券状态不对'; + } + } else { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = '未查询到卡券信息。'; + } + + } else { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = '未查询到销账接口数据。'; + } + } catch (\Exception $e) { + $this->outdata['msg_rsp_code'] = '9999'; + $this->outdata['msg_rsp_desc'] = $e->getMessage(); + } + + } + + public function back() + { + return $this->outdata; + } + +} \ No newline at end of file diff --git a/packages/unionpay/src/Action/UnionPayAction.php b/packages/unionpay/src/Action/UnionPayAction.php deleted file mode 100644 index a162c7a..0000000 --- a/packages/unionpay/src/Action/UnionPayAction.php +++ /dev/null @@ -1,90 +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/unionpay/src/Action/Verification.php b/packages/unionpay/src/Action/Verification.php deleted file mode 100644 index 8fb3513..0000000 --- a/packages/unionpay/src/Action/Verification.php +++ /dev/null @@ -1,194 +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/unionpay/src/UnionPay.php b/packages/unionpay/src/UnionPay.php index a5d9e55..ee7825e 100644 --- a/packages/unionpay/src/UnionPay.php +++ b/packages/unionpay/src/UnionPay.php @@ -6,6 +6,9 @@ use App\Models\UnionpayLog; use App\Models\User; use XuanChen\UnionPay\Action\Init; use XuanChen\Coupon\Coupon; +use XuanChen\UnionPay\Action\Query; +use XuanChen\UnionPay\Action\Redemption; +use XuanChen\UnionPay\Action\Reversal; /** * 银联入口 @@ -71,69 +74,33 @@ class UnionPay extends Init switch ($this->msg_txn_code) { //聚合营销优惠查询接口 case '002025': - $res = Coupon::Query($this->params['mkt_code'], $this->outlet_id); - if (is_array($res)) { - $this->outdata['pos_display'] = $res['name']; - $this->outdata['discount'] = $res['price'] * 100; - $this->outdata['actual_amt'] = (int)bcsub($this->params['amount'], $res['price'] * 100); - } else { - $this->outdata['msg_rsp_code'] = '9999'; - $this->outdata['msg_rsp_desc'] = $res; - } + $action = new Query(); + $action->outlet_id = $this->outlet_id; + $action->params = $this->params; + $action->outdata = $this->outdata; + $action->start(); + $this->outdata = $action->back(); break; //销账交易接口 case '002100': - //查询聚合信息 - $query = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no']) - ->where('msg_txn_code', '002025') - ->latest() - ->first(); - - $this->outdata['orig_amt'] = $query->in_source['amount']; //订单金额 原始金额 - $this->outdata['discount_amt'] = $query->out_source['discount']; //折扣金额 - $this->outdata['pay_amt'] = $query->out_source['actual_amt']; //折扣后金额 - - //获取银联渠道 - $user = User::find($this->agent_id); - - if ($query) { - $coupon = []; - $coupon = Coupon::Redemption($user, $query->mkt_code, $this->params['orig_amt'] / 100, $this->outlet_id, ''); - if (!is_array($coupon)) { - $this->outdata['msg_rsp_code'] = '9999'; - $this->outdata['msg_rsp_desc'] = $coupon; - } - } else { - $this->outdata['msg_rsp_code'] = '9999'; - $this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。'; - } - + $action = new Redemption(); + $action->outlet_id = $this->outlet_id; + $action->params = $this->params; + $action->outdata = $this->outdata; + $action->agent_id = $this->agent_id; + $action->start(); + $this->outdata = $action->back(); break; + //冲正 case '002101': - break; + //撤销 case '002102': - //查询聚合信息 - $info = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no']) - ->where('msg_txn_code', '002025') - ->latest() - ->first(); - - if ($info) { - if ($info->coupon) { - $ret = $info->coupon->reprofit(); - if ($ret !== true) { - $this->outdata['msg_rsp_code'] = '9999'; - $this->outdata['msg_rsp_desc'] = $ret; - } - } else { - $this->outdata['msg_rsp_code'] = '9999'; - $this->outdata['msg_rsp_desc'] = '为查询到优惠券信息。'; - } - - } else { - $this->outdata['msg_rsp_code'] = '9999'; - $this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。'; - } + $action = new Reversal(); + $action->outlet_id = $this->outlet_id; + $action->params = $this->params; + $action->outdata = $this->outdata; + $action->start(); + $this->outdata = $action->back(); break; default: break; @@ -202,6 +169,7 @@ class UnionPay extends Init ]; switch ($this->msg_txn_code) { + //查询 case '002025': $basics = array_merge($basics, [ "discount" => 0, @@ -212,6 +180,7 @@ class UnionPay extends Init "pos_mkt_ad" => config('unionpay.pos_receipt'), ]); break; + //销账 case '002100': $basics = array_merge($basics, [ 'msg_ver' => 0.1, @@ -223,11 +192,12 @@ class UnionPay extends Init 'event_no' => '',//活动号 直接为空就可以 ]); break; + //冲正 case '002101': $basics = array_merge($basics, [ 'msg_ver' => 0.1, ]); - break; + //撤销 case '002102': $basics = array_merge($basics, [ 'msg_ver' => 0.1,